IT评测·应用市场-qidao123.com

标题: MySQL数据库:6、约束的概述及语法 [打印本页]

作者: 丝    时间: 2022-11-24 23:32
标题: MySQL数据库:6、约束的概述及语法
Python基础之MySQL数据库


目录

一、约束概述

1、为什么要约束

​        为了防止数据库中存在不符合语义规定的数据和防止错误信息的输入、输出造成无效的操作而提出的
​        为了保证数据的完整性,SQL规范以约束的方式对表数据进行额外的条件限制,从以下四个方面考虑
2、什么是约束

​        约束是指,对字段名下数据的强制约束
​        约束的方式:
  1. create table 表名(字段名 类型(数字) 约束条件);
复制代码
3、约束的分类

4、查看当前表已有的约束

方式一:
​        show create table 表名;

方式二:
​        describe 表名;/desc 表名;

二、约束语法及用法

1、无符号

  1. mysql> create table t2(id tinyint unsigned);
  2. Query OK, 0 rows affected (0.01 sec)
复制代码

2、零填充

  1. mysql> create table t4(id tinyint(2) zerofill);
  2. Query OK, 0 rows affected (0.01 sec)
复制代码

3、非空

  1. mysql> create table t5(id int(3) zerofill, name varchar(16) not null);
  2. Query OK, 0 rows affected (0.02 sec)
复制代码

4、默认值

  1. mysql> create table t(id int primary key auto_increment,
  2.     -> name varchar(32) not null,
  3.     -> gender varchar(8) default 'mela');
  4. Query OK, 0 rows affected (0.02 sec)
复制代码

5、唯一值

5、1.单列唯一(列级)

  1. mysql> create table t6(id int unique, name varchar(16) not null);
  2. Query OK, 0 rows affected (0.04 sec)
复制代码

5、2.联合唯一(表级)

  1. mysql> create table t7(id int, name varchar(16), unique(id, name));
  2. Query OK, 0 rows affected (0.02 sec)
复制代码

6、主键

  1. mysql> create table t8(id int primary key, name varchar(16));
  2. Query OK, 0 rows affected (0.02 sec)
复制代码

7、自增

  1. mysql> insert into t9(name, age) values('jason', 18),('kang',19);
  2. Query OK, 2 rows affected (0.00 sec)
  3. Records: 2  Duplicates: 0  Warnings: 0
复制代码

8、外键

8、1.外键的定义

​        外键是某个表中某一列,它包含在另一个表的主键中
​        外键也是索引的一种,是通过一张表指向另一张表中的主键,来对两张表进行关联
​        一张表可以有一个外键,也可以存在多个外键,与多张表进行关联
8、2.外键的作用
  1. 主要用于表与表之间的数据关联,减少数据冗余,保证数据的一致性,完整性,主要体现在以下两个方面:
复制代码
8、3.外键的建立

主表:
  1. create table 主表名(
  2.         id 类型 主键 ,
  3.         字段名 类型 约束条件)
复制代码
从表:
  1. create table 从表名(
  2.     id 类型 主键 ,
  3.     字段名 类型(数字) 约束条件,
  4.     绑定外键的字段名  int,
  5.     foreign key(主表名) references 主表名(主表的主键字段名)
  6. )
复制代码
级联创建的方式:
​        需要在创建表阶段,在从表外键关键词后加入:
  1. create table 从表名(
  2.    id 类型 主键 ,
  3.    字段名 类型(数字) 约束条件,
  4.    绑定外键的字段名  int,
  5.    foreign key(主表名) references 主表名(主表的主键字段名)
  6.         on update cascade
  7.         on delete cascade)
复制代码
8、4.外键的多种关系

1.一对多:

​        以员工表为例,通常,一个员工只可以对应一个部门,而一个部门可以对应多个员工,这就是一对多的关系
​        对于一对多的关系,外键字段应该建立在‘多’的一方
  1. create table dep(id int primary key auto_increment,
  2.         dep_name varchar(32)
  3.         );
  4. create table emp(id int primary key auto_increment,
  5.         emp_name varchar(32) not null,
  6.         dep_id int, foreign key(dep_id) references dep(id)
  7.         on update cascade
  8.           on delete cascade
  9.           );
复制代码


2.多对多:

​        以书籍表与作者表为例
  1. create table book(
  2.         id int primary key auto_increment,
  3.         title varchar(32),
  4.           price float(5,2)
  5. );
  6. create table author(
  7.         id int primary key auto_increment,
  8.         name varchar(32),
  9.           phone bigint
  10. );
  11. create table book2author(
  12.         id int primary key auto_increment,
  13.    author_id int,
  14.         foreign key(author_id) references author(id)
  15.           on update cascade
  16.           on delete cascade,
  17.    book_id int,
  18.           foreign key(book_id) references book(id)
  19.           on update cascade
  20.           on delete cascade
  21. );
复制代码

3.一对一:

​        以用户表与用户详情表为例
  1. create table user(
  2.         id int primary key auto_increment,
  3.           name varchar(32),
  4.         detail_id int unique,
  5.           foreign key(detail_id) references userdetail(id)
  6.           on update cascade
  7.           on delete cascade
  8. );
  9. create table userdetail(
  10.         id int primary key auto_increment,
  11.           phone bigint
  12. );
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4