关系型数据库?
MySQL
MariaDB
Oracle
DB2
SqlServer .NET
SQLite
no-sql
Redis
mongodb
什么是关系型数据库?
基于关系模型的数据库
关系模型?
基于二维表的形式去存取的模型
什么是SQL?
结构化的查询语言.
SQL的分类?
DDL :create/aleter/drop/truncate
DCL :grant/revoke
DML :update/insert/delete
DQL :select
create database db1;
create database db2 character set utf8 ;
alter database db2 character set gbk;
drop database db2;
show databases;
use db1;
select database();
create table t_user (
id int primary key auto_increment,
name varchar(30) not null,
gender int
)
byte/short/int/long
tinyint/smallint/int/bigint
String
char(30)/varchar(30)
float float
double double
boolean
bit
Date
date/time/datetime/timestamp(1970-01-01~2038-01-19)
drop table t_user;
show tables;
desc t_user;
show create table t_user;
alter table t_user add birthday date;
alter table t_user modify birthday bigint;
alter table t_user change birthday birth;
rename table t_user to t_person;
insert into t_employee values();
insert into t_employee(id, ename) values();
insert into h_person(name) select name from t_student ;
update t_user set column1=value1 where id = 1;
update t_user set column1=value1,column2=value2 where id = 1;
oracle 闪回
你可以恢复到你提交以前的commit
start transaction;
DML;
commit;/rollback;
测试库
演示库
生产库
delete from t_user where id = 1;
truncate table t_user;
delete和truncate的区别?
*****1.delete属于DML语句,可以回滚,而truncate属于DDL语句,不能回滚
2. delete是逐行进行删除,不会清空auto_increment;truncate是将表摧毁再重建一张结构完全一样的表,会清空auto_increment
start transaction;
delete
commit;
闪回(flash back)
select column1,column2 from table_name [where]
select a.id,a.name bb from table_name a;
distinct 作用于后边所有的列
between and
1. 包含边界
2. 小值在前,大值在后
order by column1 desc,column2
order by
1. 作用于后边所有列
2. order by后面可以写列名、列的序号、列的别名
in (30, 40, null)
age = 30 or age = 40 or age = null;
not in (30, 40, null)
age != 30 and age <> 40 and age != null;
sum()
avg()
max()
min()
count()
sql中的null值:
1. 表达式如果含有null值,整个表达式的值就为null
2. null永远不等于null(null = null/null != null)
3. 聚合函数能够对null进行虑空
group by...having
having和where的区别?
1. having是先分组后过滤,where是先过滤再分组
在select后边的列,如果这个列没有包含到聚合函数当中,那么这个列必须在 group by 后边
select product, sum(price) from orderitem;
select...from...where...group by...having...order...limit...
三种关系:
1. 一对多
在多的有一个外键指向了一的一方的主键
2. 多对多
会产生一个中间表,中间表有两个字段作为外键分别指向两个表的主键
3. 一对一
1) 基于外键
2) 基于主键
公民
1 张三
2 李四
身份证
1 500119199905263456
2 500119199905263455
create table t_customer(
cid int primary key auto_increment,
cname varchar(30)
);
create table t_order(
oid int primary key auto_increment,
address varchar(50),
cid int,
#foreign key(cid) references t_customer(cid)
);
alter table t_order add foreign key(cid) references t_customer(cid) on delete cascade on update cascade;
级联删除
原型
静态页面/创建数据库表
连接查询分类:
1. 交叉连接(cross join)
select * from t_customer c cross join t_order o on c.cid = o.cid where c.cname = 'zhangsan';
2. 内连接
1) 显式内连接(inner join,inner可以省略)
select * from t_customer c [inner] join t_order o on c.cid = o.cid where c.cname = 'zhangsan';
2) 隐式内连接
select * from t_customer c, t_order o where c.cid = o.cid and c.cname = 'zhangsan';
3. 外链接
1) 左外链接(left [outer] join)
select * from t_customer c left outer join t_order o on c.cid = o.cid where c.cname = 'zhangsan';
2) 右外链接(right [outer] join)
select * from t_customer c right outer join t_order o on c.cid = o.cid
age in (20, 30)
子查询:
1. 一般情况下,子查询放到小括号里面(insert into t_user(name) select name from t_person)
2. 子查询和主查询可以不是同一张表,只要子查询的结果主查询可以使用
3. 一般情况下,先执行子查询,然后再执行主查询,但是相关子查询除外。