MySQL 数据类型和约束(外键是重点🙄)

数据类型

1. 数字(默认都是由符号,宽度表示的是显示宽度,与存储无关).tinyint 括号里指定宽度

七位2进制数最大数就是2**7 -1=127 最小是-128

验证:

create tabel t1(id tinyint)

create tabe t2(id int)

浮点数

float

double

decimal

测试

create table t2(salary float)

float(6,2) 第一参数,代表宽度. 第二个参数代表小数点后面的位数.

2.字符串类型

char 与 varchar

char 类型:定长,浪费空间,存取速度快

不够就用空格补,
字符长度范围: 0 ~ 255

create table t6(name char(4));

insert into t6(‘aigen‘)#存不进去

insert into t6(‘能不能存‘)  #能存进去

####varchar:变长,精准,节省空间,存取速度慢
字符长度范围:0 ~ 65535

**char 和 varchar 区别:  char(5), 存不够5个,用空格补齐.     varhar(5), 你是几个它就存几个.**

char_length : 查看的是字符的长度
‘abc‘和‘你好啊‘ 都是三个字符.

但是前面是3个字节,后面 char(5)的length话,是11个字节.

length : 查看字节.

一个英文字符算一个 bytes

一个中文字符算3个 bytes

insert into t7 values(‘abc‘,‘abc ‘)

select * from t6 where y= ‘abc   ‘

3.日期类型

create table student(
id int,
name char(5),
born_date date,#‘2017-09-06‘,
born_year year, #‘2017‘,
res_time datetime, #‘2017-09-06 10:53:23‘
class_time time  #‘10:53:23‘
);

insert into student values(1,‘ff‘,now(),now(),now(),now());

insert into student values(2,‘ff2‘,,now(),now(),now());

4.枚举和集合

enum 枚举: 规定一个范围,可有多个值,但是为该字段传值时,只能取规定范围内的一个值.

set 集合: 规定一个范围,但是为该字段传值时,能取规定范围中的一个值或多个值.

create table person(
id int primary key auto_increment,
name char(10),
sex enum(‘male‘,‘female‘),
hobbies set(‘music‘,‘read‘,‘swimming‘,‘learning English‘)
);

insert into person (name,sex enum,hobbies set) values(‘ff‘,‘male‘,‘read,swimming‘);

约束

1.not null和 default...

create table student( id int primary key auto_increment, name char(5), sex enum(‘male‘,‘female‘) not null default ‘femela‘ );

insert into student (name) values(‘egon‘);

2.unique 唯一

主键就是不为空且唯一.

2.1单列唯一

create table teacher( id int not null unique, name char(10));

insert into teacher values(1,‘go‘);

insert table teacher values(1,‘alex‘);

2.2多列唯一

create table service( id int primary key autoincrement, name char(10), host char(15), port int, /#constraint hostport unique(host,port) );

2.3偏移量: autoincremenoffset

create table dep( id int primary key autoincrement, name char(10) )autoincrement=10;

insert into dep(name) values (‘it‘),(‘hr‘);

2.4步长 autoincrementincrement

create table dep( id int primary key auto_increment, name char(10) );

insert into dep(name) values (‘it‘),(‘hr‘);

set session autoincrementincrement=10;#会话级,只对当前回话有效.

set global autoincrementincrement=2;#全局,对所有的会话都有效.

autoincrementoffset#偏移量(也就是从几开始) + autoincrementincrement#步长

set session autoincrementoffset=5; set session autoincrementincrement=2;

!!!如果偏移量的值大于步长的值,则偏移量的值会被忽略!

set session autoincrementoffset=2;

set session autoincrementincrement=3;

2.5show variables like ‘%auto_in%‘; #查看变量

primary key字段的值不为空且唯一... 一个表中可以,一般都加在 id 字段上. ```

3.foreign key(附练习) #外键

关联

关联 自己的字段 references(关联) 要关联的地方(字段) dep(id);

foreign key (dep_id) references dep(id)

如果想指定名字 constraint fk_depid_id

先建被关联的表,先建被关联的表,要保证被关联的那个表示不为空且唯一的表. 关联: 多的关联那个1, 多本书关联一个出版社,那么被关联的就是出版社.

create table dep(

id int,

name varchar(50),

comment varchar(100)

);

然后建需要关联的表: create into emp_info(

id int primary key auto_increment,

name varchar(20),

dep_id int,

foreign key(dep_id) references dep(id)

on delete cascade

on update cascade

);

delete from dep where id=2;

update dep set id = 301 where id=3;

以上就是一条记录对应另一个表的多条记录.

先给被关联的表插入初始化记录

一个作者可以属于多个出版社

一本书只能属于一个出版社

书(两本书的信息)

出版社(1设,2设)

多本书属于一个出版社,那么就是多对1, 1就是需要被关联的那个表

书这张表 foreign key了出版社那张表的 id 字段.

出版社是需要被关联的那个1.
create table press(
id int primary key auto_increment,
name varchar(20) not null
);

create table book(
id int primary key auto_increment,
name varchar(20),
press_id int not null,
foreign key(press_id) references press(id)
on delete cascade
on update cascade

insert into press(name) values(‘人出版社‘),(‘民出版社‘);

insert into book(name,press_id) values (‘pyjy‘,1),(‘ons‘,2),(‘go‘,2),(‘python‘,2),(‘java‘,3);

一个作者可以写多本书,一本书可以有多个作者,他俩相互对应

create table author(
id int primary key  auto_increment,
name varchar(20)
);

create table book2author(
id int not null unique auto_increment,
book_id int not null,
author_id int not null,
foreign key(book_id) references book(id)
on delete cascade
on update cascade,
foreign key(author_id) references author(id)
on delete cascade
on update cascade,

primary key (book_id,author_id)
);

insert into author(name) values(‘ff‘),(‘jm‘),(‘by‘);

insert into book2author(book_id,author_id) values(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(2,1),
(2,6),
(3,4),
(3,5),
(3,6),
(4,1);

每个作者与自己的代表作如下
1 ff:
      1 九阳神功
      2 九阴真经
      3 九阴白骨爪
      4 独孤九剑
      5 降龙十巴掌
      6 葵花宝典

2 jm:
      1 九阳神功
      6 葵花宝典

3 by:
      4 独孤6剑
      5 降龙7巴掌
      6 玫瑰花花宝典
时间: 2024-12-17 19:38:48

MySQL 数据类型和约束(外键是重点🙄)的相关文章

mysql为表添加外键完成性约束 报错Can't create table 'sfkbbs.#sql-513_25' (errno: 150)

代码 alter table sfk_son_module add constraint foreign key(father_module_id) references sfk_father_module(id) on delete restrict on update restrict; (constraint 后面可以加上约束名字) 错误原因是之前两张表的id的类型不一样,一个时int,一个时bigint 解决办法时修改表, alter table sfk_father_module mo

Mysql使用Navicat建立外键时报错cannot add foreign key constraint分析

Mysql使用Navicat建立外键时报错cannot add foreign key constraint分析 1)要关联的字段类型或长度不一致. 2)两个要关联的表编码不一样. 3)某个表已经有记录了. 4)将"删除时"和"更新时"都设置相同,如都设置成CASCADE. 原文地址:https://www.cnblogs.com/neymargoal/p/10072347.html

mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)

在MySQL中删除一张表或一条数据的时候,出现 [Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails (...) 这是因为MySQL中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. 我们可以使用 SET FOREIGN_KEY_CHECKS=0;来禁用外键约束.     然后这块执行我们的插入语句....之后再

MySQL学习10:外键约束下的更新操作

上一篇只是讲述了外键约束的要求,并没有讲述外键约束的参照操作.这一次我来看看外键约束下的更新操作. 一外键约束的参照操作 我们进行外键约束的创建以后,在更新表的时候,子表是否也进行相应的更新.这是我们创建外键约束最大的好 处.有以下几种: 1)CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行. 2)SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL.如果使用该选项,必须保证子表列没有指 定NOT NULL. 3)RESTRICT:拒绝对父表的删除或更新操作. 4)

mysql启动和关闭外键约束的方法

关闭外键约束,输入命令:SET FOREIGN_KEY_CHECKS=0; 启动外键约束,输入命令:SET FOREIGN_KEY_CHECKS=1; 查看当前是否有外键约束:SELECT  @@FOREIGN_KEY_CHECKS;

MySQL 如何删除有外键约束的表数据

-- 禁用外键约束 SET FOREIGN_KEY_CHECKS=0; -- 删除数据 truncate table stockTBL; -- 启动外键约束 SET FOREIGN_KEY_CHECKS=1; -- 查看当前FOREIGN_KEY_CHECKS的值,可用如下命令: SELECT @@FOREIGN_KEY_CHECKS;

MySQL的四种外键

转自:http://blog.csdn.net/cnjsnt_s/article/details/5548280 具体使用时需要参考:http://blog.csdn.net/codeforme/article/details/5539454 MySQL有两种常用的引擎类型:MyISAM和InnoDB.目前只有InnoDB引擎类型支持外键约束.InnoDB中外键约束定义的语法如下: [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col

MySQL进阶 一 主外键讲解

1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, 外键可以有重复的, 可以是空值,用来和其他表建立联系用的.所以说,如果谈到了外键,一定是至少涉及到两张表.例如下面这两张表: 上面有两张表:部门表(dept).员工表(emp).Id=Dept_id,而Dept_id就是员工表中的外键:因为员工表中的员工需要知道自己属于哪个部门,就可以通过外键Dept_id找到对应的部门,然后才能找到部门表里的各种字段信息,从而让二者相关联.所以说,

MySql不支持主外键

创建表不支持主外键,能够添加外键成功,但是无法外键约束.查资料发现MySql的默认ENGINE 为MyISAM  ,不支持外键,需要修改为 INNODB 修改前: 1 Create Table 2 3 CREATE TABLE `person` ( 4 `id` varchar(32) NOT NULL, 5 `NAME` varchar(30) DEFAULT NULL, 6 PRIMARY KEY (`id`) 7 ) ENGINE=InnoDB DEFAULT CHARSET=gbk 在创