首页 > 数据库 >第5节-MySQL表的约束

第5节-MySQL表的约束

时间:2022-12-31 15:11:07浏览次数:39  
标签:sno create 约束 char course MySQL table id

1、约束关键字介绍 

约束条件------说明

PRIMARY KEY ------主键约束,用于唯一标识对应的记录
FOREIGN KEY------外键约束
NOT NULL ------非空约束
UNIQUE------唯一性约束
DEFAULT------默认值约束,用于设置字段的默认值
AUTO_INCREMENT------自增约束,用于设置表的字段值自动增加

2、主键

2.1、单一主键

create table course(
id smallint primary key,
sno char(10),
name varchar(32)
);

2.2、多主键

create table course(
id smallint,
sno char(10),
name varchar(32),
primary key(id,sno)
);

3、约束

3.1、非空约束

create table course(
id smallint,
sno char(10),
name varchar(32) not null
);

3.2、唯一约束

create table course(
id smallint,
sno char(10),
name varchar(32) unique
);

3.3、默认约束

create table course(
id smallint,
sno char(10),
name varchar(32) default 'test-name'
);

3.4、自增约束

create table course(
id smallint primary key auto_increment,
sno char(10),
name varchar(32)
);

3.5、外键约束

create database temp_db;
use temp_db;
create table course(
    courseID smallint primary key,
    courseName varchar(50),
    ctime tinyint unsigned,
    cType varchar(20)
);

create table student(
    sno  char(10) primary key,
    sname varchar(10),
    sex char(1),
    classId char(9),
    idCard char(18)
);

-- 外键的使用方法
create table score(
    score_id tinyint,
    course_id smallint,
    sno_id  char(10),
    constraint fk_course_id foreign key(course_id) references course(courseID),
    constraint fk_student_id foreign key(sno_id) references student(sno)
);

3.6、删除约束

-- 删除外键
alter table score drop foreign key fk_course_id;

3.7、外键的级联

-- 外键的级联的动作
RESTRICT:当要删除或更新父表中被参照列上在外键中出现的值时,拒绝对父表的删除或更新操作。
CASCADE:从父表删除或更新行时自动删除或更新子表中匹配的行·
SET NULL:当从父表删除或更新行时,设置子表中与之对应的外键列为NULL·
No ACTION:意味不采取动作,跟RFSTRICT一样。

-- 级联使用方法,注意语法fk,cascade后面不用逗号
create table score(
    score_id tinyint,
    course_id smallint,
    sno_id  char(10),
    constraint fk_course_id foreign key(course_id) references course(courseID),
    constraint fk_student_id foreign key(sno_id) references student(sno)
    on update cascade
    on delete cascade
);

 

标签:sno,create,约束,char,course,MySQL,table,id
From: https://www.cnblogs.com/ygbh/p/17016675.html

相关文章

  • 第6节-MySQL表增、删、改
    1、测试表结果createtablestudent(snovarchar(20)primarykey,snamevarchar(20),sexenum('男','女'),ageint,sdeptvarchar(20));2......
  • Centos安装Mysql
    Centos安装MysqlCentos通过下载安装包方式进行安装Mysql,Centos版本7.9,Mysql版本8.0.31#查看Centos版本cat/etc/redhat-release#查看mysql版本mysql--versionmysq......
  • MySQL密码策略审计,提升安全
    背景:审核密码是否符合规范,如大小写、数字、特殊字符等安装插件:1、在线启动INSTALLPLUGINvalidate_passwordSONAME'validate_password.so';2、写入配置文件plugi......
  • 认真学习MySQL中锁机制(二)
    接上文​​认真学习MySQL中锁机制(一)​​我们继续学习MySQL中的锁机制。【5】按加锁的方式划分:显示锁、隐式锁①隐式锁一个事务在执行insert操作时,如果即将插入的间隙已经被......
  • SQLAlchemy连接MySQL及记录的查询、更新、删除、多表关联查询
    SQLAlchemy是Python的ORM库,支持多种数据库。建立连接连接MySQL要用到​​Engine​​,Engine集成了连接池pool和方言Dialect(支持不通数据库的SQL语法),最后都统一成标准DBAPI。f......
  • Docker运行mysql
    1、拉取mysql5.7镜像dockerpullmysql:5.72、创建用于挂载的目录(日志目录、数据目录、配置目录) 3、在conf目录下创建my.cnf配置文件my.cnf内容如下:[mysqld]......
  • MySQL报错解决
    1mysql报错解决mysql>grantallon*.*to"dba"@"%"identifiedby"mysql123";ERROR1819(HY000):Yourpassworddoesnotsatisfythecurrentpolicyrequirement......
  • MySQL slave upgrade: Slave failed to initialize relay log info structure from th
    MySQL slaveafterupgradefrom 5.6.x to 5.7.x maythrowthefollowingerror:12mysql>STARTSLAVE;ERROR1872(HY000):Slavefailedtoinitiali......
  • mysql调优
    一、insert调优1、插入多条数据时最好批量插入(但一般不超过一千条)2、手动提交事务,多条语句一起提交starttransaction;insertintotablevalues(),(),();......
  • 数据库(约束)
    概述1.概念:约束是作用于表中字段上的规则,用于限制存储在表中的数据。2.目的:保证数据库中数据的正确、有效性和完整性。3.分类:约束描述关键字非空约束限制该字段的数据不能为......