mysql 数据类型+约束+关联
1.什么是存储引擎
存储引擎就是表的类型,针对不同的存储引擎,mysql会有不同的处理逻辑
2.存储引擎介绍
InnoDB| DEFAULT | Supports transactions, row-level locking, and foreign keys
事物
blackhole 黑洞
配置文件管理
\s看mysql当前配置编码情况
表操作之数据类型
常用数据类型:
#整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT
存储大小越来越大↑
#作用:存储年龄,等级,id,各种号码等
create table t1(id int(1));
insert into t1 values(256111);
select * from t1;
create table t2(id int(20));
insert into t2 values(256111);
select * from t2;
create table t3(id int(20) zerofill);
insert into t3 values(256111);
select * from t3;
mysql> create table t4(id int);
Query OK, 0 rows affected (0.46 sec)
mysql> desc t4;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> insert into t4 values(1111111111111111111111111111111111111111111);
Query OK, 1 row affected, 2 warnings (0.17 sec)
mysql> select * from t4;
+------------+
| id |
+------------+
| 2147483647 |
+------------+
1 row in set (0.00 sec)
mysql> create table t5(id int unsigned);
Query OK, 0 rows affected (0.45 sec)
mysql> desc t5;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> insert into t5 values(11111111111111111111111111111111111111111);
Query OK, 1 row affected, 2 warnings (0.05 sec)
mysql> select * from t5;
+------------+
| id |
+------------+
| 4294967295 |
+------------+
1 row in set (0.00 sec)
#强调:整型的宽度指的是显示宽度,并不是存储宽度
#浮点类型:FLOAT,DOUBLE,DECIMAL
#作用:体重,薪资,价格
约束
设计表结构、构建表与表之间的关系(多对一,一对一,多对多):
mysql> create table t6(weight float(256,56) unsigned);
ERROR 1425 (42000): Too big scale 56 specified for column 'weight'. Maximum is 30.
mysql> create table t6(weight float(256,30) unsigned);
ERROR 1439 (42000): Display width out of range for column 'weight' (max = 255)
mysql> create table t6(weight float(255,30) unsigned);
Query OK, 0 rows affected (0.37 sec)
mysql> desc t6;
+--------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------------+------+-----+---------+-------+
| weight | float(255,30) unsigned | YES | | NULL | |
+--------+------------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> create table t7(weight double(256,33) unsigned);
ERROR 1425 (42000): Too big scale 33 specified for column 'weight'. Maximum is 30
mysql> create table t7(weight double(256,30) unsigned);
ERROR 1439 (42000): Display width out of range for column 'weight' (max = 255)
mysql> create table t7(weight double(255,30) unsigned);
Query OK, 0 rows affected (0.36 sec)
mysql> create table t8(weight decimal(66,33) unsigned);
ERROR 1425 (42000): Too big scale 33 specified for column 'weight'. Maximum is 30.
mysql> create table t8(weight decimal(66,30) unsigned);
ERROR 1426 (42000): Too big precision 66 specified for column 'weight'. Maximum is 65.
mysql> create table t8(weight decimal(65,30) unsigned);
Query OK, 0 rows affected (0.39 sec)
#对比三种类型的精度
insert into t6 values(1.1111111111111111111111111111111111111111111111111111111111111111);
insert into t7 values(1.1111111111111111111111111111111111111111111111111111111111111111);
insert into t8 values(1.1111111111111111111111111111111111111111111111111111111111111111);
mysql> select * from t6;
+----------------------------------+
| weight |
+----------------------------------+
| 1.111111164093017600000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> select * from t7;
+----------------------------------+
| weight |
+----------------------------------+
| 1.111111111111111200000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> select * from t8;
+----------------------------------+
| weight |
+----------------------------------+
| 1.111111111111111111111111111111 |
+----------------------------------+
1 row in set (0.00 sec)
#了解:BIT
mysql> create table t9(x bit(1));
Query OK, 0 rows affected (0.34 sec)
mysql> insert into t9 values(2);
Query OK, 1 row affected, 1 warning (0.04 sec)
mysql> select * from t9;
+------+
| x |
+------+
| |
+------+
1 row in set (0.00 sec)
mysql> select bin(x) from t9;
+--------+
| bin(x) |
+--------+
| 1 |
+--------+
1 row in set (0.02 sec)
mysql> select hex(x) from t9;
+--------+
| hex(x) |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
日期类型
DATE:2017-11-11
TIME: 10:14:11
DATETIME:2017-11-11 10:14:11
TIMESTAMP:2017-11-11 10:14:11
YEAR:1970
可以直接调用mysql的函数,能按照类型直接获取时间数据
了解:timestamp与datatime
create table t10(
born_date date,
class_time time,
reg_time datetime,
born_year year
);
insert into t10 values
('1999-11-11','08:30:00','2017-11-11 11:11:11',2011);
insert into t10 values
(now(),now(),now(),now());
#了解:datetime与timestamp
create table t11(
x datetime,
y timestamp
);
desc t11;
mysql> desc t11;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| x | datetime | YES | | NULL | |
| y | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.01 sec)
mysql> insert into t11 values(null,null);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t11;
+------+---------------------+
| x | y |
+------+---------------------+
| NULL | 2017-10-23 10:25:07 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> insert into t11 values('1011-11-11','1011-11-11');
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> select * from t11;
+---------------------+---------------------+
| x | y |
+---------------------+---------------------+
| NULL | 2017-10-23 10:25:07 |
| 1011-11-11 00:00:00 | 0000-00-00 00:00:00 |
+---------------------+---------------------+
字符类型
#char与varchar
select char_length(name) from t16;#统计字符长度4
字符串类型作用:名字,密码,职位,地址
char类型:
范围:0-255
特点:
定长,简单粗暴,浪费空间(待存储的数据长度<宽度限制),存取速度快
varchar类型:
范围:0-21844
特点:
变长,精准,节省空间(待存储的数据长度<宽度限制),存取速度慢
1、范围
mysql> create table t12(x char(256));
ERROR 1074 (42000): Column length too big for column 'x' (max = 255); use BLOB or TEXT instead
mysql> create table t12(x varchar(21845));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 6553
ns to TEXT or BLOBs
mysql> create table t12(x varchar(21844));
Query OK, 0 rows affected (0.41 sec)
mysql> desc t12;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| x | varchar(21844) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> create table t13(x varchar(65534));
Query OK, 0 rows affected, 1 warning (0.38 sec)
mysql> desc t13;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x | mediumtext | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.01 sec)
2、宽度的限制
mysql> create table t14(x char(3));
Query OK, 0 rows affected (0.67 sec)
mysql> create table t15(x varchar(3));
Query OK, 0 rows affected (0.40 sec)
mysql>
mysql> insert t14 values('xxxxxxxxxx');
Query OK, 1 row affected, 1 warning (0.10 sec)
mysql> insert t15 values('xxxxxxxxxx');
Query OK, 1 row affected, 1 warning (0.07 sec)
mysql> select * from t14;
+------+
| x |
+------+
| xxx |
+------+
1 row in set (0.00 sec)
mysql> select * from t15;
+------+
| x |
+------+
| xxx |
+------+
1 row in set (0.00 sec)
mysql> insert t14 values('你好啊啊啊');
Query OK, 1 row affected, 1 warning (0.06 sec)
mysql> select * from t14;
+-----------+
| x |
+-----------+
| xxx |
| 你好啊 |
+-----------+
2 rows in set (0.00 sec)
create table t16(name char(5));
create table t17(name varchar(5));
alex |e |wupei|yh |
标记alex|标记e|标记wupei|标记yh|
验证定长与变长
create table t16(name char(5));
create table t17(name varchar(5));
insert into t16 values('a'); #'a '
insert into t17 values('a'); #'a'
select * from t16;
select * from t17;
SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';
select char_length(name) from t16;
select char_length(name) from t17;
枚举类型
#enum set
enum()括号内可以规定要传的是什么不是就是空白值,可以设置默认值
set()set类型里面的值可以在insert时候选择里面的多个传进去,例如多个爱好
create table t18(
id int,
name char(10),
sex enum('male','female','None')
);
alter table t18 modify sex enum('male','female','None') not null default 'male';
insert into t18 values(1,'egon','xxxxx');
insert into t18(id,name) values(1,'egon');
create table t19(
id int,
name char(10),
hobbies set('music','read','basketball','football','eat','sleep')
);
insert into t19 values(1,'egon','music,read,eat');
完整性约束
key:
primay key
unique
foreign key
primay key()可以放两个,联合主键,还是一个主键但是有两个条件
差错了自增ID也走 所以得truncate 表名
KEY:
primay key
unique
foreign key
mysql> create table t20(id int auto_increment,name char(10));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
create table t20(id int primary key auto_increment,name char(10));
create table t21(id int not null unique auto_increment,name char(10));
create table t22(
id int primary key,
name char(10)
);
create table t23(
id int,
name char(10),
constraint pri_id primary key(id)
);
create table t24(
id int,
name char(10),
primary key(id)
);
create table t25(
id int,
name char(10),
constraint uni_id unique(id)
);
create table t26(
id int,
name char(10),
unique(id)
);
#只能有一个主建,但是可以有多个not null unique
create table t27(
id int,
name char(10),
primary key(id),
primary key(name)
);
create table t28(
id int not null unique,
name char(10) not null unique
);
联合唯一
create table t29(
id int,
ip char(15),
port int,
primary key(ip,port)
);
insert into t29 values
(1,'1.1.1.1',3306),
(2,'1.1.1.2',3306),
(3,'1.1.1.1',8080)
;
create table t30(
id int primary key auto_increment,
ip char(15) not null,
port int not null,
unique(ip,port)
);
insert into t30(ip,port) values
('1.1.1.1',3306),
('1.1.1.1',3307),
('1.1.1.2',3307)
;
自增ID
auto_increment
show variables like '%incre%' 查看mysql跟increment有关的配置信息
外键
foreign key() reference dep(id)
#先创建被关联的表
create table dep(
id int primary key auto_increment,
dep_name char(20) not null unique,
dep_comment varchar(50)
)auto_increment=200;
insert into dep(dep_name,dep_comment) values
('IT','xxxxxxxxxx'),
('Sale','yhyyyyyyy'),
('Operation','asdfadfadsf'),
('HR','asfasdfasdfasdfasdf')
;
#再创表去关联上面的表
create table emp(
id int primary key auto_increment,
name char(6) not null,
sex enum('male','female') not null default 'male',
dep_id int,
foreign key(dep_id) references dep(id)
on delete cascade
on update cascade
);
insert into emp(name,sex,dep_id) values
('egon','male',200),
('alex','male',200),
('yh','female',203),
('evia','female',200),
('wpq','male',202)
;
insert into emp(name,sex,dep_id) values
('alex1','male',250);
#解散一个部门
#未指定同步更新、同步删除的参数时,需要这么删除
delete from emp where dep_id=200;
delete from dep where id=200;
#指定后
mysql> select * from dep;
+-----+-----------+---------------------+
| id | dep_name | dep_comment |
+-----+-----------+---------------------+
| 200 | IT | xxxxxxxxxx |
| 201 | Sale | yhyyyyyyy |
| 202 | Operation | asdfadfadsf |
| 203 | HR | asfasdfasdfasdfasdf |
+-----+-----------+---------------------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+----+------+--------+--------+
| id | name | sex | dep_id |
+----+------+--------+--------+
| 1 | egon | male | 200 |
| 2 | alex | male | 200 |
| 3 | yh | female | 203 |
| 4 | evia | female | 200 |
| 5 | wpq | male | 202 |
+----+------+--------+--------+
5 rows in set (0.00 sec)
mysql> delete from dep where id=200;
Query OK, 1 row affected (0.06 sec)
mysql> select * from emp;
+----+------+--------+--------+
| id | name | sex | dep_id |
+----+------+--------+--------+
| 3 | yh | female | 203 |
| 5 | wpq | male | 202 |
+----+------+--------+--------+
2 rows in set (0.00 sec)
mysql> select * from dep;
+-----+-----------+---------------------+
| id | dep_name | dep_comment |
+-----+-----------+---------------------+
| 201 | Sale | yhyyyyyyy |
| 202 | Operation | asdfadfadsf |
| 203 | HR | asfasdfasdfasdfasdf |
+-----+-----------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+----+------+--------+--------+
| id | name | sex | dep_id |
+----+------+--------+--------+
| 3 | yh | female | 203 |
| 5 | wpq | male | 202 |
+----+------+--------+--------+
2 rows in set (0.00 sec)
mysql> update dep set id=2002 where id=202;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from dep;
+------+-----------+---------------------+
| id | dep_name | dep_comment |
+------+-----------+---------------------+
| 201 | Sale | yhyyyyyyy |
| 203 | HR | asfasdfasdfasdfasdf |
| 2002 | Operation | asdfadfadsf |
+------+-----------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+----+------+--------+--------+
| id | name | sex | dep_id |
+----+------+--------+--------+
| 3 | yh | female | 203 |
| 5 | wpq | male | 2002 |
+----+------+--------+--------+
2 rows in set (0.00 sec)
查询操作:
select * from mysql.user\G这样的话竖起来显示
distinct查的时候去重复
mysql 数据类型+约束+关联的更多相关文章
- Database学习 - mysql数据类型约束
mysql数据类型 - 属性
- MySQL数据类型 约束
一.数据库CDGS. 库 增 create database 库名; 删 drop 库名; 改 alter database 库名称 修改的属性名称; 查 show databases;#查看 ...
- Mysql 数据类型、约束类型
mysql数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准 ...
- MySQL 数据类型简介 创建数据表及其字段约束
数据类型介绍 MySQL 数据类型分类 整型 浮点型 字符类型(char与varchar) 日期类型 枚举与集合 具体数据类型见这篇博客 MySQL表操作中的约束 primary key 主键约束 非 ...
- MySQL数据类型——数值类型
1.1.1 整型 整型 占用字节 范围 范围 tinyint 1 -27~27-1 -128~127 smallint 2 -215~215-1 -32768~32767 mediumint 3 -2 ...
- mysql的约束
SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). (1)NOT NULL约 ...
- [转]mysql的约束
转自:http://blog.csdn.net/kqygww/article/details/8882990 MySQL中约束保存在information_schema数据库的table_constr ...
- MYSQL数据库约束类型
07.14自我总结 MYSQL数据库约束类型 一.主键约束(primary key) 主键约束要求主键列的数据唯一,并且不能为空.主键分为两种类型:单字段主键和多字段联合主键. 1.单字段主键 写法 ...
- 🚴♂️全套MySQL数据库教程_Mysql基础入门教程,零基础小白自学MySQL数据库必备教程☔ #002 # 第二单元 MySQL数据类型、操作表#
二.本单元知识点概述 (Ⅰ)知识点概述 二.本单元教学目标 (Ⅰ)重点知识目标 1.Mysql的数据类型2.如何选择数据类型3.创建表4.修改表5.删除表 (Ⅱ)能力目标 1.熟练创建数据库及删除数据 ...
随机推荐
- [cogs729]圆桌问题(最大流)
传送门 模型 二分图多重匹配问题,可以用最大流解决. 实现 建立二分图,每个单位为X集合中的顶点,每个餐桌为Y集合中的顶点,增设附加源S和汇T. 1.从S向每个Xi顶点连接一条容量为该单位人数的有向边 ...
- C++ 错误解决 —— internal compiler error
问题: g++ 编译时,报错: g++: internal compiler error: Killed (program cc1plus) 出错原因: 出错的原因是(虚拟机)运行内存不足,而大量te ...
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
如何在spring mvc框架中实现MessageSource来管理国际资源文件呢 如下: 1.在applicationContext.xml文件内配置如下 <span style=" ...
- bzoj 3924 幻想乡战略游戏
题目大意: 有边权点权的树,动态修改点权 每次修改后求带权重心x (\(minimize\) \(S=\sum_i val[i]*dist[x][i]\)) 分析: 从暴力找突破口: 对于边x,y,设 ...
- 排列计数(bzoj 4517)
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- 【BZOJ2243】染色(树链剖分)
题意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由 ...
- 利用github搭建个人网站
1.注册一个github https://github.com/ 2.新建一个仓库 仓库名 用 Owner.github.io 的格式,然后点击创建 3.源码上传至github 安装github桌 ...
- 51NOD 1833 环
考虑一下简单环覆盖这个图的意义,其实就是找出原序列的所有排列,满足所有<i,a[i]>都是原图中的一条有向边. 因为一个置换就是由很多简单环构成的. 于是我们可以设 f[i][S] 为考虑 ...
- 第5章 Spring Boot 功能
Spring Boot 功能 本节将会介绍Spring Boot的一些细节. 在这里,您可以了解您将要使用和自定义的主要功能. 如果还没有准备好,您可能需要阅读第二部分“入门指南”和第三部分“使用 S ...
- Spring Cloud服务的注册与发现
Spring Cloud简介: Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分 ...