赞
踩
主键:primary key用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个
示例:创建表的时候直接在字段上指定主键
mysql> create table t4 (
-> id int unsigned primary key comment '学号不能为空',
-> name varchar(20) not null);
mysql> desc t4;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | | # key中pri表示该字段是主键
| name | varchar(20) | NO | | NULL | |
+-------+------------------+------+-----+---------+-------+
主键约束:主键对应的字段中不能重复,一旦重复,操作失败
mysql> insert into t4 values(1, 'aaa');
mysql> insert into t4 values(1, 'aaa');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
当表创建好以后但是没有主键的时候,可以再次追加主键
alter table 表名 add primary key(字段列表)
删除主键
alter table 表名 drop primary key;
mysql> alter table t4 drop primary key;
mysql> desc t4;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+------------------+------+-----+---------+-------+
复合主键:在创建表的时候,在所有字段之后,使用primary key来创建主键,如果有多个字段作为主键,可以使用复合主键
mysql> create table t5( -> id int unsigned, -> course char(10) comment '课程代码', -> score tinyint unsigned default 60 comment '成绩', -> primary key(id, course) <= id和course为复合主键 -> ); mysql> desc t5; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | 0 | | <= 这两列合成主键 | course | char(10) | NO | PRI | | | | score | tinyint(3) unsigned | YES | | 60 | | +--------+---------------------+------+-----+---------+-------+ mysql> insert into t5 (id,course)values(1, '123'); mysql> insert into t5 (id,course)values(1, '123'); ERROR 1062 (23000): Duplicate entry '1-123' for key 'PRIMARY' <= 主键冲突
mysql> create table t6(
-> id int unsigned primary key auto_increment,
-> name varchar(10) not null default ''
-> );
mysql> insert into tt21(name) values('a');
mysql> insert into tt21(name) values('b');
mysql> select * from tt21;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
+----+------+
mysql > select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
mysql> create table student ( -> id char(10) unique comment '学号,不能重复,但可以为空', -> name varchar(10) -> ); mysql> insert into student(id, name) values('01', 'aaa'); mysql> insert into student(id, name) values('01', 'bbb'); <=唯一约束不能重复 ERROR 1062 (23000): Duplicate entry '01' for key 'id' mysql> insert into student(id, name) values(null, 'bbb'); -- 但可以为空 mysql> select * from student; +------+------+ | id | name | +------+------+ | 01 | aaa | | NULL | bbb | +------+------+
如下面示例,即便学生表通过class_id设置了和班级表的联系,但是不具备任何的约束力
class_id
只有外键之名(关联关系),没有外键之实(约束关系)
外键:
外键用于定义主表和从表之间的关系:
语法:
foreign key (字段名) references 主表(列)
示例:
如果将班级表中的数据都设计在每个学生表的后面,那就会出现数据冗余
对上面的示意图进行设计:
create table myclass (
id int primary key,
name varchar(30) not null comment'班级名'
);
create table stu (
id int primary key,
name varchar(30) not null comment '学生名',
class_id int,
foreign key (class_id) references myclass(id)
);
mysql> insert into myclass values(10, '高三(9)班'),(20, '高三(19)班');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into stu values(100, '张三', 10),(101, '李四',20);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into myclass values(10, '高三(9)班'),(20, '高三(19)班');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into stu values(100, '张三', 10),(101, '李四',20);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into stu values(102, 'wangwu', null);
-- 创建数据库 create database if not exists bit32mall default character set utf8 ; -- 选择数据库 use bit32mall; -- 创建数据库表 -- 商品 create table if not exists goods ( goods_id int primary key auto_increment comment '商品编号', goods_name varchar(32) not null comment '商品名称', unitprice int not null default 0 comment '单价,单位分', category varchar(12) comment '商品分类', provider varchar(64) not null comment '供应商名称' ); -- 客户 create table if not exists customer ( customer_id int primary key auto_increment comment '客户编号', name varchar(32) not null comment '客户姓名', address varchar(256) comment '客户地址', email varchar(64) unique key comment '电子邮箱', sex enum('男','女') not null comment '性别', card_id char(18) unique key comment '身份证' ); -- 购买 create table if not exists purchase ( order_id int primary key auto_increment comment '订单号', customer_id int comment '客户编号', goods_id int comment '商品编号', nums int default 0 comment '购买数量', foreign key (customer_id) references customer(customer_id), foreign key (goods_id) references goods(goods_id) );
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。