sql筆記-2

四.約束

1.創建表時指定約束:

create table tb(

id int primary key auto_increment,

name varchar(20) unique not null,

ref_id int,

foreign key(ref_id) references tb2(id)

);

create table tb2(

id int primary key auto_increment

);

2.外鍵約束:

(1)增加外鍵:

可以明確指定外鍵的名稱,如果不指定外鍵的名稱,mysql會自動為你創建一個外鍵名稱。

RESTRICT : 只要本表格里面有指向主表的數據, 在主表里面就無法刪除相關記錄。

CASCADE : 如果在foreign key 所指向的那個表里面刪除一條記錄,那么在此表里面的跟那個key一樣的所有記錄都會一同刪掉。

alter table book add [constraint FK_BOOK] foreign key(pubid) references pub_com(id) [on delete restrict] [on update restrict];

(2)刪除外鍵

alter table 表名 drop foreign key 外鍵(區分大小寫,外鍵名可以desc 表名查看);

3.主鍵約束:

(1)增加主鍵(自動增長,只有主鍵可以自動增長)

Alter table tb add primary key(id) [auto_increment];

(2)刪除主鍵

alter table 表名 drop primary key

(3)增加自動增長

Alter table employee modify id int auto_increment;

(4)刪除自動增長

Alter table tb modify id int;

五.多表設計

一對一(311教室和20130405班級,兩方都是一):在任意一方保存另一方的主鍵

一對多、多對一(班級和學生,其中班級為1,學生為多):在多的一方保存一的一方的主鍵

多對多(教師和學生,兩方都是多):使用中間表,保存對應關系

六.多表查詢

create table tb (id int primary key,name varchar(20) );

create table ta (

id int primary key,

name varchar(20),

tb_id int

);

insert into tb values(1,'財務部');

insert into tb values(2,'人事部');

insert into tb values(3,'科技部');

insert into ta values (1,'劉備',1);

insert into ta values (2,'關羽',2);

insert into ta values (3,'張飛',3);

mysql> select * from ta;

+----+------+-------+

| id | name | tb_id |

+----+------+-------+

|? 1 | aaa? |? ? 1 |

|? 2 | bbb? |? ? 2 |

|? 3 | bbb? |? ? 4 |

+----+------+-------+

mysql> select * from tb;

+----+------+

| id | name |

+----+------+

|? 1 | xxx? |

|? 2 | yyy? |

|? 3 | yyy? |

+----+------+

1.笛卡爾積查詢:

兩張表中一條一條對應的記錄,m條記錄和n條記錄查詢,最后得到m*n條記錄,其中很多錯誤數據

select * from ta ,tb;

mysql> select * from ta ,tb;

+----+------+-------+----+------+

| id | name | tb_id | id | name |

+----+------+-------+----+------+

|? 1 | aaa? |? ? 1 |? 1 | xxx? |

|? 2 | bbb? |? ? 2 |? 1 | xxx? |

|? 3 | bbb? |? ? 4 |? 1 | xxx? |

|? 1 | aaa? |? ? 1 |? 2 | yyy? |

|? 2 | bbb? |? ? 2 |? 2 | yyy? |

|? 3 | bbb? |? ? 4 |? 2 | yyy? |

|? 1 | aaa? |? ? 1 |? 3 | yyy? |

|? 2 | bbb? |? ? 2 |? 3 | yyy? |

|? 3 | bbb? |? ? 4 |? 3 | yyy? |

+----+------+-------+----+------+

2.內連接:

查詢兩張表中都有的關聯數據,相當于利用條件從笛卡爾積結果中篩選出了正確的結果。

select * from ta ,tb where ta.tb_id = tb.id;

select * from ta inner join tb on ta.tb_id = tb.id;

mysql> select * from ta inner join tb on ta.tb_id = tb.id;

+----+------+-------+----+------+

| id | name | tb_id | id | name |

+----+------+-------+----+------+

|? 1 | aaa? |? ? 1 |? 1 | xxx? |

|? 2 | bbb? |? ? 2 |? 2 | yyy? |

+----+------+-------+----+------+

3.外連接

(1)左外連接:在內連接的基礎上增加左邊有右邊沒有的結果

select * from ta left join tb on ta.tb_id = tb.id;

mysql> select * from ta left join tb on ta.tb_id = tb.id;

+----+------+-------+------+------+

| id | name | tb_id | id? | name |

+----+------+-------+------+------+

|? 1 | aaa? |? ? 1 |? ? 1 | xxx? |

|? 2 | bbb? |? ? 2 |? ? 2 | yyy? |

|? 3 | bbb? |? ? 4 | NULL | NULL |

+----+------+-------+------+------+

(2)右外連接:在內連接的基礎上增加右邊有左邊沒有的結果

select * from ta right join tb on ta.tb_id = tb.id;

mysql> select * from ta right join tb on ta.tb_id = tb.id;

+------+------+-------+----+------+

| id? | name | tb_id | id | name |

+------+------+-------+----+------+

|? ? 1 | aaa? |? ? 1 |? 1 | xxx? |

|? ? 2 | bbb? |? ? 2 |? 2 | yyy? |

| NULL | NULL |? NULL |? 3 | yyy? |

+------+------+-------+----+------+

(3)全外連接:在內連接的基礎上增加左邊有右邊沒有的和右邊有左邊沒有的結果

select * from ta full join tb on ta.tb_id = tb.id; --mysql不支持全外連接

select * from ta left join tb on ta.tb_id = tb.id

union

select * from ta right join tb on ta.tb_id = tb.id;

mysql> select * from ta left join tb on ta.tb_id = tb.id

-> union

-> select * from ta right join tb on ta.tb_id = tb.id; --mysql可以使用此種方式間接實現全外連接

+------+------+-------+------+------+

| id? | name | tb_id | id? | name |

+------+------+-------+------+------+

|? ? 1 | aaa? |? ? 1 |? ? 1 | xxx? |

|? ? 2 | bbb? |? ? 2 |? ? 2 | yyy? |

|? ? 3 | bbb? |? ? 4 | NULL | NULL |

| NULL | NULL |? NULL |? ? 3 | yyy? |

+------+------+-------+------+------+

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容