欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > My_SQL day3

My_SQL day3

2025/2/26 3:01:19 来源:https://blog.csdn.net/WuMingf_/article/details/143610059  浏览:    关键词:My_SQL day3

知识点:约束

        1.dafault 默认约束

        2.not null 非空约束

        3.unique key 唯一约束

        4.primary key 主键约束

        5.anto_increment 自增长约束

        6.foreign key 外键约束

知识点:表关系

        1.一对一

        2.一对多

        3.多对多

知识点:约束

1.default 默认约束

create table t1(id int default 110,name varchar(10));
insert into t1(name) values("jianqi"),("fufu");+------+--------+
| id   | name   |
+------+--------+
|  110 | jianqi |
|  110 | fufu   |
+------+--------+insert into t1(id,name)values(1,"beibei"),(2,"gangber");+------+---------+
| id   | name    |
+------+---------+
|  110 | jianqi  |
|  110 | fufu    |
|    1 | beibei  |
|    2 | gangber |
+------+---------+

2. not null非空约束

数据不能为空

create table t2(id int not null,name varchar(10));
#即使赋值为空也不行,除非变成字符串;id为int类型,所以不行

3.unique key 唯一约束

create table t3(id unique key,name varchar(10) not null);
insert into t3 value(1,"jianqi");#ok
insert into t3 value(1,"jianqi");#Duplicate entry '1' for key 't3.id'
非空且唯一,第一个非空且唯一的字段会默认为主键(没设置主键的情况)

4.primary key 主键约束

非空且唯一

create table t4(id int primary key,name varchar(20) not null unique key);

5.anto_increment 自增长约束

create table t5 (id int primary key auto_increment,name varchar(20));
create table tb5(id int primary key auto_increment,name varchar(20) not null)auto_increment=100;#如果给定id值之后,后面的自增长就是按照最大的ID进行增长

6.foreign key 外键约束

create table a(a_id int primary key auto_increment,a_name varchar(20) not null);
insert into a(a_name) values ("a1"),("a2");
create table b(b_id int primary key,b_name varchar(20) not null,fy_id int not null,foreign key(fy_id) references a(a_id));
insert into b values(1,"a1",1),(2,"a2",2);
insert into b values(3,"a1",3);#报错,主表中没有#直接删除删不掉a 只能先删除b

知识点:表关系

1.一对一

学生表    课程表

2.一对多

3.多对多

学生表
create table student (s_id int primary key auto_increment,s_name varchar(20) not null);课程表
create table course(course_id int primary key auto_increment,course_name varchar(20) not null)关系表
create table zhongjian(s_id int,course_id int,primary key(s_id,course_id),foreign key(course_id) references course(course_id),);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词