mysql建表


MySQL常見的建表選項及約束:

1、create table選項

  1、指定列選項:default、comment

  2、指定表選項:engine、auto_increment、comment

2、create table約束 

  1、not null:非空約束

  2、unique:唯一約束

  3、primary key:主鍵約束

  4、foreign key:外鍵

  5、enum、set:枚舉

 

DEFAULT:定義列的默認值

  當插入一個新行到表中並且沒有給該列明確賦值時,如果定義了列的默認值,將自動得到默認值 ;如果沒有,則為null。

create table student(id int, name char(10) default '小明') #建學生表里面有字段id,name,name的默認值為小明

   函數default(column)可以得到一個列的默認值:

select defalut(列名) from

 

COMMENT:給列添加注釋

mysql> create table student(id int comment '學號',name char(10) comment '名字');

#查看注釋
mysql> select column_name,column_comment from information_schema.columns where table_name = 'student';

 

 

NOT NULL:非空約束,指定某列不能為空

  作用:用於確保當前列的值不為空。

create table student(id int not null,name char(10));

 

PRIMARY KEY:主鍵約束

  primary key = not null + unique

  主鍵:用來唯一的標示表中的每一行(類型一般為整型或者字符串)

  具有主鍵約束的列不允許有null值,並且不允許有重復值;

  每個表最多只允許一個主鍵(可定義聯合主鍵),主鍵名總是PRIMARY。

create table student(id int not null primary key);

 

UNIQUE:唯一約束,指定某列和幾列組合的數據不能重復

  1.唯一約束是指定table的列或列組合不能重復,保證數據的唯一性,約束的列不允許有重復值;

  2.唯一約束不允許出現重復的值,但是可以為多個null;

  3.同一個表可以有多個唯一約束,多個列組合的約束

  

create table student(id int, name char(10)unique);

insert into student values(1,'a');
insert into student values(2,null);
insert into student values(3,null);

不可以插入重復值但是可以插入多個null

 

auto_increment:自增

create table student (id int not null primary key auto_increment, name char(10)); #創建學生表主鍵id自增
注意:只有主鍵才能設置自增

 

foreign key外鍵約束

外鍵約束:

  參照完整性約束,保證一個或兩個表之間的參照完整性,外鍵是構建於一個表的兩個字段或是兩個表的兩個字段之間的參照關系。

注意:

  1)具有外鍵約束的列的值不能隨便給,必須滿足外鍵所引用的主鍵的取值;

  2)一張表中可以定義多個外鍵;

  3)外鍵列默認可以給null值。

按照定義,外鍵必須引用一個主鍵或者唯一鍵,引用的主鍵一般在另外一張表中,也可以是本表的主鍵(后者稱為“自引用”)。

父子表:

  外鍵所在的表叫做子表、從表

  外鍵所引用的主鍵所在的表叫做父表、主表

示例:創建外鍵約束

父表學生表:
create table student (
id int not null primary key,
name char(10)
);

子表成績表:
create table score (
id int not null primary key,
grades int, 
student_id int,
foreign key (student_id) references student(id) on delete cascade on update cascade
);

數據:
insert into student values (1,'張三');
insert into student values (2,'李四');
insert into student values (3,'王五');
insert into score values(1,60,1);
insert into score values(2,50,3);
insert into score values(3,60,2);

ON DELETE/ ON UPDATE:級聯更新 級聯刪除接在外鍵后面
級聯更新:更新父表的數據子表也會更新
級聯刪除: 刪除副標的數據子表也會刪除
公式:
create 父表(
id int not null primary key,
name char(10)
)

create 子表(
id int not null primary key,
score int,
#外鍵名 數據類型,
foreign key (外鍵名) references 主表(關聯的外鍵一般為主鍵)
);

注意:刪表必須先刪子表,且外鍵只支持innodb存儲引擎

 

 

enum、set

兩者的區別是:

  使用ENUM,只能選一個值;

  使用SET,可以選多個值;

create table student(
id int not null primary key,
name enum('小紅','張三','李四')
);
insert into student values(1,'小紅')
等同
insert into student values(1,1)
create table student( id int not null primary key, name set('小紅','張三','李四') );
insert into student values(1,'小紅,李四');
insert into student values(1,'小紅');




 

練習

1.
  建表員工表字段有(id,name,age,sex)id為主鍵且不能為空 name唯一約束 sex為enum類型 age為int類型並不為空 2.
  建工資表,字段有(id,wage)並有一個外鍵與員工表關聯,員工表數據的更新刪除能影響到工資表

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM