DDL | 數(shù)據(jù)定義語(yǔ)言 | create、drop、alter | 數(shù)據(jù)定義語(yǔ)言 create、drop、alter 語(yǔ)句 。 |
DML | 數(shù)據(jù)操縱語(yǔ)言 | insert、delete、update | 定義對(duì)數(shù)據(jù)庫(kù)記錄的增、刪、改操作。 |
DQL | 數(shù)據(jù)庫(kù)查詢語(yǔ)言 | select | 定義對(duì)數(shù)據(jù)庫(kù)記錄的查詢操作。 |
DCL | 數(shù)據(jù)庫(kù)控制語(yǔ)言 | grant、remove |
定義對(duì)數(shù)據(jù)庫(kù)、表、字段、用戶的訪問(wèn)權(quán)限和安全級(jí)別。 (授權(quán)grant,收回權(quán)限r(nóng)evoke等)。 |
TCL | 事務(wù)控制語(yǔ)言 |
set autocommit=0、 start transaction、 savepoint、commit、rollback |
定義對(duì)數(shù)據(jù)庫(kù)的事務(wù)操作。 |
這小節(jié)主要了解下數(shù)據(jù)定義語(yǔ)言DDL(Data Define Language)。我們用它對(duì)數(shù)據(jù)庫(kù)、表進(jìn)行一些管理操作(創(chuàng)建、刪除、修改等),比如:建庫(kù)、刪庫(kù)、建表、修改表、刪除表、對(duì)字段的增刪改等,庫(kù)表結(jié)構(gòu)的管理。
接下來(lái)我們逐一來(lái)說(shuō)明(下文[]中的內(nèi)容屬于可選項(xiàng))。
數(shù)據(jù)庫(kù)管理
創(chuàng)建數(shù)據(jù)庫(kù)
create database [if not exists] dbname;
刪除數(shù)據(jù)庫(kù)
drop databases [if exists] dbname;
完整的寫(xiě)法如下:
drop databases [if exists] o_dbname; create database n_dbname;
o_dbname 代表舊的數(shù)據(jù)庫(kù)名,n_dbname 代表新的數(shù)據(jù)庫(kù)名。
測(cè)試一下:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | buyerparty | | buyerparty1 | | git_jeeshop | | jz | | kdmy | | kdmygf | | localsdk | | mgrcentercontrol | | mysql | | performance_schema | | stroke_data | | test | +--------------------+ 13 rows in set mysql> drop database if exists test1; Query OK, 0 rows affected mysql> create database test1; Query OK, 1 row affected mysql> create database test1; 1007 - Can't create database 'test1'; database exists
通過(guò)上面的測(cè)試可以知道:刪除之前要先判斷數(shù)據(jù)庫(kù)是否存在,否則會(huì)報(bào)出異常;同時(shí)創(chuàng)建之前也要判斷是否存在,如果存在則會(huì)提示已存在。
表管理
創(chuàng)建表
在數(shù)據(jù)庫(kù)中一張表的基本語(yǔ)法格式如下:
create table tbname( column_name_1 column_type_1[(n)] [constraints] [comment 'comment1'], column_name_2 column_type_2[(n)] [constraints] [comment 'comment2'], column_name_3 column_type_3[(n)] [constraints] [comment 'comment3'] )[table_options];
語(yǔ)法說(shuō)明
1、column_name是指字段名;column_type指的是字段類(lèi)型(CHAR、INT等);n代表字段寬度,可選;constraints 約束,可選;comment 為字段備注,可以對(duì)字段詳細(xì)描述。
2、同一個(gè)表里面,column_name不能相同
3、字段名和類(lèi)型為必選,其他均為可選參數(shù)
4、類(lèi)型限制了 字段 的存儲(chǔ)格式,必須以給定的數(shù)據(jù)類(lèi)型來(lái)存儲(chǔ),并可以額外添加的約束
約束說(shuō)明
not null:非空約束
mysql> use test; Database changed mysql> create table if not exists `user1`(age int comment '年齡',name char(5) comment '姓名' not null); Query OK, 0 rows affected mysql> insert into user1 values(8,null); 1048 - Column 'name' cannot be null
建表的時(shí)候,對(duì)name字段做了非空約束,這時(shí)候傳入的值為null,就會(huì)有錯(cuò)誤提示。所以非空約束的目的是保證字段不為空。
default value:提供字段默認(rèn)值
mysql> use test; Database changed mysql> create table if not exists `user2`(age int not null default 0 comment '年齡',name char(50) comment '姓名' not null); Query OK, 0 rows affected mysql> insert into user2(name) values('brand'); Query OK, 1 row affected mysql> select * from user2; +-----+-------+ | age | name | +-----+-------+ | 0 | brand | +-----+-------+ 1 row in set
設(shè)置了默認(rèn)值之后,如果在寫(xiě)入數(shù)據(jù)時(shí),不指定值,他會(huì)自動(dòng)取默認(rèn)值0。
primary key:標(biāo)識(shí)主鍵約束
設(shè)置該字段為表的主鍵,全局唯一標(biāo)識(shí)錄,插入重復(fù)時(shí)報(bào)錯(cuò)。
有兩種表現(xiàn)方式:一種是直接在字段約束中跟上;一種是字段都聲明完了之后,在結(jié)尾加上,與上一個(gè)字段之間用逗號(hào)隔開(kāi)。
mysql> use test; Database changed mysql> create table if not exists `user3`(id int primary key,age int not null default 0 comment '年齡',name char(50) comment '姓名' not null); Query OK, 0 rows affected mysql> insert into user3 values(1,20,'brand'); Query OK, 1 row affected mysql> insert into user3 values(1,22,'sol'); 1062 - Duplicate entry '1' for key 'PRIMARY' mysql> insert into user3 values(2,22,'sol'); Query OK, 1 row affected mysql> select * from user3; +----+-----+-------+ | id | age | name | +----+-----+-------+ | 1 | 20 | brand | | 2 | 22 | sol | +----+-----+-------+ 2 rows in set
如上,主鍵必須保持值的唯一性,如果插入重復(fù)值,會(huì)提示違反主鍵約束
另外一種方式是在字段聲明的尾部,可以支持多個(gè)主鍵,用逗號(hào)隔開(kāi)并且不可重復(fù),格式:primary key(字段1,字段2,字段n),這種叫組合主鍵(或復(fù)合主鍵),舉個(gè)栗子:
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:標(biāo)識(shí)外鍵約束
語(yǔ)法:foreign key(t1_columnname) references t2(columnname),t1 為當(dāng)前表,t2為外鍵表,當(dāng)前表和外鍵表有一個(gè)字段約束成外鍵。
mysql> create table if not exists `class`(classid int primary key,classname varchar(50)); Query OK, 0 rows affected 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)); Query OK, 0 rows affected mysql> insert into `user4` values(1,20,'brand',1); 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`)) mysql> insert into `class` values(1,'grad 3'); Query OK, 1 row affected mysql> insert into `user4` values(1,20,'brand',1); Query OK, 1 row affected mysql> select a.age as '年齡',a.name as '學(xué)生姓名',b.classname as '班級(jí)' from user4 a left join class b on a.cid = b.classid; +------+----------+--------+ | 年齡 | 學(xué)生姓名 | 班級(jí) | +------+----------+--------+ | 20 | brand | grad 3 | +------+----------+--------+ 1 row in set
幾點(diǎn)說(shuō)明:
1、插入user4表的時(shí)候,會(huì)檢查關(guān)聯(lián)的外鍵classid的值是否存在,如果不存在就會(huì)報(bào)錯(cuò)誤。如上述代碼中第三段,classid=1的值在class表中不存在。
2、建立外鍵關(guān)系的兩張表的對(duì)應(yīng)字段,類(lèi)型需要保持一致。
3、設(shè)置為外鍵的字段不能為本表的主鍵,而關(guān)聯(lián)表的字段需要為主鍵。(所以外鍵cid關(guān)聯(lián)到class表的classid字段為主鍵)。
unique key:標(biāo)識(shí)唯一值約束
可以設(shè)置一個(gè)到多個(gè)字段,不允許重復(fù)值,重復(fù)會(huì)報(bào)違反唯一約束,導(dǎo)致插入失敗。
同樣的有兩種定義方式,一種是直接在字段后設(shè)置,一種是定義完所有字段之后再設(shè)置。以下例子:
mysql> create table `user5` (id int primary key,name varchar(50),ident char(18) unique key); Query OK, 0 rows affected mysql> create table `user6` (id int primary key,name varchar(50),ident char(18) not null,sex int not null,unique key(ident,sex)); Query OK, 0 rows affected mysql> insert into `user5` values(1,'brand','012345678901234567'); Query OK, 1 row affected mysql> insert into `user5` values(2,'sol','012345678901234567'); 1062 - Duplicate entry '012345678901234567' for key 'ident'
第二段中演示了支持多字段,用逗號(hào)隔開(kāi),語(yǔ)法格式:unique key(字段1,字段2,字段n);
第三段重復(fù)輸入了ident的值,他就提示重復(fù)輸入了。
auto_inc:標(biāo)識(shí)自動(dòng)增長(zhǎng)
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 說(shuō)明:
1、auto_increacement 的字段為自動(dòng)增長(zhǎng),默認(rèn)值從1開(kāi)始,每次+1
2、自動(dòng)增長(zhǎng)字段的初始值、步長(zhǎng)可以在mysql中進(jìn)行設(shè)置,比如設(shè)置初始值為1萬(wàn),步長(zhǎng)每次增長(zhǎng)10
3、自增列當(dāng)前值存儲(chǔ)在內(nèi)存中,數(shù)據(jù)庫(kù)重啟后,會(huì)查詢當(dāng)前表中自增列max為當(dāng)前值。
4、如果表數(shù)據(jù)被清空并重啟數(shù)據(jù)庫(kù),自增列會(huì)從初始值開(kāi)始。
刪除表
drop table [if exists] tname;
修改表名、備注
alter table o_tname rename [to] n_tname; alter table tname comment 'memo';
復(fù)制表
僅復(fù)制架構(gòu)
create table tname like from_tname;
mysql> select * from `user7`; +----+-------+ | id | name | +----+-------+ | 1 | brand | | 2 | sol | | 3 | helen | +----+-------+ 3 rows in set mysql> create table `user8` like `user7`; Query OK, 0 rows affected mysql> select * from `user8`; Empty set
復(fù)制架構(gòu)+數(shù)據(jù)
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
數(shù)據(jù)和架構(gòu)都被復(fù)制過(guò)來(lái)了,這個(gè)超實(shí)用。
管理字段
添加字段
alter table tname add column column_name column_type [constraints];
mysql> select * from `user9`; +----+-------+ | id | name | +----+-------+ | 1 | brand | | 2 | sol | | 3 | helen | +----+-------+ 3 rows in set mysql> alter table `user9` add column newcolumn int not null default 0; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from `user9`; +----+-------+-----------+ | id | name | newcolumn | +----+-------+-----------+ | 1 | brand | 0 | | 2 | sol | 0 | | 3 | helen | 0 | +----+-------+-----------+ 3 rows in set
修改字段
alter table tname modify column col_name new_col_type [constraints]; -- 修改類(lèi)型、約束,不能修改字段名 alter table tname change column col_name new_col_name new_col_type [constraints]; -- 修改字段名、類(lèi)型、約束
以下分別是modify和change示例:
mysql> desc `user9`; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name | varchar(50) | YES | | NULL | | | newcolumn | int(11) | NO | | 0 | | +-----------+-------------+------+-----+---------+-------+ 3 rows in set mysql> alter table `user9` modify column name varchar(100); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0 mysql> desc `user9`; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name | varchar(100) | YES | | NULL | | | newcolumn | int(11) | NO | | 0 | | +-----------+--------------+------+-----+---------+-------+ 3 rows in set
mysql> desc `user9`; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name | varchar(100) | YES | | NULL | | | newcolumn | int(11) | NO | | 0 | | +-----------+--------------+------+-----+---------+-------+ 3 rows in set mysql> alter table `user9` change column name name1 varchar(100); Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> desc `user9`; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name1 | varchar(100) | YES | | NULL | | | newcolumn | int(11) | NO | | 0 | | +-----------+--------------+------+-----+---------+-------+ 3 rows in set
刪除字段
alter table tname drop column col_name;
mysql> desc `user9`; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name1 | varchar(100) | YES | | NULL | | | newcolumn | int(11) | NO | | 0 | | +-----------+--------------+------+-----+---------+-------+ 3 rows in set mysql> alter table `user9` drop column newcolumn; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> desc `user9`; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | name1 | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 2 rows in set
以上就是MySQL ddl語(yǔ)句的使用的詳細(xì)內(nèi)容,更多關(guān)于MySQL ddl語(yǔ)句的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:沈陽(yáng) 天津 惠州 合肥 牡丹江 阿里 公主嶺 呼和浩特
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL ddl語(yǔ)句的使用》,本文關(guān)鍵詞 MySQL,ddl,語(yǔ)句,的,使用,MySQL,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。