DDL(資料庫定義語言)

2021-10-05 06:32:59 字數 1984 閱讀 8856

1.1建立資料庫和建立表:

語法:create database if not exists 資料庫名;

create table if not exists 表名(

欄位名 字段型別 字段約束

#不存在就建立資料庫,存在就不建立資料庫

create database if not exists text;

#如果沒有 if not exists 則已存在text建立時就會報錯。

create database text;

#當有if not exists時存在同樣的表名,就不建立表

create table if not exists text(

text1 int(2) not null

)

1.1.1 表字段命名規則:

組成:英文單詞,數字,普通符號

見名知意

不分大小寫,以"_"區分,如:my_name;

已英文本母開頭

不能使用關鍵字

1.1.2 字段約束:

字段完整性:

​ 資料型別,預設值:default,非空:not null

實體完整性:

​ 主鍵:primary key

​ 自動增長:increment

​ 唯一鍵,索引

表完整性:

​ 外來鍵:foreign key

​ 語法:foreigm key 外來鍵表欄位名 references 主鍵表的列名

自定義完整性:

​ 事務,檢視,函式,儲存過程

1.1.3 字段屬性:

數值 整數:int ,bigint

小數:float,double,real,decimal

字元:

char:不可變字元,如果長度不夠,則以空格填充

varchar:長度不可變

text:存較大的

blob:大字段資料型別

日期:date,datetime,time,timestamp,now()

mysql不支援布林,所以用tinyint,1就為true,0就為false

1.2刪除資料庫與刪除表:

語法:drop database if exists 資料庫名;

drop table if exists 表名;

#存在就刪除,不存在就不刪除

drop database if exists text;

#如果沒有 if exists 則不存在text就會報錯。

drop table if exists text;

1.3 alter:

修改表名:alter table 舊表名 rename [to] 新錶名;

修改字段:alter table 表名 change [column] 原欄位名 新欄位名 資料型別 約束

​ alter table 表名 modify [column] 欄位名 資料型別 約束

新增字段:alter table 表名 add [column] 欄位名 資料型別 [屬性];

刪除字段:alter table 表名 drop 欄位名;

#修改表名:alter table 舊表名 rename [to] 新錶名;

alter table s1 rename s2;

#修改字段:alter table 表名 change [column] 原欄位名 新欄位名 資料型別;

alter table s2 change name myname char not null;

#新增字段:alter table 表名 add [column] 欄位名 資料型別 [屬性];

alter table s2 add name varchar not null;

#刪除字段:alter table 表名 drop 欄位名;

alter table s2 drop myname;

1.4 基本操作:

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...