脚本宝典收集整理的这篇文章主要介绍了MYSQL笔记,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
数据库:就是数据存放的仓库
数据库(Database,简称DB)是长期存储在计算机内部有结构的、大量的共享数据集合。
长期存储:持久存储
有结构:
大量:大多数数据库都是文件系统的,也就是说存储数据在数据库中的数据实际上就是存储在磁盘文件中
共享:多个应用程序可以通过数据库共享
关系型数据库
关系型数据库,采用了关系模型来组织数据存储,以行和列的形式存储数据并记录数据与数据之间的关系。
将数据存储在表格中,可以通过建立表格与表格之间的关联来维护数据与数据之间的关系。
学生信息——学生表
班级信息——班级表
非关系型数据库
非关系型数据库,采用键值对的模型来存放存储数据,只完成的数据记录,不会记录数据与数据之间的关系。
在非关系型数据库中基于其特定存储结构解决一些大数据应用难题
NoSQL(Not only sql)数据库来指代非关系型数据库
关系型数据库产品
非关系型数据库产品
面向检索的列式存储 Column-Oriented
面向高并发的缓存存储 Key-value
面向海量数据访问的文档存储 Document-Oriented
略
当完成数据库安装之后,mysql是以服务的形式运行在windows/liunx系统,用户通过DBMS工具对mysql进行操作的,当我们安装完成Mysql之后默认安装了 mysql Command line clinet,此工具是一个命令行形式工具,通常我们会单独安装可视化DBMS工具
- navicat
打开mysql Command line clinet:开始菜单–mysql –mysql Command line clinet
链接mysql:输入密码即可(如果密码输入错误会闪退 ——–密码password)
关闭输入exIT即可
略
Mysql可以存储数据,但是存储在mysql中的数据需要按照特定的接管进行存储
学生——学校
数据——数据库
数据库软件——》数据库——》数据表——》字段(列)
SQL结构化查询语言,用于存储、查询、更新数据以及管理关系型数据库系统
根据SQL指令完成数据库操作的不同,可以将SQL指令分为四类:
在MYSQL command line client 或者navicat等工具中都可以编写SQL指令
SQL指令不区分大小写
每条SQL表达式结束后都已;结束
SQL指令以空格进行分隔
SQL之间可以不限制换行(可以有空格的地方就可以有换行)
使用DDL语句可以创建数据库、查询数据库、修改数据库、删除数据库
查询数据库
##显示当前MYSQL中数据库列表
show databases;
##显示指定名称的数据库的创建的SQL指令
show create database db_test;
创建数据库
##创建数据库 db_name 表示创建数据库名称,可以自定义
create database <db_name>;
##创建数据库,当指定名称的数据库不存在时执行创建
create database if not exists <db_name>;
##在创建数据库的同时指定数据库的字符集(字符集:数据存储在数据库采用的编码格式utf8 gbk)
create database db_fmsc character set utf8;
修改数据库 修改数据库的字符集
##修改数据库的字符集
alter database <db_test> character set utf8; #utf8 gbk
删除数据库 删除数据库时会删除当前数据库中所有的数据表以及数据表中数据
## 删除数据库
drop database db_test;
## 如果数据库存在则删除数据库
drop database if exists db_test;
使用/切换数据库
use <db_name>
创建数据表
数据表实际就是一个二维表格,一个表格是由多列组成,表格每一类称之为表格的一个字段
create table students(
stu_num char(8) not null unique,
stu_name vArchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
stu_tel char(11) not null unique,
stu_QQ VARchar(11) unique
);
查询数据表
show tables;
查询表结构
desc <stundents>;
删除数据表
##删除数据表
drop table <studnts>;
##当数据表存在时删除数据表
drop table if exists students;
修改数据表
##修改表名
ALTER TABLE <students> rename to stus;
##数据表也是有字符集的,默认字符集和数据库一致
alter table <stus> character set utf8;
##添加字段(添加列)
alter table stus add stu_remark varchar(200);
##修改列类型和名称
alter table stus change stu_remark stu_desc text;
##只修改字段类型
alter table stus modify stu_desc varchar(200);
##删除字段
alter table stus drop stu_desc;
数据类型,指的是数据表中的列支持存放的数据类型
在mysql中有多种数据类型可以存放数值,不同类型存放的数值的范围或者形式是不同的
类型 | 内存空间大小 | 范围 | 说明 |
---|---|---|---|
tinyint | 1byte | 有符号-128127<br>无符号0255 | 小型整数(年龄) |
smallint | 2byte(16bit) | 有符号-3276832767<br>无符号065535 | 小型整数 |
mediumint | 3byte | 有符号-231~231-1无符号0~2^32-1 | 中型整数 |
int/integer | 4byte | 有符号-3276832767<br/>无符号065535 | 整数 |
bigint | 8byte | 有符号-3276832767<br/>无符号065535 | 大型整数 |
float | 4byte | 单精度 | |
double | 8byte | 双精度 | |
decimal | 第一个参数+2 | decimal(10,2)表示数值一共有10位小数位有两位 |
存储字符序列的类型
类型 | 字符序列的长度范围 | 说明 |
---|---|---|
char | 0~255字节 | 定长字符串,最多可以存储255个字符,当我们指定数据表字段为char(n)此列中的数据最长为n个字符,如果添加的数据少于n,则补充0000至n长度 |
varchar | 0~65535字节 | 可变长度字符串,此类型最大长度为65535 |
tinyblob | 0~255字节 | 存储二进制字符串 |
blob | 0~65535字节 | 存储二进制字符串 |
mediumblob | 0~1677215字节 | 存储二进制字符串 |
longblob | 0~4294967295字节 | 存储二进制字符串 |
tinytext | 0~255字节 | 文本数据(字符串) |
text | 0~65535字节 | 文本数据(字符串) |
mediumtext | 0~1677215字节 | 文本数据(字符串) |
longtext | 0~4294967295字节 | 文本数据(字符串) |
在mysql数据库中,我们可以使用字符串来存储时间,但是我们需要基于时间字段进行查询(查询在某个时间段内数据) 就不便于查询现实
类型 | 格式 | 说明 |
---|---|---|
date | 2021-9-13 | 日期、只存储年月日 |
time | 11:12:54 | 时间、只存储时分秒 |
year | 2021 | 年份 |
datetime | 2021-9-13 1:12:54 | 日期+时间 存储年月日时分秒 |
timestamp | 20210913 111213 | 日期+时间(时间戳) |
创建数据表时,对数据表的列的数据限制性要求(对表的列的数据进行限制)
为什么要给表中添加约束?
字段常见约束有哪些?
限制数据表中此列的值必须提供*
create table books(
book_isbn char(4),
book_name varchar(10) not null,
book_author varchar(6)
);
添加数据:
在表中的多条数据,此列的值不能重复
create table books(
book_isbn char(4) unique,
book_name varchar(10) not null,
book_author varchar(6)
);
主键--就是数据表中唯一标识,在一张表中只能有一个主键(主键可以为一列,也可以是多个列的组合)
当一个字段声明为主键之后,添加数据时:
- 此字段数据不能为null
- 此字段数据不能重复
创建表时定义主键
create table books(
book_isbn char(4) PRimary key,
book_name varchar(10) not null,
book_author varchar(6)
);
或者
create table books(
book_isbn char(4) ,
book_name varchar(10) not null,
book_author varchar(6),
primary key(book_isbn)
);
删除数据表主键约束
alter table books drop primary key;
创建表后添加主键约束
##创建表时没有添加主键约束
create table books(
book_isbn char(4),
book_name varchar(10) not null,
book_author varchar(6)
);
##创建表之后添加主键约束
alter table books modify book_isbn char(4) primary key;
我们在创建数据表时,如果数据表中有列可以作为主键(例如学生表学号、图书表isbn)我们可以直接设置这个列为主键;当有些数据表中没有合适的列作为主键时,我们可以额外定义一个与记录本书无关的列(ID)作为主键,此列数据无具体含义,主要用于标识一条记录,在mysql中我们可以将此列定义为int,同时设置为自动增长,当我们像数据表中新增一条记录时,无需提供ID列的值,他会自动生成
定义自动增张
create table types(
type_id int primary key auto_increment,
type_name varchar(20) not null,
type_remark varchar(100)
);
注意:自动增长从1开始,每田间一条记录,自动的增长的列会自定+1,当我们把某条记录删除之后,自动增长的数据也不会重复生成(自动增长只保证唯一性,不保证连续性)
联合主键——将数据表中多列组合在一起设置为表的主键
定义联合主键
create table grades(
stu_num char(8),
course_id int,
score int,
primary key(stu_num,course_id)
);
注意:在实际企业项目数据库设计中,联合主键使用比较少,使用频率并不高,当一张数据表中没有明确的字段可以作为主键时,我们可以额外添加一个ID字段作为主键。
在多表管理部分讲解
用于完成对数据表中数据的插入、删除、修改操作
create table students(
stu_num char(8) primary key,
stu_name varchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
stu_tel char(11) not null unique,
stu_qq varchar(11) unique
);
语法
insert into <table_name> (columName...)values(values1,values2....);
示例
##向数据表中指定的列添加数据(不允许为空的列必须提供数据)
insert into stus(stu_num,stu_name,stu_gender,stu_age,stu_tel)
values('20210101','张三','男','21','130313131');
##数据表名后的字段名列表顺序可以不与表中一致,但是values中值的顺序必须与表名后字段名顺序对应
insert into stus(stu_num,stu_name,stu_age,stu_tel,stu_gender)
values('20210102','王五','20','123666558','女');
##当要向表中所有列添加数据时,数据表名后面的字段列表可以省略,但是value中值的顺序需要与数据表定义的字段保持一致,
insert into stus values('20210105','孙琪','男','21','13031221332','666666');
##不过在项目开发中即使要向所有列添加数据,也建议将字段列表显示写出来(增强sql稳定性)
insert into stus(stu_num,stu_name,stu_gender,stu_age,stu_tel,stu_qq)
values('20210105','孙琪','男','21','13031221332','666666');
从数据表中删除满足特定条件(所有)的数据的记录
语法
delete From <tablename> where conditions;
实例
##删除学号为20210102的学生信息
delete from stus where stu_num='20210102';
##删除年龄大于20岁的学生信息(如果满足where子句的记录有多条,则删除多条记录)
delete from stus where stu_age>20;
##删除语句没有where子句,则表示删除当前数据表中的所有数据(敏感操作)
delete from stus;
对数据表中已经添加的记录进行修改
语法
update <tableName> set columnName=vlaue
示例
##将学号20210105学生姓名修改为孙七
update stus set stu_name='孙七'where stu_num='20210105';
##将学号为20210103学生性别修改为男,qq修改为7777
update stus set stu_gender='男',stu_qq='7777' where stu_num='20210103';
##根据主键修改其他所有列
update stus set stu_name='韩美美',stu_gender='女',stu_age='18',stu_tel='1311155588',stu_qq='999999' where stu_num='20210102';
##如果update语句没有where语句,则表示当前表中所有行(记录)
update stus stu_name='Tom';
从数据表中提取满足特定条件的记录
- 单表查询
- 多表联合查询
语法
##select关键字后指定要显示查询到的记录有哪些列
select column1,[column2,column3.....]from <tableName>;
##如果要显示查询到记录的所有列,则可以使用 *代替字段名表 (在项目开发中不建议使用)
select * from stus;
在删除、修改以及查询语句后都可以添加where子句(条件),用于筛选满足特定条件的数据进行删除、修改以及查询
delete from tableName where conditions;
update tableName set ... where conditons;
select ....from tableName where conditions;
条件关系运算符
## = 等于
select * from stus where stu_num='20210101';
## != <>不等于
select * from stus where stu_num!='20210101';
select * from stus where stu_num<>'20210101';
## > 大于
select * from stus where stu_age>'18';
## < 小于
select * from stus where stu_age<'20';
## >= 大于等于
select * from stus where stu_age>='20';
##<= 小于等于
select * from stus where stu_age<='20';
##between and 区间查询 between V1 and V2
select * from stus where stu_age between 18 and 20;
条件逻辑运算符
在where子句中,可以将多个条件通过逻辑预算符(and or not)进行连接,通过多个条件来筛选要操作的数据。
##and 并且 筛选多个条件同时满足的数据
select * from stus where stu_gender='女' and stu_age<21;
##or 或者 筛选多个条件中满足一个即可
select * from stus where stu_gender='女' or stu_age<21;
##not 取反
select * from stus where stu_age not between 18 and 20;
6.7.3LIKE子句
在where子句条件中,我们可以使用LIKE关键字来实现模糊查询
语法
xxxxxxxxxx xxxxxxxxxx select * from tableName where columnName like 'reg'; mysql mysql
示例
#查询包含学生姓名字母o的学生信息
select * from stus where stu_name like '%o%';
#查询姓名第一个字为张的学生信息
select * from stus where stu_name like '张%';
#查询学生姓名最后一个字母为o的学生信息
select * from stus where stu_name like '%o';
#查询学生姓名中第二个字母为o的学生信息
select * from stus where stu_name like '_o%';
设置查询的列
声明显示查询结构指定列
select columnName1,columnName2,....from stus where stu_age>20;
计算列
对从数据表中查询的记录进行一定的运算之后显示出来
#出生年份=当前年份-年龄
select stu_name,2021-stu_age from stus;
+----------+--------------+
| stu_name | 2021-stu_age |
+----------+--------------+
| omg | 2000 |
| 韩美美 | 2003 |
| Tom | 2001 |
| lucy | 2000 |
| polly | 2000 |
| theo | 2004 |
+----------+--------------+
as字段取别名
可以给查询结果的列名,取一个语义性更强的别名(如下案例中as关键字可以省略)
mysql> select stu_name,2021-stu_age as stu_birth_year from stus;
+----------+----------------+
| stu_name | stu_birth_year |
+----------+----------------+
| omg | 2000 |
| 韩美美 | 2003 |
| Tom | 2001 |
| lucy | 2000 |
| polly | 2000 |
| theo | 2004 |
+----------+----------------+
mysql> select stu_name as 姓名,2021-stu_age as 出生年份 from stus;
+--------+----------+
| 姓名 | 出生年份 |
+--------+----------+
| omg | 2000 |
| 韩美美 | 2003 |
| Tom | 2001 |
| lucy | 2000 |
| polly | 2000 |
| theo | 2004 |
distinct消除重复行
从查询结果中将重复的记录消除
mysql> select distinct stu_age from stus;
+---------+
| stu_age |
+---------+
| 21 |
| 18 |
| 20 |
| 17 |
+---------+
将查询到的满足条件的记录按照指定的列的值升序或降序排列
语法
select *from tableName where conditions order by columnName asc|desc;
实例
#单字段排序
select * from stus where stu_age>15 order by stu_gender desc;
+----------+----------+------------+---------+-------------+--------+
| stu_num | stu_name | stu_gender | stu_age | stu_tel | stu_qq |
+----------+----------+------------+---------+-------------+--------+
| 20210101 | omg | 男 | 21 | 130313131 | NULL |
| 20210103 | Tom | 男 | 20 | 123666558 | 7777 |
| 20210105 | polly | 男 | 21 | 13031221332 | 666666 |
| 20210106 | theo | 男 | 17 | 213212312 | NULL |
| 20210102 | 韩美美 | 女 | 18 | 1311155588 | 999999 |
| 20210104 | lucy | 女 | 21 | 132132123 | NULL |
+----------+----------+------------+---------+-------------+--------+
#多字段排序 先满足第一个排序规则,当第一个排序规则的列的值相同在按照第二个列的规则排序
mysql> select * from stus where stu_age>15 order by stu_gender asc,stu_age desc;
+----------+----------+------------+---------+-------------+--------+
| stu_num | stu_name | stu_gender | stu_age | stu_tel | stu_qq |
+----------+----------+------------+---------+-------------+--------+
| 20210104 | lucy | 女 | 21 | 132132123 | NULL |
| 20210102 | 韩美美 | 女 | 18 | 1311155588 | 999999 |
| 20210101 | omg | 男 | 21 | 130313131 | NULL |
| 20210105 | polly | 男 | 21 | 13031221332 | 666666 |
| 20210103 | Tom | 男 | 20 | 123666558 | 7777 |
| 20210106 | theo | 男 | 17 | 213212312 | NULL |
+----------+----------+------------+---------+-------------+--------+
SQL中提供了一些对查询记录的列进行计算的函数——聚合函数
- count
- max
- min
- sum
- avg
#统计学生表中学生的总数
mysql> select count(stu_num) from stus;
+----------------+
| count(stu_num) |
+----------------+
| 7 |
+----------------+
#统计学生表中性别为男的总数
mysql> select count(stu_num) from stus where stu_gender='男';
+----------------+
| count(stu_num) |
+----------------+
| 5 |
+----------------+
select max(stu_age) from stus;
+--------------+
| max(stu_age) |
+--------------+
| 21 |
+--------------+
select max(stu_age) from stus where stu_gender='女';
+--------------+
| max(stu_age) |
+--------------+
| 21 |
+--------------+
mysql> select min(stu_age) from stus;
+--------------+
| min(stu_age) |
+--------------+
| 14 |
+--------------+
1 row in set (0.00 sec)
mysql> select min(stu_age) from stus where stu_gender='女';
+--------------+
| min(stu_age) |
+--------------+
| 18 |
+--------------+
1 row in set (0.00 sec)
#计算所有年龄学生总和
select sum(stu_age) from stus;
+--------------+
| sum(stu_age) |
+--------------+
| 133 |
+--------------+
#计算所有男生年龄的总和
select sum(stu_age) from stus where stu_gender='男';
+--------------+
| sum(stu_age) |
+--------------+
| 94 |
+--------------+
select avg(stu_age) from stus;
+--------------+
| avg(stu_age) |
+--------------+
| 19.0000 |
+--------------+
select avg(stu_age) from stus where stu_gender='男';
+--------------+
| avg(stu_age) |
+--------------+
| 18.8000 |
+--------------+
日期函数
当我们向日期类型的列添加数据时,可以通过字符串类型(字符串格式必须为yyyy-MM-dd hh:mm:ss)
如果想要获取当前系统时间添加到当前日期类型的列,可以使用‘now()或者Sysdate()
示例
desc stus;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| stu_num | char(8) | NO | PRI | NULL | |
| stu_name | varchar(20) | NO | | NULL | |
| stu_gender | char(2) | YES | | NULL | |
| stu_age | int | NO | | NULL | |
| stu_tel | char(11) | NO | | NULL | |
| stu_qq | varchar(11) | YES | UNI | NULL | |
| stu_enterence | datetime | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
#通过字符串类型,给日期类型的列赋值
insert into stus(stu_num,stu_name,stu_gender,stu_age,stu_tel,stu_qq,stu_enterence)
values('20210108','张小三','女','20','134343434','12311','2021-09-01 09:00:00');
#通过now()获取当前时间
insert into stus(stu_num,stu_name,stu_gender,stu_age,stu_tel,stu_qq,stu_enterence)
values('20210109','张小四','女','20','1355553434','133311',now());
#通过sysdate()获取当前时间
insert into stus(stu_num,stu_name,stu_gender,stu_age,stu_tel,stu_qq,stu_enterence)
-> values('20210110','李磊','男','16','134556434','12314441',sysdate());
#通过now和sysdate获取当前系统时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2021-12-25 07:04:16 |
+---------------------+
1 row in set (0.00 sec)
mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2021-12-25 07:04:57 |
+---------------------+
1 row in set (0.00 sec)
字符串函数
通过SQL指令对字符串进行处理
示例
#concat(column1,column2.....)拼接多列
select concat(stu_name,'-',stu_gender) from stus;
+---------------------------------+
| concat(stu_name,'-',stu_gender) |
+---------------------------------+
| omg-男 |
| 韩美美-女 |
| Tom-男 |
| lucy-女 |
| polly-男 |
| theo-男 |
| 林涛-男 |
| 张小三-女 |
| 张小四-女 |
| 李磊-男 |
+---------------------------------+
#upper(column)将字段值转为大写
mysql> select upper(stu_name) from stus;
+-----------------+
| upper(stu_name) |
+-----------------+
| OMG |
| 韩美美 |
| TOM |
| LUCY |
| POLLY |
| THEO |
| 林涛 |
| 张小三 |
| 张小四 |
| 李磊 |
+-----------------+
#lower(column)将指定列的值转换成小写
mysql> select lower(stu_name) from stus;
+-----------------+
| lower(stu_name) |
+-----------------+
| omg |
| 韩美美 |
| tom |
| lucy |
| polly |
| theo |
| 林涛 |
| 张小三 |
| 张小四 |
| 李磊 |
+-----------------+
#substring(column,start,len)从指定列中截取部分显示,start从1开始
select stu_name,substring(stu_tel,7,4) from stus;
+----------+------------------------+
| stu_name | substring(stu_tel,7,4) |
+----------+------------------------+
| omg | 131 |
| 韩美美 | 5588 |
| Tom | 558 |
| lucy | 123 |
| polly | 2133 |
| theo | 312 |
| 林涛 | |
| 张小三 | 434 |
| 张小四 | 3434 |
| 李磊 | 434 |
+----------+------------------------+
分组-就是将数据表中的记录按照指定的列分组
语法
select (分组字段/聚合函数)
from 表名
[where 条件]
group by 分组列名[having条件]
[order by排序字段]
;
示例
#先对查询学生按性别进行分组(纷争了男女两组),然后再分别统计每组学生的个数
mysql> select stu_gender,count(stu_num) from stus group by stu_gender;
+------------+----------------+
| stu_gender | count(stu_num) |
+------------+----------------+
| 女 | 4 |
| 男 | 5 |
+------------+----------------+
#先对查询学生按性别进行分组(纷争了男女两组),然后再分别统计每组学生的平均年龄
mysql> select stu_gender,avg(stu_age) from stus group by stu_gender;
+------------+--------------+
| stu_gender | avg(stu_age) |
+------------+--------------+
| 女 | 19.7500 |
| 男 | 18.2000 |
+------------+--------------+
#先对学生按年龄进行分组(分为16、17、18、20、21、22六祖),然后统计个组的学生数,还可以对最终结果进行排序
mysql> select stu_age,count(stu_num) from stus group by stu_age order by stu_age;
+---------+----------------+
| stu_age | count(stu_num) |
+---------+----------------+
| 16 | 2 |
| 17 | 1 |
| 18 | 1 |
| 20 | 3 |
| 21 | 1 |
| 22 | 1 |
+---------+----------------+
#查询所有学生,按年龄进行分组,然后分别统计每组的人数,再筛选当前组人数>1的组显示出来,再按年龄升序排序
mysql> select stu_age,count(stu_num)
-> from stus
-> group by stu_age
-> having count(stu_num)>1
-> order by stu_age
-> ;
+---------+----------------+
| stu_age | count(stu_num) |
+---------+----------------+
| 16 | 2 |
| 20 | 3 |
+---------+----------------+
#查询性别为‘男’的学生,按年龄进行分组,然后分别统计每组的人数,再筛选当前组人数>1的组显示出来,再按年龄升序排序
mysql> select stu_age,count(stu_num)
-> from stus
-> where stu_gender='男'
-> group by stu_age
-> having count(stu_num)>1
-> order by stu_age;
+---------+----------------+
| stu_age | count(stu_num) |
+---------+----------------+
| 16 | 2 |
| 20 | 2 |
+---------+----------------+
当数据表中的数据比较多时,如果一次性全部查询出来显示给用户,用户体验性/可读性不还好,因此我们可以将这些数据分页显示
语法
select ....
from...
where...
limit param1,param2
案例
对数据表中的学生进行分页显示,总共有10条数据,我们每页显示3条
总记录数 count10
每页显示 pageSize 3
总页数 pageCount =count%pageSize==0?countpageSize;countpageSiz:+1;
#查询第一页
select * from [where ....] limit 0,3; (1-1)*3
#查询第二页
select * from stus [where...] limit 3,3; (2-1)*3
#查询第三页
select * from stus [where...] limit 6,3; (3-1)*3
#查询第四页
select *from stus [where...] limit 9,3; (4-1)*3
#如果在一张数据表中,
#pageNum表示查询的页码,
#pageSize表示每页显示的条数
#通用分页语句如下
select * from <tabName>[where...] limit (pageNum-1)*pageSize,pageSize
mysql是一个关联型数据库,不仅可以存储数据,还可以维护数据与数据之间的关系,通过在数据表中添加字段建立外键约束
数据与书记之间的关联关系分为四种
人——身份证 一个人只有一个身份证,一个身份证只对应一个人
学生——学籍 一个学生只对应一个学籍,一个学籍也对应唯一一个学生
用户——用户详情 一个用户只有一个详情,一个详情也只对应一个用户
方案一:主键关联——两张数据表中主键相同的数据为相互对应的数据
方案二:唯一外键——在任意一张表中添加一个字段添加外键约束与另外一张表主键关联,并且将外键列设置添加唯一约束
班级——学生(一对多)一个班级包含多个学生
学生——班级(多对一)多个学生可以属于一个班级
图书——分类 商品——商品类别
方案:在多的一端添加外键,与一的一段进行关联
学生——课堂 一个学生可以选择多门课,一门课程也可以由多个学生选择
会员——社团 一个会员可以参加多个社团,一个社团也可以招纳多个会员
方法:额外创建一张关系表来维护多对多关联——在关系表中定义两个外键,分别在两个关系表的主键进行关联
外键约束——将一个列添加外键约束与另一张表的组件进行关联之后,这个外键约束的列添加数据
必须要在关联的主键阻断中存在
以上是脚本宝典为你收集整理的MYSQL笔记全部内容,希望文章能够帮你解决MYSQL笔记所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。