(1)实现数据持久化
(2)数据结构化,易于查询和管理
(3)数据的共享性高,冗余度低,易于扩充
(4)由DBMS统一管理和控制,易于维护
数据库(database):存储数据的“仓库”。它保存了一系列有组织的数据。
create database W;
create table customer(id varchar(20),age int; name varchar(20));
desc W;
select * from W;
select id,name,sex from
insert into w(id name) values(1,'bod');
update w set name='bod' where id = 1;
delete from w where id = 1;
show tables;
desc customer;
create database from; (忘了要写啥了再改)
DQL 数据查询语言 select
DML 数据操纵语言 insert update delete 数据的操作
DDL 数据定义语言 create alter drop 库和表的操作
DCL 数据控制语言 commmit rollback
NOT NULL非空
PRIMARY KEY 主键
DEFAULT默认
FOREIGN KEY外键
UNIQUE唯一
CHECK检查
create view (视图名)
as
(试图内容)
delimiter $$(符号)
create procedure myro1() ——存储过程名字()
begin
select * from beauty where id > 5; ——(存储内容,分号不能忘)
end $$(存储符号不能忘)
##查询操作的排序
where -> group by -> having -> order by
insert into 表()values() or insert into 表() select 数据 union
update ...set ...where
delete from..... or truncate table.....
create database......
drop database.....
create table......
alter table....操作......操作如(rename,change column,modify column,add column,drop column等)
drop table......
create table ....like....(只复制结构)
create table ...... select * from ........(复制结构和数据)
1、TRUNCATE不能加WHERE条件,而DELETE可以加WHERE
2、TRUNCATE效率较高
3、TRUNCATE不能删除多表,而DELETE可以
4、删除带标识列字段的表时,TRUNCATE 删除后再插入,标识列的值从1开始
DELETE删除后再插入,标识列的值从断点处开始
5、TRUNCATE删除后,不能回滚;DELETE删除可以回滚
(1)Timestamp支持的时间范围较小,取值范围
19700101080001——2038年的某个时间
Datetime的取值范围:1000-01-01 ——9999—12-31
(2)timestamp和实际时区有关,更能反映实际的日期,而datetime则只能反映出插入时的当地时区
(3)timestamp的属性受Mysql版本和SQLMode的影响很大
浮点型:
float(M,D)
double(M,D)
定点型:
decimal(M,D)
定点型的精度表示更高,比较适合处理货币运算
datetime和timestamp的区别
字节 时间范围 是否受时区和版本等影响
datetime 8 1000——9999 不受
timestamp 4 1970-2038 受
D表示小数位数,如果不够用0补齐,如果超过则四舍五入
M表示整数位数+小数位数,整数部分如果超过,则报错,显示为临界值
M和D 对于浮点型或定点型都可以省略
但浮点型默认的精度为插入数值的精度,而定点型默认的精度为M=10,D=0
(1) 原子性(Atomicity)
原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
(2)一致性(Consistency)
事务必须使数据库从一个一致性状态变换到另外一个一致性状态。
(3)隔离性(Isolation)
事务的隔离性是指一个事务的执行不能被其他事务干扰,即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
(4)持久性(Durability)
持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来的其他操作和数据库故障不应该对其有任何影响
事先经过编译并存储在数据库中的一段sql语句的集合。
1、简化应用开发人员的很多工作
2、减少数据在数据库和应用服务器之间的传输
3、提高了数据处理的效率
SELECT avg(salary) a FROM employees
SELECT department_id, avg(salary) b FROM employees
where department_id is not null
GROUP BY department_id
having b > (
SELECT avg(salary) a FROM employees
)
SELECT DISTINCT department_id FROM employees e
inner join (
SELECT department_id dep, avg(salary) b FROM employees
where department_id is not null
GROUP BY department_id
having b > (
SELECT avg(salary) a FROM employees
)
) d
on e.department_id = d.dep
SELECT department_id,avg(salary) FROM employees
WHERE department_id is not null
GROUP BY department_id
SELECT max(msalary) FROM (
SELECT department_id,avg(salary) msalary FROM employees
WHERE department_id is not null
GROUP BY department_id
)a
SELECT department_id FROM (
SELECT department_id,avg(salary) msalary FROM employees
WHERE department_id is not null
GROUP BY department_id
) b
WHERE msalary = (
SELECT max(msalary) FROM (
SELECT department_id,avg(salary) msalary FROM employees
WHERE department_id is not null
GROUP BY department_id
) a
)
SELECT last_name, department_id, email, salary from employees
WHERE employee_id = (
SELECT manager_id from departments WHERE department_id =(
SELECT department_id FROM (
SELECT department_id,avg(salary) msalary FROM employees
WHERE department_id is not null
GROUP BY department_id
) b
WHERE msalary = (
SELECT max(msalary) FROM (
SELECT department_id,avg(salary) msalary FROM employees
WHERE department_id is not null
GROUP BY department_id
) a
)
)
)
select last_name,department_id,email,salary,manager_id from employees where manager_id = (
select manager_id from departments where department_id = (
select department_id from employees
where department_id is not NULL
group by department_id
having avg(salary) = (
select max(dsalary) from (
select avg(salary) dsalary,department_id from employees
where department_id is not NULL
group by department_id
having dsalary > (
select avg(salary) from employees
)
) a
)
)
)