详解mysql基本操作详细(二)

吾爱主题 阅读:109 2024-04-05 14:03:08 评论:0

前言

本文类容

1、数据库的几大约束
2、表与表之间的关系

约束:

主键约束:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 作用:为了保证数据的有效性和完整性 mysql中常用的约束:主键约束( primary key ) 唯一约束( unique ) 非空约束( not null ) 外键约束( foreign key ) 主键约束:被修饰过的字段唯一非空      注意:一张表只能有一个主键,这个主键可以包含多个字段      方式1:建表的同时添加约束 格式: 字段名称 字段类型 primary key      方式2:建表的同时在约束区域添加约束          所有的字段声明完成之后,就是约束区域了          格式: primary key (字段1,字段2)                   create table pk01(              id int ,              username varchar (20),              primary key (id)          );                   insert into pk01 values (1, 'tom' ); -- 成功          insert into pk01 values (1, 'tom' ); -- 失败 Duplicate entry '1' for key 'PRIMARY'          insert into pk01 values ( null , 'tom' ); -- 失败 Column 'id' cannot be null                   create table pk01(              id int primary key ,              username varchar (20),              primary key (id)          ); -- 错误的 一张表只能有一个主键               方式3:建表之后,通过修改表结构添加约束          create table pk02(              id int ,              username varchar (20)          );                   alter table pk02 add primary key (字段名1,字段名2..);          alter table pk02 add primary key (id,username);                   insert into pk02 values (1, 'tom' ); -- 成功          insert into pk02 values (1, 'tomcat' ); -- 成功          insert into pk02 values (1, 'tomcat' ); -- 失败

唯一约束

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 被修饰过的字段唯一,对 null 不起作用      方式1:建表的同时添加约束 格式: 字段名称 字段类型 unique          create table un(              id int unique ,              username varchar (20) unique          );                   insert into un value(10, 'tom' ); -- 成功          insert into un value(10, 'jack' ); -- 错误 Duplicate entry '10' for key 'id'          insert into un value( null , 'jack' ); -- 成功          insert into un value( null , 'rose' ); -- 成功               方式2:建表的同时在约束区域添加约束          所有的字段声明完成之后,就是约束区域了          unique (字段1,字段值2...)      方式3:建表之后,通过修改表结构添加约束          alter table 表名 add unique (字段1,字段2); -- 添加的联合唯一          alter table 表名 add unique (字段1); -- 给一个添加唯一          alter table 表名 add unique (字段2); -- 给另一个添加唯一                   ////////////////              create table un01(                  id int ,                  username varchar (20)              );              alter table un01 add unique (id,username);              insert into un01 values (1, 'tom' ); -- 成功              insert into un01 values (1, 'jack' ); -- 成功              insert into un01 values (1, 'tom' ); -- 失败 Duplicate entry '1-tom' for key 'id'

非空约束

?
1 2 3 4 5 6 7 8 特点:被修饰过的字段非空      方式:          create table nn(              id int not null ,              username varchar (20) not null          );                   insert into nn values ( null , 'tom' ); -- 错误的 Column 'id' cannot be null

案例1 一对多 – 创建用户表

?
1 2 3 4 5 6 7 8 9 10 11 create table user (      id int primary key auto_increment,      username varchar (20) );   -- 创建订单表 create table orders(      id int primary key auto_increment,      totalprice double ,      user_id int );

为了保证数据的有效性和完整性,添加约束(外键约束).
在多表的一方添加外键约束

格式:
alter table 多表名称 add foreign key(外键名称) references 一表名称(主键);

例如:
alter table orders add foreign key(user_id) references user(id);

添加了外键约束之后有如下特点:

  1. 1.主表中不能删除从表中已引用的数据
  2. 2.从表中不能添加主表中不存在的数据

开发中处理一对多:
在多表中添加一个外键,名称一般为主表的名称_id,字段类型一般和主表的主键的类型保持一致,
为了保证数据的有效性和完整性,在多表的外键上添加外键约束即可.

案例2 一对多 – 创建用户表

?
1 2 3 4 5 6 7 8 9 10 11 12 -- 创建商品表 create table product(      id int primary key auto_increment,      name varchar (20),      price double );   -- 创建中间表 create table orderitem(      oid int ,      pid int );

– 添加外键约束
alter table orderitem add foreign key(oid) references orders(id);
alter table orderitem add foreign key(pid) references product(id);

开发中处理多对多:
引入一张中间表,存放两张表的主键,一般会将这两个字段设置为联合主键,这样就可以将多对多的关系拆分
成两个一对多了
为了保证数据的有效性和完整性
需要在中间表上添加两个外键约束即可.

案例3-多表查询

笛卡尔积:

?
1 2 多张表无条件的联合查询.没有任何意思      select a.*,b.* from a,b;

内连接

?
1 2 3 4      格式1:显式的内连接      select a.*,b.* from a [ inner ] join b on ab的连接条件 格式2:隐式的内连接      select a.*,b.* from a,b where ab的连接条件

外连接

?
1 2 3 4 5 6 7 8 9 10      左外连接:      select a.*,b.* from a left [outer] join b on 连接条件;      意思:          先展示join左边的(a)表的所有数据,根据条件关联查询 join右边的表(b),符合条件则展示出来,不符合以null值展示.    右外连接:      select a.*,b.* from b right [outer] join a on 连接条件;      意思:          先展示jion右边的表(a)表的所有数据,根据条件关联查询join左边的表(b),符合条件则展示出来,不符合以null值展示.    子查询: 一个查询依赖另一个查询.

以上所述是小编给大家介绍的mysql基本操作详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/weixin_43055096/article/details/88921874

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容