python學習第三十九天:mysql表相關操作

來源:互聯網
上載者:User

標籤:fill   mysq   def   table   engine   null   dep   不能   blackhole   

表的完整性條件約束

約束條件與資料類型的寬度一樣,都是選擇性參數

作用:用於保證資料的完整性和一致性

主要分為:

not null  標識該欄位不可為空

default   為該欄位設定預設值

unsigned  無符號

zerofill   使用0填充

auto_increment  標識該欄位的自動成長(整數類型,而且為主鍵)

primary key   標識該欄位為該表的主鍵,可以唯一的標識記錄

foreign key   標識該欄位為該表的外鍵

unique key    標識該欄位的值是唯一的

 

一、not null與default

create table t1(

         id int primary key auto_increment,

         name varchar(16) not null,

         sex enum(‘nale’,’female’) not null default ‘male’

);

 

Insert into t1(name) values(‘egon’),(‘lxx’),(‘alex’);

 

二、unique key

create table t2(x int unique);

 

create table t3(

         x int,

         y varchar(5),

         unique key(x)

);

 

create table t4(

         x int,

         y varchar(5),

         constraint uni_x unique key(X)

);  # 給鍵取名

 

create table service(

         ip varchar(15),

         port int,

         unique key(ip,port)

);  # 聯合唯一

 

 

三、primary key

站在約束角度看primary key = not null unique

以後但凡建表,必須注意:

1、必須有且只有一個主鍵

2、通常是id欄位被設定為主鍵

create table t5(

         id int primary key auto_increment

);

 

# 補充儲存引擎:

create table t12(x int)engine=‘myisam‘;  #會建立三個檔案

create table t13(x int)engine=‘innodb‘;   #一般都用這個,建立兩個,主鍵與資料在一個表中

create table t14(x int)engine=‘memory‘;       #記憶體中,服務端關機消失

create table t15(x int)engine=‘blackhole‘;   #不會有任何資料資訊

 

四、foreign key:限制關聯表某一個欄位的值必是來自於被關聯表的一個欄位的

注意:

1.被關聯的欄位必須是一個key,通常是id欄位

2.建立表時:必須先建立被關聯的表,才能建立關聯表

3.插入記錄時:必須先往被關聯的表中插入記錄,才能往關聯表中插入記錄

4.刪除時:應該先刪除關聯表emp中的記錄,再刪除被關聯表對應的記錄

create table dep(

         id int primary key auto_increment,

         dname varchar(20),

         info varchar(50)

);

 

create table emp(

         id int primary key auto_increment,

         name varchar(15),

         age int,

         dep_id int,

         foreign key(dep_id) references dep(id)

         on update cascade

         on delete cascade

);

 

insert into dep(dname,info) values

         (‘IT’,’技術能力有限部門’),

         (‘sale’,’文化程度不高部門’),

         (‘HR’,’招不到人部門’);

 

 

 

insert into emp(name,age,dep_id) values

(‘egon‘,18,1),

(‘alex‘,28,2),

(‘wsj‘,38,2),

(‘lxx‘,30,1),

(‘xiaohou‘,18,3);

 

# 找兩張表的關係的竅門

emp        dep

#1、先站在左表的角度:去找左表emp的多條記錄能否對應右表dep的一條記錄

    翻譯:多個員工能否屬於一個部門

 

#2、然後站在右表的角度:去找右表dep的多條記錄能否對應左表emp的一條記錄

    翻譯:多個部門能否擁有同一名員工

 

# 多對一:結果的判斷

#1、如果只有單向的多對一成立,那麼最終的關係就是多對一

#2、在emp表新增一個欄位dep_id, 該欄位外部索引鍵關聯dep(id)

 

# 多對多:結果的判斷

#1、雙向的多對一就是多對多

#2、需要建立第三張表,有一個欄位值fk左表,一個欄位的值fk右表

 

create table author(

         id int primary key auto_increment,

         name varchar(16),

         age int

);

 

create table book(

         id int primary key auto_increment,

         bname varchar(20),

         price int

);

 

create table author2book(

         id int primary key auto_increment,

         author_id int,

         book_id int,

         foreign key(author_id) references author(id) on update cascade on delete cascade,

         foreign key(book_id) references book(id) on update cascade on delete cascade

);

# 一對一:無需參考竅門,左表的一條資料唯一對應右表的一條記錄

fk+unique

python學習第三十九天:mysql表相關操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.