SQL常用语句
1.mysql的基本操作
1.开启mysql数据库服务
格式: net start mysql
(注意:开启需要使用以下语句,且要以管理员身份打开cmd窗口)
如: net start mysql57
2.关闭mysql数据库服务
格式: net stop mysql
如: net stop mysql57
3.登陆mysql服务
格式: mysql -u root -p
如: mysql -u root -p 执行成功后,在输入密码,
4. 查看当前数据库的版本信息
格式: select version();
5.查看当前时间
格式: select now();
6.退出,
quit,exit
2.数据库database的操作
1.查看当前有哪些数据库
格式: show databases;
2.创建一个数据库
格式: create database 数据库名 charset=utf8;
例: create database db charset=utf8;
3.删除一个数据库
格式: drop database 数据库名;
例: drop database db;
4.使用数据库
格式:use 数据库名;
例: use db;
5.查看当前使用数据库
格式: select database();
3.表table的操作
1.查看当前数据库有哪些表
格式: show tables;
2.创建一个表
格式: create table 表名(字段名及类型);
例: create table student(
id int auto_increment primary key,
name varchar(20) not null,
age int not null,
address varchar(20),
gender bit default 1
);
3.查看表的结构
格式: desc 表名;
例: desc student;
4.查看创建表的语句
格式: show create table 表名;
5.删除表
格式: drop table 表名;
例: drop table student;
6.修改表
1.修改表的名字
格式: rename table 旧表名 to 新表名;
例: rename table student to students;
2.修改表的结构: modify | add | drop | change
1.modify 修改表字段类型
格式: alter table 表名 modify 字段名 varchar(20);
例: alter table student modify name varchar(20);
1.add 添加一个新的字段
格式: alter table 表名 add 字段名 类型;
例: alter table student add phone int;
2.change 修改
格式: alter table 表名 change 旧字段名 新字段名 新字段类型;
例: alter table student change phone number varchar(20);
3.drop 删除
格式: alter table 表名 drop 字段名;
例: alter table student drop number;
4.数据的操作
1.插入数据
1.插入全列数据
格式: insert into 表名 values(值1,值2,....)
(注意:顺序需要与表的结构对应,主健可以写0)
例: insert into student values(0,"马蓉",36,"北京朝阳区",1);
2.缺省插入(推荐,不传入自增的主键)
格式:insert into 表名(字段名1,字段名2,....) values(值1,值2,....)
例: insert into student(name,age,address) values("马化腾",48,"深圳南山区");
3.插入多条数据
格式: insert into 表名 values(值1,值2,....),(值1,值2,....),(值1,值2,..)
例: insert into student values(0,"马云",45,"杭州",0),(0,"马克思",105,"俄罗斯",0),(0,"马赛克",99,"日本",0);
2.删除数据
格式: delete from 表名 where 条件
例: delete from student where age = 105;
delete from student;
(不加条件表示全部删除, 请谨慎使用)
3.修改数据
格式: update 表名 set 字段名1=值,字段名2=值,... where 条件;
update student set gender = 1 where name = "马赛克";
update student set age = 52,address="深圳" where name = "马化腾";
4.查看所有数据
格式: select * from 表名;
例: select * from student;
5.查询数据
1.查询格式
1.查询所有
格式: select * from 表名;
2.按条件查询
格式: select 字段名1,字段名2,... from 表名 where 条件;
1. select后跟的是查询结果需要显示的字段名
2. * 表示所有的字段名都要显示
3. from 后跟的是表名,表示从那个表查
4. where 后跟的是筛选条件
5. 可以使用 字段名 as 别名 来给字段名取个别名
例: 显示 name和age
select name,age from student;
select name as "姓名" from student;
2.条件中的运算
1.比较运算符
> 大于
< 小于
>= 大于等于
<= 小于等于
= 等于
!= 不等于
格式: select 字段名1,字段名2,... from 表名 where 字段名 比较运算符 值;
例: select * from student where age < 50;
2. 逻辑运算符
and 且
or 或者
not 非
格式: select 字段名1,字段名2,... from 表名 where 字段名 比较运算符 值 逻辑运算符 字段名 比较运算符 值 ...;
例: select * from student where age < 50 and age > 40;
select * from student where not (age < 50 and age > 40);
3.模糊运算符 like
% 表示多个任意字符
_ 表示一个任意字符
insert into student values(0,"李小璐",35,"杭州",0),(0,"白百合",25,"俄罗斯",0),(0,"贾乃绿",30,"日本",0);
insert into student values(0,"小辉辉",35,"杭州",0),(0,"小小俊",25,"俄罗斯",0),(0,"张小小",30,"日本",0);
需求: 匹配所有姓马的人
格式: select * from student where name like "马%";
格式: select * from student where name like "马_";
需求: 匹配名字中有 "小"字的
select * from student where name like "%小%";
4.范围查询
in (多个值 ) 判断一个值 是否是多个值中的一个
between 值1(包含) and 值2(包含) 判断一个值是否在 值1与值2之间
格式: select 字段名1,字段名2,... from 表名 where 字段名 范围运算符 值;
例: 找出 25或者45或者80或者 90
select * from student where age in (25,45,80,90);
需求:找出25 到 45之间的数据
select * from student where age between 25 and 45;
5. 空判断
is null 为空
is not null 不为空
格式: select 字段名1,字段名2,... from 表名 where 字段名 is null;
插入数据: insert into student(name,age) values("习大大",60);
例: select * from student where address is null;
select * from student where address is not null;
6.优先级
()
5.去除重复的值
格式: select distinct 字段名 from 表名 where 条件;
例: select distinct gender from student;
6.聚合函数
count(*) 求当前结果总共有多少条数据
sum(列名) 求列名对应列的 和
avg(列名) 求当前列的平均值
max(列名) 求当前列的最大值
min(列名) 求当前列的最小值
例: 求当前表总共有多少条数据?
select count(*) from student;
求年龄最小的?
select min(age) from student;
7.分组 group by
格式: select 字段名... from 表名 where 条件 group by 字段名
查看有多少中性别
例: select gender from student group by gender;
需求:统计 男生 和 女生 各有多少个
select gender,count(*) from student group by gender;
需求: 统计所有女生的个数?
例: select gender,count(*) from student group by gender having gender = 1;
where 查询条件, 是对select数据结果加的查询条件
having 查询条件 是对group by分组后结果加的查询条件
8. 排序
格式: select 字段名... from 表名 where 条件 order by 字段名1,字段名2...
例: 年龄小到大
select * from student order by age;
默认是从小到大排列
asc 从小到大
desc 从大到小
select * from student order by age asc;
9. 分页
格式: select 字段名... from 表名 where 条件 limit 起始值,多少条数据
起始值可以从 0 开始
例: select * from student limit 0,3;
-----------------------------------------------------------------------------------------------------------------
多表操作
1.表的设计
1.一对一
学生(学号)(1) ---- 学生档案(1)
2.一对多
班级(1) ------ 学生(多个)
3.多对多
学生(1) --- 课程(多)
课程(1) ---- 学生(多)
学生(多) --- 课程(多)
2.实现
一对多
1.设计表,创建表
1.表1- 班级表
create table class(
id int auto_increment primary key,
classname varchar(20) not null
);
2.表2- 学员表
create table student(
id int auto_increment primary key,
name varchar(20) not null,
age int,
classid int not null,
foreign key(classid) references class(id)
);
外键格式: foreign key(外键名) references 关联的表名(关联的字段名)
2.模拟插入数据
表1-插入
insert into class values(0,"szpy1704"),(0,"szpy1801"),(0,"szpy1802"),(0,"java1704"),(0,"java1801"),(0,"h5-1704");
表2-插入
insert into student values(0,"小俊俊",25,1);
insert into student values(0,"小贱贱",28,1);
<!-- 关联的外键没有时,插不进去 -->
**insert into student values(0,"金三胖胖",25,10);**
insert into student values(0,"小花花",38,3);
insert into student values(0,"金华火腿",25,5);
3.关联查询
#打印所有的学生,并对应班级名称?
select student.name,class.classname from student,class where student.classid = class.id;
#查出某个班的学生
select student.name,class.classname from student,class where student.classid = class.id and class.id = 1;
#打印出所有的信息?
select student.*,class.* from student,class where student.classid = class.id;
4.表的关联方式
1.内连接 inner join 或 join
select student.*,class.* from class inner join student on class.id = student.classid;
2.左外连接 left join 或 left out join
select class.*,student.* from class left join student on class.id = student.classid;
3.右外连接 right join 或者 right out join
select class.*,student.* from student right join class on class.id = student.classid;
多对多
1.设计表,创建表
1.表1 - 学生表- stu
create table stu(
id int auto_increment primary key,
name varchar(20)
);
.表2 - 课程表- course
create table course(
id int auto_increment primary key,
coursename varchar(20) not null,
teachername varchar(20)
);
3.表3 - 选修记录表 - sc
create table sc(
stuid int not null,
courseid int not null,
primary key(stuid,courseid),
foreign key(stuid) references stu(id),
foreign key(courseid) references course(id)
);
2.模拟插入多条数据
1.表1
insert into stu values(0,"金华火腿"),(0,"爱我中华"),(0,"花花世界"),(0,"采花大盗"),(0,"小贱贱");
2.表2
insert into course values(0,"java","金三胖胖"),(0,"C","习大大"),(0,"C#","奥巴马"),(0,"ps","仓老师");
3.表3
insert into sc values(1,4),(1,1),(5,2);
<!--注意:关联性, 唯一性 1,4 ,1,5 -->
insert into sc values(1,4);
3.关联查询
#查询出选修记录表对应的学生名和课程名?
select stu.*,course.*,sc.* from stu,course,sc where stu.id = sc.stuid and course.id = sc.courseid;
select stu.*,course.*,sc.* from stu inner join sc on stu.id = sc.stuid inner join course on course.id = sc.courseid;
select stu.*,course.*,sc.* from stu left join sc on stu.id = sc.stuid left join course on course.id = sc.courseid;