mysql- constraint Overview

constraint

What are the constraints (constraint)

Constraint is a restriction,

Database constraints, is security, to ensure the integrity of the data;

The constraints mysql

unique (unique)

The only constraint, indicating that the field can not appear duplicate values, that uniquely identifies a record

Mainly used for: ID, Student ID, etc.

not null (non-empty)

Non-null constraint indicates that the value of the field can not be null

Mainly used for: account name / password

null

Some data types that can be empty by default

default

The default value for a field to set a default value

Ordinary constraint example:

# 完整的建表语句
create table 表名(字段名 字段类型[(宽度)约束]) charset utf8;

# 学生类(类型具备:学号/姓名/性别)
create table student(
    id int unique, 
    name char(10) not null, 
    gender enum("women","man") default "man"
);
insert into student values(null,null,null);  # 因为name约束了不能为空,所以会报错
ERROR 1048 (23000): Column 'name' cannot be null  

insert into student values(null,"jack",null); # 给name加入值,就可以了,但是我们的学号和性别都是null,这                                               # 样不行,虽然性别设置了默认值,但因为传入了null,系统默认                                                 有值
Query OK, 1 row affected (0.38 sec)
+------+------+--------+
| id   | name | gender |
+------+------+--------+
| NULL | jack | NULL   |
+------+------+--------+
1 row in set (0.00 sec)  
alter table student modify id int  unique not null;
# 如果要确保学号和性别都不能为空,那我们也要加非空,非常麻烦,所以出现了一个即表示,唯一性又是非空的约束条件,primary key

primary key

Overview:

Primary key constraint, the constraint is equal to the angle of view: the only non-null +

The primary role of bonds:

In innodb storage engine, the primary key for organizing data (tree structure), that is the primary key for the engine is innodb must no not.

If no primary key is specified manually, mysql will automatically find a unique field includes a non-null primary key and;

If there is no such field, mysql will create a means of concealment, as the primary key;

Is a primary key index, unique also, the role of the index is to speed up queries

Primary key: have the constraints of the role, but also speed up our query speed

If there is non-empty and the only original field of business, then set up the primary key, if not add their own specialized field as a primary key;

Usually we'll set the primary key of type int, in order to facilitate a guaranteed unique.

# auto_increment 表示主键自动增长字段,可以给null,会自动生成值
# 自动增长可以用在 具备索引,并且是数字类型的字段上,但是通常与主键一起使用!
create table student1(id int primary key auto_increment, name char(10) not null);

insert into student1 values (null,"jack");  
Query OK, 1 row affected (0.39 sec)

mysql> select * from student1;
+----+------+
| id | name |
+----+------+
|  1 | jack |
+----+------+
1 row in set (0.00 sec)

The difference between the ordinary and the primary key constraint

create table person(
    id char(19) primary key,
    name char(20)
);

insert into person values("1","rose");# ok 
insert into person values("1","jack");# ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'主键冲突;
insert into person values("2","jack"); # ok

#从约束角度来看就等同于,非空+唯一  
create table person2(
    id char(19) unique not noll,
    name char(20)

Guess you like

Origin www.cnblogs.com/raynduan/p/11444672.html