约束条件

  • unsigned

    设置无符号, 针对整形,这样一设置比如说 tinyint本来是(-127--128),设置之后存储范围就变成了255mysql> create table t1 (id int unsigned);
    Query OK, 0 rows affected (0.07 sec)mysql> desc t1;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type         | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | id    | int unsigned | YES  |     | NULL    |       |
    +-------+--------------+------+-----+---------+-------+
    1 row in set (0.01 sec)mysql> insert into t1 values(-1);  # 这里不能插入负数
    ERROR 1264 (22003): Out of range value for column 'id' at row 1
    mysql> insert into t1 values(1);
    Query OK, 1 row affected (0.01 sec)
    
  • zerofill

    用0来填充mysql> create table t1 (id int(10) zerofill);# 注意这里的10不是指存储范围或则长度, 整形的存储范围之和他的类型名有关系,10代表的是最大显示宽度
    Query OK, 0 rows affected, 2 warnings (0.04 sec)mysql> insert into t1 values (11);
    Query OK, 1 row affected (0.01 sec)mysql> select * from t1;
    +------------+
    | id         |
    +------------+
    | 0000000011 |
    +------------+
    1 row in set (0.00 sec)mysql> desc t1;
    +-------+---------------------------+------+-----+---------+-------+
    | Field | Type                      | Null | Key | Default | Extra |
    +-------+---------------------------+------+-----+---------+-------+
    | id    | int(10) unsigned zerofill | YES  |     | NULL    |       |
    +-------+---------------------------+------+-----+---------+-------+
    1 row in set (0.00 sec)int(M) M指示最大显示宽度。最大有效显示宽度是255。显示宽度与存储大小或类型包含的值的范围无关
    
  • not null

    非空mysql> create table t1 (id int, name varchar(16));
    Query OK, 0 rows affected (0.04 sec)mysql> create table t2 (id int, name varchar(16) not null);
    Query OK, 0 rows affected (0.04 sec)mysql> insert into t1 values (1, 'egon');
    Query OK, 1 row affected (0.01 sec)mysql> insert into t1(id) values (1);
    Query OK, 1 row affected (0.01 sec)mysql> insert into t1(id, name) values (1, ''); # 注意这里的 '' 和 空是不一样的
    Query OK, 1 row affected (0.01 sec)mysql> select * from t1;
    +------+------+
    | id   | name |
    +------+------+
    |    1 | egon |
    |    1 | NULL |
    |    1 |      |
    +------+------+
    3 rows in set (0.00 sec)mysql> insert into t2(id, name) values(1, 'egon');
    Query OK, 1 row affected (0.01 sec)mysql> insert into t2(id, name) values(1, '');
    Query OK, 1 row affected (0.01 sec)mysql> insert into t2(id) values(1);  # 报错,因为给name这个字段设置了非空mysql> select * from t2;
    +------+------+
    | id   | name |
    +------+------+
    |    1 | egon |
    |    1 |      |
    +------+------+
    2 rows in set (0.00 sec)mysql> desc t1;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | YES  |     | NULL    |       |
    | name  | varchar(16) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)mysql> desc t2;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | YES  |     | NULL    |       |
    | name  | varchar(16) | NO   |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)# 注意这里的 '' 和 空是不一样的
    
  • default

    设置默认值mysql> create table t1 (id int, name varchar(32) default 'egon');
    Query OK, 0 rows affected (0.04 sec)mysql> desc t1;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int         | YES  |     | NULL    |       |
    | name  | varchar(32) | YES  |     | egon    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)默认值可传可不传
    
  • unique

    唯一:单列唯一:mysql> create table t1 (id int, name varchar(32) unique);Query OK, 0 rows affected (0.06 sec)mysql> desc t1;+-------+-------------+------+-----+---------+-------+| Field | Type        | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id    | int         | YES  |     | NULL    |       || name  | varchar(32) | YES  | UNI | NULL    |       |+-------+-------------+------+-----+---------+-------+2 rows in set (0.00 sec)mysql> insert into t1 values(1, 'qwe');Query OK, 1 row affected (0.01 sec)mysql> insert into t1 values(1, 'qwe');  # qwe上面已经传过一次了;ERROR 1062 (23000): Duplicate entry 'qwe' for key 't1.name'mysql>   多列唯一:mysql> create table t1 (id int, host varchar(16), port int, unique(host, port));# 这个就是host和port加起来唯一,单个可以重复Query OK, 0 rows affected (0.07 sec)mysql> insert into t1 values (1, '127.0.0.1', 3306);Query OK, 1 row affected (0.01 sec)mysql> insert into t1 values (1, '127.0.0.1', 3306);ERROR 1062 (23000): Duplicate entry '127.0.0.1-3306' for key 't1.host'mysql> insert into t1 values (1, '127.0.0.2', 3306);Query OK, 1 row affected (0.01 sec)mysql> insert into t1 values (1, '127.0.0.2', 3302);Query OK, 1 row affected (0.00 sec)
    
  • primary key

    # 1. 从限制角度来说, 主键相当于非空且唯一id int primary key <==> id int not null unique
    # 2. InnoDB存储引擎规定表中必须要有一个主键在之前创建的表中,主键给我们隐藏了,隐藏意味着看不到,也不能用
    # 主键的功能查询速度快, 主键本身也是一种索引
    
  • auto_increment

    每次自增1create table t1 (id int primary key auto_increment)  # id字段 创建的标准语法
    
  • foreign key

    这个是外键关系
    

清空表数据

# 第一种:delete from t1;  # 删除了,下次插入数据,主键不是从1开始
# 第二种truncate table t1;  # 推荐binlog  恢复数据用的,,我们写的sql语句和数据都在这个里面

外键关系的判断

表呢有三种关系:一对一一对多多对多###############一对一##################
eg: 作者表和作者详情表一个作者对应一个作者详情一个作者详情对应一个作者# 这样的关系就是一对一, 建立外键的时候,推荐建立在查询频率高的一方###############一对多##################
eg:书籍表和出版社表一个书籍只能有一个出版社一个出版社可以有多个书籍# 这样的关系就是一对多,外键建立在多的一方###############多对多##################eg:作者表和书籍表一个作者可以写多本书一本书可以有多个作者# 这样的关系就是多对多, 这个建立一张第三方的关系表

代码实现上述需求

###############一对一##################create table author (id int primary key auto_increment,name varchar(16),author_detail_id int unique,foreign key (author_detail_id) references author_detail(id)on update cascade # 级联更新on delete cascade # 级联删除
)create table author_detail(id int primary key auto_increment,phone bigint,email varchar(16),gender enum('male', 'female') default 'male'
)###############一对多##################create table book(id int primary key auto_increment,title varchar(16),price decimal,publish_id int,foreign key (publish_id) references publish(id)on update cascadeon delete cascade
)create table publish(id int primary key auto_increment,name varchar(16),addr varchar(16)
)###############对多对##################create table author(id int primary key auto_increment,name varchar(16)
)create table book(id int primary key auto_increment,title varchar(16),price decimal
)create table author_book(id int primary key auto_increment,book_id int,author_id int,foreign key (book_id) references author(id)on update cascadeon delete cascade,foreign key (author_id) references book(id)on update cascadeon delete cascade
)

聚合函数

max  最大值
min  最小值
sum  求和
count 计数
avg   平均值

模糊查询--like

like关键符合:%: 匹配任意个数的任意字符_: 匹配单个个数的任意字符# 注意: 模糊查询第一个%不走索引,如果想要走索引就把数据同步到esc(elasticsearch)中

查询前期准备数据

create table emp(id int primary key auto_increment,name varchar(20) not null,sex enum('male','female') not null default 'male', #大部分是男的age int(3) unsigned not null default 28,hire_date date not null,post varchar(50),post_comment varchar(100),salary double(15,2),office int, #一个部门一个屋子depart_id int
);#插入记录
#三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','male',48,'20101111','teacher',10000,401,1),
('哈哈','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('呵呵','female',38,'20101101','sale',2000.35,402,2),
('西西','female',18,'20110312','sale',1000.37,402,2),
('乐乐','female',18,'20160513','sale',3000.29,402,2),
('拉拉','female',28,'20170127','sale',4000.33,402,2),
('僧龙','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3);

查询关键字之where

# 1. 查询id大于等于3小于等于6的数据select * from emp where id >=3 and id <= 6;select * from emp where id between 3 and 6;"""between and 在什么之间"""
# 2.查询薪资是20000或者18000或者17000的数据select * from emp where salary =20000 or salary =18000 or salary = 17000;select * from emp where salary in (20000, 17000, 18000);"""in 后面跟一个集合 在这个集合里面"""
# 3. 查询姓名中带有字母o的员工姓名和薪资select name, salary from emp where name like '%o%';\# 4. 查询姓名由四个字符组成的员工姓名和薪资select * from emp where name like '____';select * from emp where char_length(name) = 4;"""char_length()  # 计算长度"""
# 5.查询id小于3或者大于6的数据select * from emp where id < 3 or id > 6;select * from emp where not id between 3 and 6;"""not 取反"""
# 6.查询薪资不在20000,18000,17000范围的数据select * from emp where salary not in (20000, 18000, 17000);"""not in 不走索引"""
# 7.查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用isselect name, post from emp where post_comment is null;"""针对null不能用等号,只能用is"""

查询关键字之group by 分组

分组:

​ 将单个单个的个体按照指定的条件分成一个整体

​ 分组之后默认只能直接取到分组的依据, 其他字段无法直接获取(可以间接获取)

# 严格模式set global sql_mode='STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH,only_full_group_by'# 1. 每个部门的最高薪资select post, max(salary) from emp  group by post;
# 2.每个部门的最低薪资select post, min(salary) from emp group by post;
# 3. 每个部门的平均薪资select post, avg(salary) from emp group by post;
# 4.每个部门的人数select post, count(salary) from emp group by post;
# 5.每个部门的月工资总和select post, sum(salary) from emp group by post;# as 可以给字段起别名select post, sum(salary) as sum_salary from emp group by post;# 6. 查询分组之后的部门名称和每个部门下所有的员工姓名"""group_concat()   获取分组以外的字段数据  并且支持拼接操作concat()   未分组之前使用的拼接功能concat_ws()  """mysql> select group_concat(name, ':', salary) from emp group by post;
+--------------------------------------------------------------------------------------------------+
| group_concat(name, ':', salary)                                                                  |
+--------------------------------------------------------------------------------------------------+
| 僧龙:10000.13,程咬金:20000.00,程咬银:19000.00,程咬铜:18000.00,程咬铁:17000.00                    |
| 哈哈:3000.13,呵呵:2000.35,西西:1000.37,乐乐:3000.29,拉拉:4000.33                                 |
| tom:1000000.31,kevin:8300.00,tony:3500.00,owen:2100.00,jack:9000.00,jenny:30000.00,sank:10000.00 |
+--------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)mysql> select concat(name, ':', salary) from emp where id <3;;
+---------------------------+
| concat(name, ':', salary) |
+---------------------------+
| tom:1000000.31            |
| kevin:8300.00             |
+---------------------------+
2 rows in set (0.00 sec)ERROR:
No query specifiedmysql> select concat_ws(':', name, salary, post) from emp;
+------------------------------------+
| concat_ws(':', name, salary, post) |
+------------------------------------+
| tom:1000000.31:teacher             |
| kevin:8300.00:teacher              |
| tony:3500.00:teacher               |
| owen:2100.00:teacher               |
| jack:9000.00:teacher               |
| jenny:30000.00:teacher             |
| sank:10000.00:teacher              |
| 哈哈:3000.13:sale                  |
| 呵呵:2000.35:sale                  |
| 西西:1000.37:sale                  |
| 乐乐:3000.29:sale                  |
| 拉拉:4000.33:sale                  |
| 僧龙:10000.13:operation            |
| 程咬金:20000.00:operation          |
| 程咬银:19000.00:operation          |
| 程咬铜:18000.00:operation          |
| 程咬铁:17000.00:operation          |
+------------------------------------+
17 rows in set (0.00 sec)

MySQL约束条件及外键的关系相关推荐

  1. 零基础带你学习MySQL—foreign key 外键(二十六)

    零基础带你学习MySQL-foreign key 外键(二十六) -- 外键演示 -- 创建 主表 my_class CREATE TABLE my_class ( id INT PRIMARY KE ...

  2. mysql外键约束案例_详解MySQL中的外键约束问题

    使用MySQL开发过数据库驱动的小型web应用程序的人都知道,对关系数据库的表进行创建.检索.更新和删除等操作都是些比较简单的过程 .理论上,只要掌握了最常见的SQL语句的用法,并熟悉您选择使用的服务 ...

  3. oracle+cascade=gt;true,mysql数据库主外键级联删除脚本RESTRICT -- CASCADE

    在项目中,我们一般在数据库设计的时候做主外键关联设计,要么就不做.但是这样不符合规范,呵呵. 建立主外键关系的时候,默认是不能级联删除的.而出现往往在删除主表的数据时报错, 需要先删除从表然后再删除主 ...

  4. mysql建表 外键_mysql建表外键怎么设

    mysql建表外键怎么设 mysql建表时设置外键的方法:在"CREATE TABLE"语句中,通过"[CONSTRAINT ] FOREIGN KEY 字段名 [,字段 ...

  5. mysql级联删除外键约束_玩转MySQL的外键约束之级联删除篇

    [IT168 文档]如今,许多关系型数据库管理系统都提供了外键约束这一强大的功能特性,它能够帮助我们自动地触发指定的动作,诸如删掉.更新数据库表的记录等,从而维护各数据库表之间预定义的关系.本文将演示 ...

  6. mysql中的外键约束_MySQL外键约束

    在本教程中,您将了解MySQL外键(foreign key)以及如何在MySQL中创建,添加和删除外键约束. MySQL外键简介 外键表示一个表中的一个字段被另一个表中的一个字段引用.外键对相关表中的 ...

  7. Mysql无法创建外键的原因

    在Mysql中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1.  两个字段的类型或者大小不严格匹配.例如,如果一个 ...

  8. mysql不能删除外键吗,MySQL不能删除外键约束所需的索引

    MySQL不能删除外键约束所需的索引 我需要改变现有的数据库添加一列. 因此我也想更新UNIQUE字段来包含这个新列. 我试图删除当前的索引,但不断收到错误MySQL Cannot drop inde ...

  9. mysql setnull_1、Mysql无法创建外键的原因 2、MySql 外键约束 之CASCADE、SET NULL、RESTRICT、NO ACTION分析和作用...

    在Mysql中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1.  两个字段的类型或者大小不严格匹配.例如,如果一个 ...

最新文章

  1. ExtJS 4.2菜单
  2. 配置Configuration Manager站点和层次架构(1)
  3. java android统计图_Android统计图表之柱状图(条形图)
  4. 操作系统/etc/hosts文件配置
  5. 面向数据科学的概率论 二、计算几率
  6. cocos中如何让背景模糊_cocos2dx-js Shader的使用(高斯模糊)
  7. python开源的人脸识别库_什么是 SeetaFace 开源人脸识别引擎
  8. excel转置怎么操作_excel怎么做日历?使用Excel制作日历的操作方法
  9. 【渝粤题库】陕西师范大学209004道德教育案例研究 作业 (高起专)
  10. 网页保存mhtml格式
  11. Autodesk 2013 免费下载 及所有产品product Key(产品密匙)
  12. linux测试dvi接口,支持Linux系统的高清DVI采集卡推荐
  13. k8s node节点重启后遇到的问题及解决
  14. 膜拜!京东大牛彻底讲透Java多线程面试题,看完直怼阿里面试官,堪称吾辈楷模!
  15. 【适配】521- 移动端开发各种兼容适配问题(屏幕、图像、字体与布局等)
  16. TCP\IP 数据流与数据包
  17. 通用mapper——自定义搭配继承Mapper
  18. 基于C#的ArcEngine二次开发28: 等高线高程值与国标码一致性检查思路及代码分析
  19. 调整ie浏览器分辨率_浏览器趋势2015年1月:IE8使用率……三倍?
  20. 被绕晕了,嵌入式用C好还是用C++好

热门文章

  1. 水泵状态监测与故障诊断
  2. vue2前端实现网页截图
  3. 2021-03-08的博客
  4. 神经机器翻译WMT14英法基准系统 WMT14 English-French Baseline
  5. 读中科院一博士论文致谢的感悟
  6. 走进中东走进华为 看中国企业国际化的战略突围
  7. 给自己的网站、app接入广告的方法说明
  8. C++技术开发的发展方向有哪些?
  9. linux 创建多级目录
  10. 手把手教你如何玩转Solr(包含项目实战)