--------------数据类型-------------
1.数值型
整数:
tinyint(-128 - 127)
tinyint unsinged (0-255)
int(32),bigint(64);
浮点数:
float(M,N);#精度7位
decimal(m,n);#精度64位,M表示总长度,N表示小数位数
decimal(m,n)unsigned;#负数去掉 正数不变

2.文本
char(L):#定长字符串 255最大的插入长度 中文一个也算一个字符
varchar(M):#不定长字符串(最大65535) 其中1-3个字节描述字符长度
text :#大文本

定长字符串检索效率高,空间利用率低;
变长字符串检索效率低,空间利用率高。

3.时间类型
DATE:日期类型  yyyy-mm-dd:年月日 3字节
DATETIME:日期时间类型 yyyy-mm-dd HH:mm:ss 年月日 时分秒 8字节
TIMESTAMP:时间戳 yyyy-mm-dd HH:mm:ss 4字节 #每当改行有数据有修改时,显示最新更改时间

create table tt2(
	d1 DATE,
	d2 DATETIME,
	d3 TIMESTAMP
);

ENUM:表示单选
enum('选项1','选项2','选项3'...);

SET:表示多选
set('选项1','选项2','选项3'...);

create table tt3(
	username varchar(20),
	hobby set('看电视','打游戏','写代码','逛街','睡觉'),
	sex enum('男','女')
);

insert into tt3 values('张大','打游戏,睡觉,写代码','男');
insert into tt3 values('王二','看电视,逛街,睡觉','女');
insert into tt3 values('刘三','打游戏,写代码','男');

desc tt3; #查看tt3属性
select * from tt3;#查看tt3里边数据
select * from tt3 where sex='女';
****要检索出set集合中指定的选项,find_in_set('要查询的选项',set集合)
select * from tt3 where find_in_set('打游戏',hobby);
表的约束:保证数据合法性
1.空属性(NULL)
尽量保证定义表时,字段不为空(NULL),数据为空无法参与运算
定义字段后 not null

create table tt4(
	id int(10) not null,
	name varchar(20)
);

insert into tt4(id) values(1);
insert into tt4 values(2,'wy');
select * from tt4;

insert into tt4(name) values('ll'); #错误

2.默认值 default
某一列经常出现某一个具体的值,可以在定义表结构时就指定默认值
create table tt5(
	id int not null,
	name varchar(10),
	sex char(1) default '男' not null
);

3.列描述 comment
没有实际含义,用来描述字段,会根据表创建语句保存
create table tt6(
	id int not null comment '描述员工id',
	name varchar(10) not null comment '员工姓名',
	sex char(1) default '男' not null
);

show create table tt6;#comment描述

4.zerofill(自动补0操作)
create table tt7(
	id int(4) zerofill,
	count int(5)
);
insert into tt7 values(88,88);#未满4位补0 够4位 都一样id 0088 count 88
select * from tt7;

5.主键 primary key
主键:不能重复,不能为空,一张表有且只有一个主键。
主键所在的列是整数类型

create table tt8(
	id int primary key,
	name varchar(10) not null
);

insert into tt8 values(1,'张三'),(2,'李四');

6.复合主键(多个属性作为主键)
create table tt9(
	id int not null,
	username varchar(6),
	password varchar(10),
	primary key(username,password) comment '复合主键'
);

insert into tt9 values(1,'wy','123');
insert into tt9 values(2,'wy','456');
#只要复合主键不重复就可以插入
select * from tt9 where password='123';#可查询

定义表之后追加主键
alter table tb_name add primary key(字段列表);#带括号
alter table tt7 add primary key (id);

删除主键 
alter table tb_name drop primary key;
alter table tt9 drop primary key;

7.自增长 auto_increment(最大值 即使删除了也会记录最大值)
向表中插入数据时若不插入此列数据,系统会自动触发,将当前字段最大值加1写入
一般和主键搭配使用,作为逻辑主键

自增长的特点:
a.任何一个字段要做自增长,前提本身是一个索引(该列有值,不能为空)
b.自增长必须为一个整数
c.有且只有一个自增长字段

create table tt10(
	StudentId int(5) zerofill primary key auto_increment ,
	StudentName varchar(20) not null comment '学生姓名',
	Password char(10) not null default '123456'
);
insert into tt10 values(1,'张三','123');
insert into tt10(StudentName,Password) values('lisi','23443');
insert into tt10(StudentId,StudentName) values(1024,'王五');
insert into tt10(StudentId,StudentName) values(1024,'赵六');#错误 id(z主键)重复
insert into tt10(StudentName) values('田七'); #编号为1025 类似于C++枚举

delete from tt10 where StudentId=1025; #删除该行

8.唯一约束 unique
在一张表中可以有多个字段设置唯一键,唯一键允许为空,空值不作为比较

create table tt11(
	id int primary key,
	name varchar(10) unique
);

insert into tt11 values(1,'aaaa');
insert into tt11 values(2,'bbbb');
insert into tt11 values(3,'aaaa');#错误  唯一键不能相等
insert into tt11 values(3,null);
insert into tt11 values(4,null);
insert into tt11 values(5,'cccc');

9.外键
外键主要定义主表和从表的关系,外键约束定义在从表之上,关联主表的列必须是主键约束或者是唯一键约束。
要求外键所在的列数据必须在主表中存在或者为null。
--主表
create table class(
	id int primary key,
	name varchar(20) not null
);
--从表
create table group1(
	group_id int primary key comment '主键描述group_id',
	group_name varchar(10) not null,
	class_id int,
	foreign key(class_id) references class(id)
);

insert into class values(1,'first'),(2,'second');
insert into group1 values(11,'lisi',1);
insert into group1 values(42,'ldh',4);#错误 class不存在id 4
insert into group1 values(12,'lisi',null);

你可能感兴趣的:(数据库)