表的约束null/not null ; default ; comment ; zerofill ; primary key ; auto_increment ; unique key

真正约束字段的是数据类型,为保证数据合法性,需要添加额外约束: null/not null ; default ; comment ; zerofill ; primary key ; auto_increment ; unique key

空属性

  • 两个值 : null / not null
  • 数据库默认字段通常是字段为空,但是在应用过程过程中尽量保证字段不为空,因为数据为空没有办法参与运算

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

reate table de (name varchar(20) not null,age tinyint unsigned default 0,sex char(2) default '男');
Query OK, 0 rows affected (0.01 sec)

MariaDB [test1]> desc de;
+-------+---------------------+------+-----+---------+-------+
| 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.00 sec)

MariaDB [test1]> insert into de(name) values ('张三');
MariaDB [test1]> select *from de;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| 张三   |    0 ||
+--------+------+------+
1 row in set (0.00 sec)

列描述 comment
没有实际含义,只是用来描述

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

zerofill
对列添加zerofill属性,如果表中元素宽度小于设定的宽度,自动填充 0 ,这个结果只是显示出来的结果,在MySQL内部储存的还是实际值,这个可以通过hex()函数查看

select X,hex(X)  from tablename;

主键primary key
主键 : primary key用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表最多只能有一个主键, 主键对应的类通常是整数类型

  • 创建表的时候直接在字段上指定主键
create table pk ( id int unsigned primary key comment 'id不能为空', name varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test1]> desc pk;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | NULL    |       |
| name  | varchar(20)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
//PRI : 表示该字段是主键 
//在创建表的时候,在所有字段之后,在使用primary key(主键字段序列)来创建序列,如果有多个字段作为主键,可以使用复合主键.
  • 当表创建好之后可以追加主键
alter table tablename add primary key (字段列表);
  • 主键约束 : 主键对应的字段不能重复,一旦重复,操作失败

  • 删除主键

alter table tablename drop primary key;

MariaDB [test1]> alter table pk drop primary key;
MariaDB [test1]> desc pk;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   |     | NULL    |       |
| name  | varchar(20)      | NO   |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

自增长auto_increment
auto_increment : 当对应的字段, 不给值,会自动被系统触发,系统会从当前字段中已经有的最大值 + 1 操作,得到一个新的不同的值,经常和主键搭配使用,作为逻辑主键

自增长的特点

  • 任何一个字段要做自增长,前提是本身是一个索引(key 一栏有值)

  • 自增字段必须是整数

  • 一张表最多只有一个自增长

    索引 : 在关系数据库中,索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容。
    索引提供指向存储在表的指定列中的数据值的指针,然后根据您指定的排序顺序对这些指针排序。数据库使用索引以找到特定值,然后顺指针找到包含该值的行。这样可以使对应于表的SQL语句执行得更快,可快速访问数据库表中的特定信息。

唯一键
当表中需要除主键外还需字段唯一,可使用唯一键,即: 唯一键可解决表中多个字段需要唯一性约束

MariaDB [test1]> create table uk ( id char(20) unique comment 'id不能重复,但是可以为空',name varchar(
Query OK, 0 rows affected (0.00 sec)

MariaDB [test1]> insert into uk (id,name) values ('01','a');
Query OK, 1 row affected (0.01 sec)

MariaDB [test1]> insert into uk (id,name) values (null,'b');
Query OK, 1 row affected (0.01 sec)

MariaDB [test1]> insert into uk (id,name) values ('01','c');
ERROR 1062 (23000): Duplicate entry '01' for key 'id'
MariaDB [test1]> select * from uk;
+------+------+
| id   | name |
+------+------+
| 01   | a    |
| NULL | b    |
+------+------+
2 rows in set (0.00 sec)

外键
外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束。当定义外键后,要求外键列数据必须在主表的主键列存在或为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)
);
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值