ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

约束条件、主键和外键

2022-03-03 12:04:24  阅读:137  来源: 互联网

标签:约束条件 int create 外键 id key table 主键


约束条件

1.unsigned  非负
create table t2 (id int unsigned);

2.zerofill 0填充
create table t3 (id int zerofill);
insert into t3 values(1);
insert into t3 values(111);

3.default 默认
create table t4 (id int, gender enum('male','female') default'male');

4.unique 唯一
create table t6 (id int,ip varchar(16) unique); ip唯一
create table t7 (id int,ip varchar(16),port int,unique(ip,port)); ip和端口唯一

5.not null 非空
create table t8 (id int, name varchar(16) not null);

6.primary key 主键
	'''
		1. 在数据控制上面,主键相当于非空且唯一
			id int primary key  ===  id int not null unique
		2. InnoDB存储引擎要求每一张表必须有一个主键,之前的InnoDB存储引擎也没有指定主键,但是表也创建成功了,为什么?
			是因为InnoDB引擎有一个默认的主键,而这个默认的主键只是帮助我们把表创建成功,并且是隐藏的主键,不能用。
			主键的另外一个主要作用是:可以帮助我们提高查询速度
		3. 以后我们自己创建表的时候,主动指定主键,
			结论:id int primary key
	'''
 7. auto_increment
	# 自增,你就理解为是配合主键使用的
    id int primary key auto_increment unsigned  # 终极版本
    
    create table t9 (
        id int primary key auto_increment,
        name varchar(16)
    );

清空数据的两种方式

delete from t1;
# 不影响主键

truncate t1;
# 会影响主键的

外键

# 外键:建立两张表之间的关系的中间桥梁
1. 一对多
2. 多对多
3.一对一

# 如何判断表关系
'''一对多的判断?'''
以书表和出版社表为例
	1. 站在两张表的角度,分别问一个问题,一本书能不能有多个出版社?不能
    2. 一个出版社能不能有多个图书?能
    '''
    	结论:如果一个能,一个不能,那么表关系就是'一对多'
    	针对一对多的关系,外键字段健在多的一方
    '''
    
'多对多的判断?'
以图书和作者表为例
	1. 站在图书表角度
    	问:一个图书能否有多个作者写?
        答:可以
    2. 站在作者表的角度
    	问:一个作者能否写多本书
        答:可以
     '''
     	结论:如果两边都可以,那么,表关系就是多对多
     	针对,多对多关系,外键字段不健在任何一方
     	而是,建在第三张表中
     	
     '''
    
    
 '''一对一关系判断'''
以作者表和作者详情表为例
	1. 站在作者表角度
    	问:一个作者能否有多个作者详细?
        答:不可以
    2. 站在作者详情表的角度
    	问:一个作者详情能否有多个作者
        答:不可以
        '''
        	结论:两边都不能,那就是一对一,或这没有关系
        	针对,一对一关系,外键字段建在任何一方都可以,推荐在使用频率比较高的一张表
        '''

SQL语句实现

####################################一对多##########################################
1. 一对多的关系
# 1.在创建表关系的时候,先创建基础字段
# 2.在创建外键关系
# 3. 先创建没有外键的一张表
# 4. 录入数据的时候先录入没有外键的一张表
create table publish (
	 id int primary key auto_increment, 
     name varchar(32)
)

create table book (
    id int primary key auto_increment, 
    title varchar(64),
    price decimal(8, 2),
    publish_id int,
    Foreign key(publish_id) references publish(id)
);


####################################多对多##########################################
create table book (
    id int primary key auto_increment, 
    title varchar(64),
    price decimal(8, 2)
);

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

create table book2author (
	 id int primary key auto_increment, 
     book_id int,
     Foreign key(book_id) references author(id) 
     on update cascade
     on delete cascade,
     author_id int,
     Foreign key(author_id) references book(id) 
     on update cascade 
     on delete cascade
);

###############################一对一######################################
create table author_detail (
	 id int primary key auto_increment, 
     name varchar(32)
)

create table author2 (
    id int primary key auto_increment, 
    name varchar(64),
    author_detail_id int unique,
    Foreign key(author_detail_id) references author_detail(id)
);

级联更新级联删除

create table publish (
	 id int primary key auto_increment, 
     name varchar(32)
)

create table book (
    id int primary key auto_increment, 
    title varchar(64),
    price decimal(8, 2),
    publish_id int,
    Foreign key(publish_id) references publish(id)
    on update cascade 
    on delete cascade
);

标签:约束条件,int,create,外键,id,key,table,主键
来源: https://www.cnblogs.com/blog-tc/p/15959121.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有