MySQL 空-默认值-主键

来源:互联网 发布:mac launchpad 快捷键 编辑:程序博客网 时间:2024/06/05 20:48

列属性

 

null:

是否为空,规定一个字段的值,是否可以是NULL .

null | not null

 

空字符串不是null

 

default value

默认值属性。

 

primary key:

主键,可以唯一标识某条纪律的字段或字段的集合。

 

主键可以是真实实体的属性.但是常用的比较好的方案采用的是

一个与实体信息不相关的属性作为唯一标识.

因此主键与业务逻辑不发生关系,只是用来标识记录。

 

设置主键的两种语法:

1.字段上设置。

主键不能重复,也不能为空。

2.另起一行定义。

这种方式可以创建组合主键。

这种创建的含义:

一个主键包含多个字段,而不是多个字段都是主键

 

create database cs7;use cs7;create table t1(a int not null,b int);insert into t1 (a) values (5);select * from t1;insert into t1 (b) values (5);    /*插入失败:显示a没有默认值*/select * from t1;create table t2 (a int not null default 10,b int not null default 50);insert into t2 (a) values (5);select * from t2;insert into t2 (b) values (5);select * from t2;insert into t2 values (null,null);  /*无法插入:显示a不能为null*/insert into t2 values ();           /*可以这样直接插入默认值。*/select * from t2;create table t3(a int default 10);insert into t3 values();select * from t3;                   /*a优先显示默认值而不是null*/insert into t3 values(null);select * from t3;                   /*可以显示null.*/create table teacher(t_id int primary key,t_name varchar(5),class_name varchar(6),days tinyint unsigned);insert into teacher values (1,'韩A','0331',25);/*插入失败,需要设置字符集*/set names gbk;insert into teacher values (1,'韩A','0331',25);insert into teacher values (1,'李A','0228',24);/*插入失败,主键必须唯一,显示:Duplicate entry '1' for key 'PRIMARY'*/insert into teacher values (null,'李A','0228',24);/*显示主键不能为空*/desc teacher;    /*可以查看主键设置*//*另一种设置主键的方法*/create table teacher1(t_id int,t_name varchar(5),class_name varchar(6),days tinyint unsigned,primary key(t_id));insert into teacher1 values (1,'韩A','0331',25);desc teacher1; /*组合主键*/create table teacher2(t_name varchar(5),class_name varchar(6),days tinyint unsigned,primary key(t_name,class_name)    );


0 0
原创粉丝点击