表的约束

真正约束字段的是数据类型,但是数据类型的约束较单一,需要额外的约束,来保证数据的合法性
这里介绍如下几个:null/not null ,default,comment,zerofill,primary key,auto_increment,unique key

1 空属性

  1. 两个值:null(空属性),not null(不为空)
  2. 数据库默认字段基本都是为空

案例:
假如:有个班级表,表中存放班级的名称,如果班级的名称可以为空,那我们就不知道有哪些班级,若教室名字可以为空,那我们就不知道要去那个教室上课

 mysql>create table myclass(
         ->class_name varchar(20) not null,
         -> class_room varchar(10) not null);
Query OK, 0 rows affected (0.87 sec)
查询表结构:
mysql> desc myclass;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_name | varchar(20) | NO   |     | NULL    |       |
| class_room | varchar(10) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.21 sec)

//插入数据,若不给教室数据插值就会失败

mysql> insert into myclass(class_name) values('class1');
ERROR 1364 (HY000): Field 'class_room' doesn't have a default value

2.默认值

默认值:某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值

create table tt10 (
 -> name varchar(20) not null, 
 -> age tinyint unsigned default 0, 
 -> sex char(2) default '男'  );
mysql> desc tt10;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name  | varchar(20)         | NO   |     | NULL    |       |
| age   | tinyint(3) unsigned | YES  |     | 0       |       |
| sex   | char(2)             | YES  |     | 男      |       |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.06 sec)

有默认值时,在插入数据时若没有给该字段赋值,就使用默认值

mysql> insert into tt10(name) values('zhangsan');
Query OK, 1 row affected (0.09 sec)

mysql> select * from tt10;
+----------+------+------+
| name     | age  | sex  |
+----------+------+------+
| zhangsan |    0 | 男   |
+----------+------+------+

3.列描述

comment:没有实际的含义,跟其他编译器中的注解类似,告诉别人这个字段是什么

mysql> create table tt12 ( 
         -> name varchar(20) not null comment '姓名', 
         -> age tinyint unsigned default 0 comment '年龄', 
         -> sex char(2) default '男' comment '性别' );

通过desc查看不到注释信息
通过show可以看到:

mysql> show create table tt12\G;
*************************** 1. row ***************************
       Table: tt12
Create Table: CREATE TABLE `tt12` (
  `name` varchar(20) NOT NULL COMMENT '姓名',
  `age` tinyint(3) unsigned DEFAULT '0' COMMENT '年龄',
  `sex` char(2) DEFAULT '男' COMMENT '性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

4.zerofill

mysql> show create table users\G;
*************************** 1. row ***************************
       Table: users
 Create Table: CREATE TABLE `tt3` ( 
      `a` int(10) unsigned DEFAULT NULL, 
      `b` int(10) unsigned DEFAULT NULL 
 ) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

可以看到int(10),其实没有zerofill这个属性,括号内的数字是没有意义的

mysql> select * from tt3;
 +------+------+ 
 | a    | b    | 
 +------+------+ 
 | 1    | 2    | 
 +------+------+

当我们给列添加zerofill属性后

mysql> alter table tt3 change a a int(5) unsigned zerofill;
mysql> select * from tt3; 
+-------+------+ 
| a     | b    | 
+-------+------+ 
| 00001 | 2    | 
+-------+------+

其实MySQL中实际存的还是1,我们可以用hex函数来证明

mysql> select a, hex(a) from tt3; 
+-------+--------+ 
| a     | hex(a) | 
+-------+--------+ 
| 00001 | 1      | 
+-------+--------+

可以看出zerofill就是一种格式化输出

5.主键

主键:primary key用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个主键;主键所在的列通常是整数类型。
创建主键的三种方式:
5.1 创建表的时候直接在字段上指定主键

mysql> create table tt13 (
        -> id int unsigned primary key comment '学号不能为空', 
        -> name varchar(20) not null); 
        Query OK, 0 rows affected (0.00 sec) 

mysql> desc tt13; 
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra | 
 +-------+------------------+------+-----+---------+-------+ 
 | id    | int(10) unsigned | NO   | PRI | NULL    |       | <= key 中 pri表示该字段是主键
 | name  | varchar(20)      | NO   |     | NULL    |       | 
 +-------+------------------+------+-----+---------+-------+

5.2创建复合主键:

ysql> create table tt14( 
     -> id int unsigned, 
     -> course char(10) comment '课程代码', 
     -> score tinyint unsigned default 60 comment '成绩', 
     -> primary key(id, course) -- id和course为复合主键

5.3当表创好了,可以再追加主键

alter table 表名 add primary key(字段列表)

主键对应的字段不能重复,一定能重复,操作失败
5.4删除主键:

alter table 表名 drop primary key;

6.自增长

自增长的特点:

  • 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
  • 自增长字段必须是整数
  • 一张表最多只能有一个自增长
    案例:
mysql> create table tt21( 
        -> 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    | 
 +----+------+

在插入后获取上次插入的值,批量插入获取的是第一个值

7.唯一键

一张表中有很多字段需要唯一性,但是一张表中只能由一个主键,唯一键就可以解决这个问题
唯一键可以为空,而且可以多个为空,空字段不做唯一性比较

mysql> create table student ( 
        -> id char(10) unique comment '学号,不能重复,但可以为空',
         -> name varchar(10) -> ); 
Query OK, 0 rows affected (0.01 sec)

8.外键

外键用于定义表与表之间的关系,外键约束主要定义在从表上,主表则必须是主键或唯一键约束,当定义外键后,要求外键列数据必须在主键列存在或为null
语法:

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, 'C++大牛班'),(20, 'java大神班'); 
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

若插入一个班级为30的学生,因为从表中没有30班,所以插入失败

mysql> insert into stu values(102, 'wangwu',30); ERROR 1452 (23000): 
Cannot add or update a child row: 
a foreign key constraint fails (mytest.stu, CONSTRAINT stu_ibfk_1 FOREIGN KEY (class_id) REFERENCES myclass (id))

新来的同学没有分班则class_id 就为null

mysql> insert into stu values(102, 'wangwu', null);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值