数据库的CRUD操作
MySQL的语句:
SQL: Structure Query Language 结构化查询语句
DDL: 数据定义语言(定义数据库,数据表的结构):create(创建)drop(删除) alter(修改)
DML: 数据操纵语言(主要用来操作数据): insert(插入) update(修改) delete(删除)
DCL: 数据控制语言:定义访问权限,取消访问权限 ,安全设置 grant
DQL: 数据查询语言:select(查询) from字句,where字句
首先登录数据库服务器:
mysql -u root -p
创建数据库:
create database "创建数据库的名称"
或
create database "数据库的名称" character set utf8; (character set utf8:指定字符编码)
查看所有数据库:
show databases;
修改数据库:
修改数据库字符集:
alter database "数据库名字" character set "指定字符编码";
删除数据库:
删库:
drop database "数据库名字";
删表:
drop table "数据表名字";
其他操作命令:
切换数据库: use "数据库名字"; 查看当前正在使用的数据库: select database();
表的CURD操作
创建表: create table student ( id int primary key auto_increment, (primary key :绑定主键)(auto_increment:自动递增) name varchar(31), sex int, age int );
查看所有表: show tables; 查看表的创建过程: show create table "表名"; 查看表结构: desc "表名";
修改表:
添加列(add)
alter table "表名" add "列名" "类型" "约束";
alter table student add name varchar(30) not null;
修改列(modify)
"表名" modify "列名" "类型" ;
alter table student modify name varchar(20);
修改列名(change)
"表名" change "列名" "新的列名" "类型" ;
alter table student change name username varchar(20);
删除列(drop)
"表名" drop "列名";
alter table student drop username ;
修改表名(rename)
"表名" "新的表名"
rename table student to newstudent;
修改表的字符集:
alter table student character set gbk;
删除表:
drop table "表名";
SQL对表中数据的CURD操作:
插入数据
--单行插入数据--
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3)
insert into student(id,name,age) values(null,"张三",30);
--简单写法--
insert into student values(null,"张三",30);
--批量插入多行数据--
insert into student values(null,"A",1,1), (null,"A",2,2), (null,"A",3,3);
删除数据
# delete - 条件删除
DELETE FROM "表名" WHERE ID=1; (WHERE 后面添加条件)
# delete - 删除整个表数据
DELETE FROM "表名";
# truncate - 删除整个表数据
TRUNCATE TABLE "表名";
#DELETE和TRUNCATE的差别
DELETE是DML语句,一条一条删除数据。(表中数据有可能被恢复)
TRUNCATE是DDL语句,先删除表在重建表。(表中数据不可被恢复)
修改数据
# update -条件修改
"表名" set "列名"="值" where "条件"
update student set name="李四" where id=1;
查询记录
#查询列表所有数据
select * from "表名"
#根据条件查询
select * from where "条件"
比如 :select * from where id=1;
#多表查询:
--内链接查询:
隐式内链接: select * from student as s,calss as c where s.classId=c.Id;
显示内链接: select * from student as s inner join class as c on s.classId=c.Id;
--左外连接
会将左表(student)中所有数据查询出来,如果右表(class)中没有会以NULL标明
select * from student as s left outer join class as c on s.classId=c.Id;
--右外连接
会将右表(class)中所有数据查询出来,如果左表(student)中没有会以NULL标明
select * from student as s right outer join class as c on s.classId=c.Id;
#分页查询
select * from student limit 0,5;(0:索引;5:查询的个数)
#子查询
例子:查询"JAVA"课程的所有学生
select * from student as s where s.classId= (select id from class as c where classname="JAVA" );(先查出JAVA课程的ID,再查询学生表中的CLSSID中跟查出来的JAVA的id相同数据)
--select运算查询(+,-,x,/)
select *,money -50 as money2 from student; (在查询结果出来之后,将money这列中的所有数值减去50,查询出新的一列money2,原来的money值不变)
--去掉重复值(distinct)
select distinct money from student;(查询数值相同的只显示一个)
--like(模糊查询)
_ :代表一个字符
select * from student where name like "_英" (查询student表中名字带有英的两个字符)
% :代表多个字符
select * from student where name like "%英%"
--in(在某个范围中获取值)
select * from student where 成绩 in (60,80) (查询成绩是60和80的记录)
--排序查询(order by 关键字)
asc: ascend 升序(默认的排序方式)
select * from student order by 成绩;
desc:descend 降序
select * from student order by 成绩 desc;
查询名称带有"王"的姓名,成绩按升序排序:
select * from student where name like "王%" order by 成绩 ;
--聚合函数
sum(): select sum(成绩) from student; (求和)
avg(): select avg(成绩) from student; (求平均值)
max(): select max(成绩) from student;(求最大值)
min(): select min(成绩) from student;(求最小值)
count(): select count(*) from student; (统计数量)
注意:where 后面不能接聚合函数
例子:查询成绩大于平均值的同学
select * from student where 成绩 > avg(成绩);(不正确的)
select * from student where 成绩 > (select avg(成绩) from student); (正确的:利用子查询)
--分组(group by 关键字)
例子: 根据成绩进行分组,统计个数并排序
select 成绩,count(*) from student group by 成绩 order by 成绩;
having 关键字:可以接聚合函数,出现在分组之后
例子:根据成绩分组,并且平均分大于60的个数
select 成绩,count(*) from student group by 成绩 having avg(成绩) > 60 order by 成绩;
--关系运算符
<> :不等于(标准SQL语法)
!= :不等于(非标准SQL语法)
> :大于
>= :大于或等于
< :小于
<= :小于或等于
= :等于
--
多表之间的建表原则:
- 一对多:在多的一方添加一个外键,指向一的一方的主键。
例子(班级和学生)
班级表(class): 学生表(student):
查询语句: select * from class as c
left join student as st on st.classId=c.id;
(根据ID查询):
select * from class as c
left join student as st on st.classId=c.id where st.Id=1;
- 多对多: 多建一张中间表,将多对多的关系拆成一对多关系,中间表至少要有两个外键,分别指向原来的那张表。
例子(学生和课程):
学生表(student): 课程表(timetable): 关系表(relationtable)
查询语句: select * from relationtable as rt
left join student as st on rt.studentId = st.Id
left join timetable as tb on rt.timetableId= tb.Id;
(根据ID查询):
select * from relationtable as rt
left join student as st on rt.studentId = st.Id
left join timetable as tb on rt.timetableId= tb.Id where st.Id=1;