DDL(資料庫定義語言)

2021-10-05 04:14:40 字數 3813 閱讀 8336

1.1建立資料庫

​ 使用 create 命令建立資料庫,語法如下:

create database 資料庫名;

//如果不存在建立,存在就不建立

create database if not exists 資料庫名;

​ 例如:

create database myschool;

create database if not exists myschool;

1.2刪除資料庫

​ 使用drop 命令刪除資料庫,語法如下:

drop database 資料庫名;

//如果存在刪除,不存在就不刪除

drop database if exists 資料庫名;

​ 例如:

drop database myschool;

drop database if exists myschool;

1.3檢視所有資料庫名稱

​ 使用show關鍵字檢視所有資料庫名稱,語法如下:

show databases;
1.4切換資料庫

​ 使用use關鍵字切換資料庫,語法如下:

use 資料庫名;
1.5修改資料庫編碼

​ 使用charset關鍵字修改資料庫編碼,語法如下:

alter database 資料庫名 charset 字符集;
​ 注意:在mysql中所有的utf-8編碼都不能使用中間的「-」,即utf-8要書寫為utf8。

2.1建立表

​ 使用 create 命令建立資料表,語法如下:

create table[

if not exists] 表名 (

欄位1 資料型別 [字段屬性|約束]

[索引]

[注釋]

, 欄位2 資料型別 [字段屬性|約束]

[索引]

[注釋],.

....

. 欄位n 資料型別 [字段屬性|約束]

[索引]

[注釋],)

;

​ 例如:

create table `student`(

`studentno` int(4

) not null comment `學號` primary key, #非空,主鍵

`loginpwd` varchar(20

) not null comment `密碼`,

`studentname` varchar(50

) not null comment `學生姓名`,

`***` char(2

) default `男` not null comment `性別`, #非空,預設值男

`gradeid` int(4

) unsigned comment `年級編號`, #無符號數

`phone` varchar(50

) comment `聯絡**`,

`address` varchar

(255

) default `位址不詳` comment `位址`, #預設值「位址不詳」

`borndate` datetime comment `出生日期`,

`email` varchar(50

) comment `郵件賬號`,

`indetitycard` varchar(18

) unique key comment`身份證` #唯一

) comment=student; #表注釋「學生表」

2.2檢視當前資料庫所有表名稱
show tables;
2.3檢視表結構
desc 表名;
2.4刪除表

​ 使用drop 命令刪除表,語法如下:

drop table 表名;

//如果存在刪除,不存在就不刪除

drop table if exists table;

​ 例如:

drop table result;

drop table if exists result;

2.5修改表

​ (1)刪除表的字段

​ 使用了 alter 命令及 drop 子句來刪除以上建立表的字段,語法如下:

alter table 表名 drop 欄位名;
​ 例如:

//刪除result表中studentresult欄位

alter table result drop studentresult;

注:如果資料表中只剩餘乙個欄位則無法使用drop來刪除字段

​ (2)新增表的字段

​ 使用 add 子句來向資料表中新增列,語法如下:

alter table 表名 add 欄位名 資料型別[屬性]

;

​ 例如:

//向result表中新增studentresult欄位

alter table result add studentresult int not null;

​ (3)修改表名

​ 在 alter table 語句中使用 rename 子句來實現,語法如下:

alter table 舊表名 rename to 新錶名;
​ 例如:

alter table demo01 rename to demo02;
​ (4)修改表字段

​ 在alter命令中使用 change 子句 ,語法如下

alter table 表名 change 原欄位名 新欄位名 資料型別[屬性]

;

​ 例如:

alter table result change resultname01 resultname02 int not null;
​ (5)新增主外來鍵

​ 新增主鍵約束,語法如下:

alter table 表名 add constraint 主鍵名 primary key 表名(主鍵字段)

;

​ 例如:

//將grade表中的gradeid設定為主鍵

alter table grade add constraint pk_grade primary key grade

(gradeid)

;

​ 新增外來鍵約束,語法如下:

alter table 表名 add constraint 外鍵名 foreign key

(外來鍵字段) references 關聯表名(關聯字段)

;

​ 例如:

//設定student表的gradeid欄位與grade表的gradeid欄位建立主外來鍵關聯

alter table student add constraint fk_student_grade foreign key

(gradeid) references grade

(gradeid)

;

DDL 資料庫定義語言

建表 id name age drop talbe if esists student create table student id int primary keyauto increment,name varchar 20 not null,age int not null default 18...

資料庫定義語言DDL

sql是結構化查詢語句,sql是專門為資料庫而建立的操作命令集。是一種功能齊全的資料庫語言。在使用它時,只需要發出 做什麼 的命令,怎麼做 是不用使用者考慮的。ddl 資料定義語言 用來定義資料庫物件,建立庫 表 列等。dml 資料操作語言 用來運算元據庫表中的記錄 dql 資料查詢語言 用來查詢資...

DDL(資料庫定義語言)

ddl 資料庫定義語言 1.基本操作 檢視所有資料庫名稱 語法 show databases 切換資料庫 語法 use test 切換到test資料庫 顯示表 語法 show tables 查詢表 語法 select form goods 2.運算元據庫 2.1建立資料庫 create databs...