侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL学习笔记之数据定义表约束,分页方法总结

数据库  /  管理员 发布于 6年前   210

本文实例讲述了MySQL学习笔记之数据定义表约束,分页方法。分享给大家供大家参考,具体如下:

1. primary key 主键

特点:主键是用于唯一标识一条记录的约束,一张表最多只能有一个主键,不能为空也不能重复

create table user1(id int primary key,name varchar(32));mysql> insert into user1 values(1,'hb');Query OK, 1 row affected (0.10 sec)mysql> insert into user1 values(1,'hb');ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'mysql> insert into user1 (name) values('hb');ERROR 1364 (HY000): Field 'id' doesn't have a default value

2. auto_increament 自增长

mysql> create table user2(id int primary key auto_increment,name varchar(34));mysql> insert into user2 (name ) values ("name1");Query OK, 1 row affected (0.09 sec)mysql> insert into user2 (name ) values ("name2");Query OK, 1 row affected (0.05 sec)mysql> insert into user2 (name ) values ("name3");Query OK, 1 row affected (0.13 sec)mysql> select * from user2;+----+-------+| id | name |+----+-------+| 1 | name1 || 2 | name2 || 3 | name3 |+----+-------+

3. unique 唯一约束

特点:表的某列值不能重复,可以添加重复的NULL

create table user3(id int primary key auto_increment,name varchar(34) unique);mysql> create table user3(id int primary key auto_increment,name varchar(34) unique);Query OK, 0 rows affected (0.39 sec)mysql> insert into user3 (name ) values ("name3");Query OK, 1 row affected (0.11 sec)mysql> insert into user3 (name ) values ("name3");ERROR 1062 (23000): Duplicate entry 'name3' for key 'name'

允许插入null,并且可以多个

mysql> insert into user3 (name ) values (null);Query OK, 1 row affected (0.12 sec)mysql> insert into user3 (name ) values (null);Query OK, 1 row affected (0.12 sec)mysql> select * from user3;+----+-------+| id | name |+----+-------+| 3 | NULL || 4 | NULL || 1 | name3 |+----+-------+

4. not null

mysql表的列默认情况下可以为null,如果不允许某列为空则可以使用not null说明

create table user4 (id int primary key auto_increment,name varchar(32) not null);mysql> insert into user4 (name) values(null);ERROR 1048 (23000): Column 'name' cannot be null

5. foreign key 外键

从理论上说先建立主表,再建立从表

雇员表:

create table dept(id int primary key , name varchar(32));

部门表:

create table emp(id int primary key ,name varchar(32),deptid int,constraint myforeignkey foreign key(deptid) references dept(id));mysql> select * from dept;+----+-------+| id | name |+----+-------+| 1 | name1 |+----+-------+1 row in set (0.00 sec)mysql> insert into emp values(1,'aaa',1);Query OK, 1 row affected (0.22 sec)mysql> insert into emp values(1,'aaa',2);ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'mysql> insert into emp values(1,'aaa',null);ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'mysql> insert into emp values(2,'aaa',null);Query OK, 1 row affected (0.13 sec)mysql> select * from emp;+----+------+--------+| id | name | deptid |+----+------+--------+| 1 | aaa |   1 || 2 | aaa |  NULL |+----+------+--------+2 rows in set (0.00 sec)

总结:

① 外键只能指向主表的主见列或者unique
② 外键的数据类型应该与它指向的列类型一致
③ 外键的值:NULL 或者 指向列中存在的值
④ 外键可以指向本表的主键列或者unique

mysql 不支持check

create table user99(age int check(age>13));mysql> create table user99(age int check(age>13));Query OK, 0 rows affected (0.19 sec)mysql> insert into user99 values(99);Query OK, 1 row affected (0.04 sec)mysql> select * from user99;+------+| age |+------+|  99 |+------+

mysql 分页

基本语法:

select * from 表明 where 条件 limit 从第几条取,取出几条
mysql 是从第0条开始取数据

mysql> select * from student;+------+--------+---------+---------+------+| id  | name  | chinese | english | math |+------+--------+---------+---------+------+|  1 | 张小明   |   89 |   78 |  90 ||  2 | 李进    |   67 |   98 |  56 ||  3 | 王五    |   87 |   78 |  77 ||  4 | 李一   |   88 |   98 |  90 ||  5 | 李来财    |   82 |   84 |  67 ||  6 | 张进宝   |   55 |   85 |  45 ||  7 | 张小明   |   75 |   65 |  30 |+------+--------+---------+---------+------+7 rows in set (0.05 sec)mysql> select * from student limit 2,2;+------+------+---------+---------+------+| id  | name | chinese | english | math |+------+------+---------+---------+------+|  3 | 王五   |   87 |   78 |  77 ||  4 | 李一  |   88 |   98 |  90 |+------+------+---------+---------+------+2 rows in set (0.00 sec)

按照语文成绩排序,查处第3条到第5条

mysql> select * from student order by chinese desc limit 3,2;+------+--------+---------+---------+------+| id  | name  | chinese | english | math |+------+--------+---------+---------+------+|  5 | 李来财    |   82 |   84 |  67 ||  7 | 张小明   |   75 |   65 |  30 |+------+--------+---------+---------+------+2 rows in set (0.00 sec)

扩展,分页:pageNow , pageSize

select * from 表明 where 条件 [group by … having … order by …]limit 从第几条取,取出几条
select * from 表明 where 条件 [group by … having … order by …]limit (pageNow-1)*pageSize, pageSize

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL索引操作技巧汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》

希望本文所述对大家MySQL数据库计有所帮助。


  • 上一条:
    MySQL学习笔记之创建、删除、修改表的方法
    下一条:
    Mysql获取id最大值、表的记录总数等相关问题的方法汇总
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客