SQL語言總結(學習筆記)

2021-06-22 19:19:46 字數 2914 閱讀 4783

儲存資料:

首先建立資料庫

之後建立資料表

運算元據

資料有哪些操作(curd)

c => create update read delete(drop)

1 庫操作:database

① 檢視資料庫: show databases like 「條件」;完整命令

show databases;

show databases like 『%t%』;

萬用字元:%可以任意長度的字串,包括空字串

例: showdatabases like 『%itcast%』; 全匹配

show databases like 『%itcast』;  前匹配

show databases like 『itcast%』;  後匹配

_下劃線可以匹配任意乙個字元

例:show databases like 『_itcast』; 不包括空字元

資料庫命名規則:可以使用字母,下劃線,數字。

注意:盡量不要使用系統保留字作為資料庫、表、欄位的名稱。

盡量使用小寫字母,原因是linux這種作業系統是大寫有區分,如果庫名比較長,不要使用駝峰法,如果userinfo 建議使用user_info。

② 檢視資料庫的建立資料

show create database資料庫名;

show create database itcast;

③ 建立資料庫

簡單方式:

create database 資料庫名;

create databases itcast2;

如果沒有指定資料的字符集,則使用資料庫安裝時的預設字符集。

有四個級別的字符集:

伺服器級:

資料庫級:

資料表級:

字段級:

資料庫安裝時需要指定字符集,並不是資料庫只支援這一種字符集。

容錯方式:

create database if not exists itcast2;

庫名包裹方法

就是用反引號將庫名包裹起來

create database `match`;

當然庫名錶名欄位名不是保留字最好也加上反引號。

指定字符集:

create database itcast default charset=gbk;

mysql中是utf8而不是utf-8;

④修改資料庫:alter

alter database 資料庫名 選項

我們都要什麼選項:字符集和校對規則

每建立乙個資料庫都會建立db.opt,下面的那行為校對規則。

alter database itcase charset=utf8;

⑤刪除資料庫

dropdatabase if exists 庫名

dropdatabase if exists itcast;

如果刪除資料庫,則資料庫所有資料及檔案全部都沒有了。

2 表操作

①     建立表

create table表名 (字段定義) 表選項

create table student_info

student_id int,

student_name varchar(20)

建立表之前選擇資料庫,使用use庫名的方法指定資料庫。

也可以在建立時寫成itcast.student_info;

②     檢視資料表,show

格式:show  tables 檢視所有的表

也支援條件查詢,模糊查詢

show table like 『student_info%』;

檢視表的建立資訊

格式:show createtable student_info;新增\g使內容更清晰

③     檢視表結構 desc, describe

格式:desc 表名

格式:showcolumns from 表名

show columns from student_info;

④     修改表結構 alter

格式:alter 表名修改內容1, …,…

modify 修改字串屬性

alter table student_info modify student_name varchar(30);

alter table student_info modify student_name studentnamevarchar(20);

change可以該欄位名

alter table student_info change student_name studentnamevarchar(20);

add 可以增加字段

alter table student_info add gender char(1) after student_name;

增加了字段同時指定了放置的順序

drop 可以刪除字段

alter table student_info drop gender;

⑤     複製表

格式:createtable like 或者 select

先插入一條資料,然後複製表結構

create table student_copy like student_info;此時沒有資料,只是複製表結構

同時複製資料呢?

create table student_copy select * from student_info;

⑥     交換表名的技巧,rename

本身rename table student_info to student_tmp, student_copyto student_info,

student_tmp to student_copy

⑦     刪除表

drop table if exists student_copy2; 執行第二次不報錯。

sql語言學習筆記

dml select delete 每刪除一行提交一次事務 update insert into等 ddl create drop alter truncate 刪除表內容,速度比delete快,因為只提交一次事務,無法恢復 sql修改表結構操作 1 新增表字段 alter table 表名 add...

SQL語言學習總結一

基礎查詢 一 語法 select 查詢列表 from 表名 二 特點 1.查詢列表可以是字段 常量 表示式 函式 也可以是多個 2.查詢結果是乙個虛擬表 三 示例 1.查詢單個字段 select 欄位名 from 表明 2.查詢多個字段 select 欄位名,欄位名 from 表明 3.查詢多有字段...

學習總結 SQL學習總結之SQL語法

選取所有列即原表返回 select from table name 例如 select distinct country from websites 例如 從 websites 表中選取國家為 cn 的所有 例如 從 websites 表中選取id為1的所有 文字字段 vs.數值字段 where 子...