SQL的語言分類主要包含以下幾種:mysql
DDL | 數據定義語言 | create、drop、alter | 數據定義語言 create、drop、alter 語句 。 |
DML | 數據操縱語言 | insert、delete、update | 定義對數據庫記錄的增、刪、改操做。 |
DQL | 數據庫查詢語言 | select | 定義對數據庫記錄的查詢操做。 |
DCL | 數據庫控制語言 | grant、remove | 定義對數據庫、表、字段、用戶的訪問權限和安全級別。git (受權grant,收回權限revoke等)。sql |
TCL | 事務控制語言 | set autocommit=0、數據庫 start transaction、安全 savepoint、commit、rollback架構 |
定義對數據庫的事務操做。 |
這小節主要了解下數據定義語言DDL(Data Define Language)。咱們用它對數據庫、表進行一些管理操做(建立、刪除、修改等),好比:建庫、刪庫、建表、修改表、刪除表、對字段的增刪改等,庫表結構的管理。ide
接下來咱們逐一來講明(下文[]中的內容屬於可選項)。測試
1 create database [if not exists] dbname;
drop databases [if exists] dbname;
1 drop databases [if exists] o_dbname; 2 create database n_dbname;
o_dbname 表明舊的數據庫名,n_dbname 表明新的數據庫名。spa
1 mysql> show databases; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | buyerparty | 7 | buyerparty1 | 8 | git_jeeshop | 9 | jz | 10 | kdmy | 11 | kdmygf | 12 | localsdk | 13 | mgrcentercontrol | 14 | mysql | 15 | performance_schema | 16 | stroke_data | 17 | test | 18 +--------------------+ 19 13 rows in set 20 21 mysql> drop database if exists test1; 22 Query OK, 0 rows affected 23 24 mysql> create database test1; 25 Query OK, 1 row affected 26 27 mysql> create database test1; 28 1007 - Can't create database 'test1'; database exists
經過上面的測試能夠知道:刪除以前要先判斷數據庫是否存在,不然會報出異常;同時建立以前也要判斷是否存在,若是存在則會提示已存在。code
在數據庫中一張表的基本語法格式以下:
1 create table tbname( 2 column_name_1 column_type_1[(n)] [constraints] [comment 'comment1'], 3 column_name_2 column_type_2[(n)] [constraints] [comment 'comment2'], 4 column_name_3 column_type_3[(n)] [constraints] [comment 'comment3'] 5 )[table_options];
一、column_name是指字段名;column_type指的是字段類型(CHAR、INT等);n表明字段寬度,可選;constraints 約束,可選;comment 爲字段備註,能夠對字段詳細描述。
二、同一個表裏面,column_name不能相同
三、字段名和類型爲必選,其餘均爲可選參數
四、類型限制了 字段 的存儲格式,必須以給定的數據類型來存儲,並能夠額外添加的約束
1 mysql> use test; 2 Database changed 3 4 mysql> create table if not exists `user1`(age int comment '年齡',name char(5) comment '姓名' not null); 5 Query OK, 0 rows affected 6 7 mysql> insert into user1 values(8,null); 8 1048 - Column 'name' cannot be null
建表的時候,對name字段作了非空約束,這時候傳入的值爲null,就會有錯誤提示。因此非空約束的目的是保證字段不爲空。
1 mysql> use test; 2 Database changed 3 4 mysql> create table if not exists `user2`(age int not null default 0 comment '年齡',name char(50) comment '姓名' not null); 5 Query OK, 0 rows affected 6 7 mysql> insert into user2(name) values('brand'); 8 Query OK, 1 row affected 9 mysql> select * from user2; 10 +-----+-------+ 11 | age | name | 12 +-----+-------+ 13 | 0 | brand | 14 +-----+-------+ 15 1 row in set
設置了默認值以後,若是在寫入數據時,不指定值,他會自動取默認值0。
設置該字段爲表的主鍵,全局惟一標識錄,插入重複時報錯。
有兩種表現方式:一種是直接在字段約束中跟上;一種是字段都聲明完了以後,在結尾加上,與上一個字段之間用逗號隔開。
1 mysql> use test; 2 Database changed 3 4 mysql> create table if not exists `user3`(id int primary key,age int not null default 0 comment '年齡',name char(50) comment '姓名' not null); 5 Query OK, 0 rows affected 6 7 mysql> insert into user3 values(1,20,'brand'); 8 Query OK, 1 row affected 9 10 mysql> insert into user3 values(1,22,'sol'); 11 1062 - Duplicate entry '1' for key 'PRIMARY' 12 13 mysql> insert into user3 values(2,22,'sol'); 14 Query OK, 1 row affected 15 16 mysql> select * from user3; 17 +----+-----+-------+ 18 | id | age | name | 19 +----+-----+-------+ 20 | 1 | 20 | brand | 21 | 2 | 22 | sol | 22 +----+-----+-------+ 23 2 rows in set
如上,主鍵必須保持值的惟一性,若是插入重複值,會提示違反主鍵約束
另一種方式是在字段聲明的尾部,能夠支持多個主鍵,用逗號隔開而且不可重複,格式:primary key(字段1,字段2,字段n),這種叫組合主鍵(或複合主鍵),舉個栗子:
1 create table if not exists `user4`(id int,age int not null default 0 comment '年齡',name char(50) comment '姓名' not null,primary key(id,name));
語法:foreign key(t1_columnname) references t2(columnname),t1 爲當前表,t2爲外鍵表,當前表和外鍵表有一個字段約束成外鍵。
1 mysql> create table if not exists `class`(classid int primary key,classname varchar(50)); 2 Query OK, 0 rows affected 3 4 mysql> create table if not exists `user4`(id int primary key,age int comment '年齡',name char(50) comment '姓名',cid int not null,foreign key(cid) references class(classid)); 5 Query OK, 0 rows affected 6 7 mysql> insert into `user4` values(1,20,'brand',1); 8 1452 - Cannot add or update a child row: a foreign key constraint fails (`test`.`user4`, CONSTRAINT `user4_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `class` (`classid`)) 9 10 mysql> insert into `class` values(1,'grad 3'); 11 Query OK, 1 row affected 12 13 mysql> insert into `user4` values(1,20,'brand',1); 14 Query OK, 1 row affected 15 16 mysql> select a.age as '年齡',a.name as '學生姓名',b.classname as '班級' from user4 a left join class b on a.cid = b.classid; 17 +------+----------+--------+ 18 | 年齡 | 學生姓名 | 班級 | 19 +------+----------+--------+ 20 | 20 | brand | grad 3 | 21 +------+----------+--------+ 22 1 row in set
幾點說明:
一、插入user4表的時候,會檢查關聯的外鍵classid的值是否存在,若是不存在就會報錯誤。如上述代碼中第三段,classid=1的值在class表中不存在。
二、創建外鍵關係的兩張表的對應字段,類型須要保持一致。
三、設置爲外鍵的字段不能爲本表的主鍵,而關聯表的字段須要爲主鍵。(因此外鍵cid關聯到class表的classid字段爲主鍵)。
能夠設置一個到多個字段,不容許重複值,重複會報違反惟一約束,致使插入失敗。
一樣的有兩種定義方式,一種是直接在字段後設置,一種是定義完全部字段以後再設置。如下例子:
1 mysql> create table `user5` (id int primary key,name varchar(50),ident char(18) unique key); 2 Query OK, 0 rows affected 3 4 mysql> create table `user6` (id int primary key,name varchar(50),ident char(18) not null,sex int not null,unique key(ident,sex)); 5 Query OK, 0 rows affected 6 7 mysql> insert into `user5` values(1,'brand','012345678901234567'); 8 Query OK, 1 row affected 9 mysql> insert into `user5` values(2,'sol','012345678901234567'); 10 1062 - Duplicate entry '012345678901234567' for key 'ident'
第二段中演示了支持多字段,用逗號隔開,語法格式:unique key(字段1,字段2,字段n);
第三段重複輸入了ident的值,他就提示重複輸入了。
mysql> create table `user7` (id int auto_increment primary key,name varchar(50)); Query OK, 0 rows affected mysql> insert into `user7`(name) values ('brand'),('sol'),('helen'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from `user7`; +----+-------+ | id | name | +----+-------+ | 1 | brand | | 2 | sol | | 3 | helen | +----+-------+ 3 rows in set
auto_increment 說明:
一、auto_increacement 的字段爲自動增加,默認值從1開始,每次+1
二、自動增加字段的初始值、步長能夠在mysql中進行設置,好比設置初始值爲1萬,步長每次增加10
三、自增列當前值存儲在內存中,數據庫重啓後,會查詢當前表中自增列max爲當前值。
四、若是表數據被清空並重啓數據庫,自增列會從初始值開始。
1 drop table [if exists] tname;
1 alter table o_tname rename [to] n_tname; 2 alter table tname comment 'memo';
1 create table tname like from_tname;
1 mysql> select * from `user7`; 2 +----+-------+ 3 | id | name | 4 +----+-------+ 5 | 1 | brand | 6 | 2 | sol | 7 | 3 | helen | 8 +----+-------+ 9 3 rows in set 10 11 mysql> create table `user8` like `user7`; 12 Query OK, 0 rows affected 13 14 mysql> select * from `user8`; 15 Empty set
1 create table tname [as] select column1,column2,... from from_tname [where condition];
mysql> select * from `user7`; +----+-------+ | id | name | +----+-------+ | 1 | brand | | 2 | sol | | 3 | helen | +----+-------+ 3 rows in set mysql> create table `user9` select id,name from `user7`; Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from `user9`; +----+-------+ | id | name | +----+-------+ | 1 | brand | | 2 | sol | | 3 | helen | +----+-------+ 3 rows in set
數據和架構都被複制過來了,這個超實用。
1 alter table tname add column column_name column_type [constraints];
1 mysql> select * from `user9`; 2 +----+-------+ 3 | id | name | 4 +----+-------+ 5 | 1 | brand | 6 | 2 | sol | 7 | 3 | helen | 8 +----+-------+ 9 3 rows in set 10 11 mysql> alter table `user9` add column newcolumn int not null default 0; 12 Query OK, 0 rows affected 13 Records: 0 Duplicates: 0 Warnings: 0 14 15 mysql> select * from `user9`; 16 +----+-------+-----------+ 17 | id | name | newcolumn | 18 +----+-------+-----------+ 19 | 1 | brand | 0 | 20 | 2 | sol | 0 | 21 | 3 | helen | 0 | 22 +----+-------+-----------+ 23 3 rows in set
1 alter table tname modify column col_name new_col_type [constraints]; -- 修改類型、約束,不能修改字段名 2 alter table tname change column col_name new_col_name new_col_type [constraints]; -- 修改字段名、類型、約束
如下分別是modify和change示例:
1 mysql> desc `user9`; 2 +-----------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +-----------+-------------+------+-----+---------+-------+ 5 | id | int(11) | NO | | 0 | | 6 | name | varchar(50) | YES | | NULL | | 7 | newcolumn | int(11) | NO | | 0 | | 8 +-----------+-------------+------+-----+---------+-------+ 9 3 rows in set 10 11 mysql> alter table `user9` modify column name varchar(100); 12 Query OK, 3 rows affected 13 Records: 3 Duplicates: 0 Warnings: 0 14 15 mysql> desc `user9`; 16 +-----------+--------------+------+-----+---------+-------+ 17 | Field | Type | Null | Key | Default | Extra | 18 +-----------+--------------+------+-----+---------+-------+ 19 | id | int(11) | NO | | 0 | | 20 | name | varchar(100) | YES | | NULL | | 21 | newcolumn | int(11) | NO | | 0 | | 22 +-----------+--------------+------+-----+---------+-------+ 23 3 rows in set
1 mysql> desc `user9`; 2 +-----------+--------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +-----------+--------------+------+-----+---------+-------+ 5 | id | int(11) | NO | | 0 | | 6 | name | varchar(100) | YES | | NULL | | 7 | newcolumn | int(11) | NO | | 0 | | 8 +-----------+--------------+------+-----+---------+-------+ 9 3 rows in set 10 11 mysql> alter table `user9` change column name name1 varchar(100); 12 Query OK, 0 rows affected 13 Records: 0 Duplicates: 0 Warnings: 0 14 15 mysql> desc `user9`; 16 +-----------+--------------+------+-----+---------+-------+ 17 | Field | Type | Null | Key | Default | Extra | 18 +-----------+--------------+------+-----+---------+-------+ 19 | id | int(11) | NO | | 0 | | 20 | name1 | varchar(100) | YES | | NULL | | 21 | newcolumn | int(11) | NO | | 0 | | 22 +-----------+--------------+------+-----+---------+-------+ 23 3 rows in set
1 alter table tname drop column col_name;
1 mysql> desc `user9`; 2 +-----------+--------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +-----------+--------------+------+-----+---------+-------+ 5 | id | int(11) | NO | | 0 | | 6 | name1 | varchar(100) | YES | | NULL | | 7 | newcolumn | int(11) | NO | | 0 | | 8 +-----------+--------------+------+-----+---------+-------+ 9 3 rows in set 10 11 mysql> alter table `user9` drop column newcolumn; 12 Query OK, 0 rows affected 13 Records: 0 Duplicates: 0 Warnings: 0 14 15 mysql> desc `user9`; 16 +-------+--------------+------+-----+---------+-------+ 17 | Field | Type | Null | Key | Default | Extra | 18 +-------+--------------+------+-----+---------+-------+ 19 | id | int(11) | NO | | 0 | | 20 | name1 | varchar(100) | YES | | NULL | | 21 +-------+--------------+------+-----+---------+-------+ 22 2 rows in set
爲了表示嚴謹性,每個都嘗試一遍,確實很耗時,寫到如今。