约束
限制字段的取值
primary key
主键约束
特点
1.唯一
2,非空
去重
去掉字段值是相同的数据
distinct(类型)
as 给一个字段临时取别名,不会影响字段的原名称
类型 as 别名
中文加引号
as可省略
备份/还原数据库
Linux中操作
navicat中操作
Navicat中
选中数据库,进行转储(备份)
xxxx.sql
后缀sql,说明这是一个sql脚本文件
什么是脚本文件
脚本文件中保存的都是增删改查的
sql语句的集合
notepad++
记事本工具
这个工具比普通记事本功能更强大,可以打开各种不同格式的文件
Navicat还原数据库
步骤
1,先创建一个新的数据库
2,鼠标移动到数据库上面,右键执行sql
3,选择sql文件,执行
linux中备份数据库
Linux 命令 /Linux 指令
sql语句
mysqldump -uroot -p +数据库名称 >脚本文件名称
mysqldump 转储
-uroot -p 用户,密码
转储的数据库 mysql
>重定向符
>覆盖
>>追加
mysql.sql
test.sql
备份数据库
mysqldump -uroot -p mysql >mysql.sql
输入root用户的密码
还原数据库
创建一个新的数据库
create database db1
linux操作页面
mysql -uroot -p 要还原度数据库 <db.sql 脚本文件
例子
mysql -uroot -p db1 <db.sql
笛卡尔积
消除笛卡尔积
3*3=9条数据,消除没有用的6条数据
a=a b=b c=c
a.id=b.id
实现多表查询
基本格式
基本链接
select *
from a,b
where a.id=b.id
select name,score
from a,b
where a.id=b.id
内连接 table1 inner join table2...on+条件
用法和基本连接一模一样的
基本格式
select *
from a inner join b
on a.id=b.id
基本连接,内连接
得到的是两个表共有字段值的数据
左连接 left join on
特点
以左表为主,得到左表所有的数据+右表共有的数据
基本格式
select *
from a left join b
on a.id=b.id
右连接 right join on
特点
1.以右表为主,得到右表所有的数据+左表共有的数据
基本格式
select *
from a right join b
on a.id=b.id
表a独有的数据
select *
from a left join b
on a.id=b.id
where b.id is null
联合 union
作用
把表a独有的数据和表b独有的数据
select *
from a left join b
on а.id=b. id
where b.id is null
union
select *
from a right join b
on a. id=b. id
where a.id is null
|
|