这是有声音的视频,请检查播放器或者声音输出设备。

这次的学习内容:主键primary key,外键foreign key

==============================

create database xue_xiao character set utf8 collate utf8_general_ci;

use xue_xiao;

create table xue_sheng(id int, xing_ming varchar(50), fen_shu int, xing_bie char(2), bj_id int);

insert into xue_sheng values(1,'张三',90,'男',1);

insert into xue_sheng values(2,'李四',70,'男',2);

insert into xue_sheng values(3,'李小红',80,'女',1);

insert into xue_sheng values(4,'陈小明',80,'男',3);

create table ban_ji( id int, ban_ming varchar(15) );

insert into ban_ji values(1,'一年级(1)班');

insert into ban_ji values(2,'一年级(2)班');

insert into ban_ji values(3,'一年级(3)班');

====================================

什么是主键? 为什么需要设置一个主键

作为表的行的唯一标识的候选关键字。一张表只有一个主键。主键可以由一个字段,也可以由多个字段组成。

主键的值不能重复,也不可以为空NULL

其实主键也是一种约束, 一种索引, 当建立了索引,在复杂多数据查询中,提高查询速度。

==========================================

没有设置主键的时候,可以重复插入数据

insert into xue_sheng values(1,'测试',50,'男',1);

当要设置的主键数据已经存在重复的时候,那么设置主键失败。

mysql> alter table xue_sheng add constraint pk_xue_sheng primary key( id );
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

要删除重复数据后,才能建立主键。
delete from xue_sheng where fen_shu=50;

查看表的结构
desc xue_sheng;

insert into xue_sheng values(5,'测试',50,'男',1);

删除主键
alter table xue_sheng drop primary key;

------------------------------------
也可以在初始化表的时候,建立一个自动增长的id 作为主键(比较常见的做法)。

create table xue_sheng(
id int(20) auto_increment not null primary key,
xing_ming varchar(50),
fen_shu int,
xing_bie char(2),
bj_id int
);

insert into xue_sheng(xing_ming,fen_shu,xing_bie,bj_id) values('张三',90,'男',1);

一次过插入多条数据,注意括号的写法和字段的数目保持一致

insert into xue_sheng(xing_ming,fen_shu,xing_bie,bj_id)
values('李四',70,'男',2),('李小红',80,'女',1),('陈小明',80,'男',3);

删除自增长的主键id ,要注意的问题

mysql> alter table xue_sheng drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

1,先要删除自增长
alter table xue_sheng change id id int(20);
2,然后才能删除主键
alter table xue_sheng drop primary key;

======================================================
外键, 数据参照的完整性, 也是一种约束

一张表的外键必须是另外一张表的主键
alter table xue_sheng add constraint fk_xue_sheng foreign key(bj_id) references ban_ji(id);
ERROR 1005 (HY000): Can't create table 'xue_xiao.#sql-b26_1' (errno: 150)
mysql>

alter table ban_ji add constraint pk_ban_ji primary key( id );
alter table xue_sheng add constraint fk_xue_sheng foreign key(bj_id) references ban_ji(id);

创建外键后, 在学生表添加数据的bj_id 必须要参考班级表的主键id

受到外键约束,不能添加
insert into xue_sheng(xing_ming,fen_shu,xing_bie,bj_id) values('张三',90,'男',4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`xue_xiao`.`xue_sheng`, CONSTRAINT `fk_xue_sheng` FOREIGN KEY (`bj_id`) REFERENCES `ban_ji` (`id`))

insert into xue_sheng(xing_ming,fen_shu,xing_bie,bj_id) values('张三',90,'男',3);

删除 班级表的数据同样受到限制

delete from ban_ji where id=3;

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`xue_xiao`.`xue_sheng`, CONSTRAINT `fk_xue_sheng` FOREIGN KEY (`bj_id`) REFERENCES `ban_ji` (`id`))

删除外键
alter table xue_sheng drop foreign key fk_xue_sheng;

=======================================================

check 约束

alter table xue_sheng add constraint ck_xue_sheng check( xing_bie='男' or xing_bie='女' );

alter table xue_sheng add constraint ck_xue_sheng check( xing_bie in('男','女' );

===============================================
not null 非空

alter table xue_sheng change xing_ming xing_ming varchar(50) not null;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| xing_ming | varchar(50) | NO   |     | NULL    |       |

alter table xue_sheng change xing_bie xing_bie char(2) default '男' not null;
| Field     | Type        | Null | Key | Default | Extra |
| xing_bie  | char(2)     | NO   |     | 男      |       |

insert into xue_sheng(xing_ming,fen_shu,bj_id) values('',90,3);

insert into xue_sheng(xing_ming,fen_shu,bj_id) values(null,90,3);
ERROR 1048 (23000): Column 'xing_ming' cannot be null

mysql> show create table xue_sheng;

CREATE TABLE `xue_sheng` (
`id` int(20) NOT NULL DEFAULT '0',
`xing_ming` varchar(50) NOT NULL,
`fen_shu` int(11) DEFAULT NULL,
`xing_bie` char(2) NOT NULL DEFAULT '男',
`bj_id` int(11) DEFAULT NULL,
KEY `fk_xue_sheng` (`bj_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

演示就到这里, 88


“mysql_5.5.8_教程_06 主键primary key,外键foreign key,其他约束check,not null”有6个评论

  1. 01月 16th, 2011 at 11:58:54 #pigzhu

    讲的很好,解决了我以前很多的疑问,ths!

    [回复]

  2. 03月 18th, 2011 at 14:56:31 #孕妇感冒

    兔年吉祥~

    [回复]

  3. 04月 21st, 2011 at 11:37:58 #nameless

    非常感谢

    [回复]

  4. 01月 13th, 2012 at 04:11:39 #Buffy

    Good to find an expert who knows what he's tlaknig about!

    [回复]

  5. 01月 13th, 2012 at 18:55:48 #ohhcpex

    UwO6UD wndbcgwnoqaq

    [回复]

  6. 01月 15th, 2012 at 20:57:06 #xicxip

    83uItJ qrqabgzyddjx

    [回复]

有任何疑问或建议,可以给作者留言:



公告:

  • 2010年5月之前的视频是文字解说演示,没有声音。
  • 2010年5月以后的视频全部带声音。