(1)检查是否已装过数据库
mysql –-version
(2)连接数据库
mysql –u用户名 –p密码
mysql –uroot –p 回车
密文输入密码
(3)开启和关闭数据库服务
net start mysql
(注意:不一定是mysql, 你安装时候服务名是什么,就填什么,安装时,默认的是mysql57)
net stop mysql
注意:这两个命令,都需要使用管理员身份, 启动命令行工具
另外一种方式
启动管理员身份
我的电脑-右键-管理-服务-标准服务-Mysql-右键-关闭或开启
或者cmd里面services.msc打开服务管理窗口
注意:在登录mysql服务器之前,必须要保证, mysql服务已经启动,否则,无法登录.
退出mysql(前提是在已经登录进去了mysql):
exit
(1)查看数据库服务器上有哪些数据库.
show databases;
(2)创建一个数据库(不存在exists)
create database 数据库名;
create database if not exists 数据库名;
两者的区别:
语句1, 如果不存在该数据库,则会创建, 如果已经存在,再使用这个语句就会出错.
语句2,加上检查是否存在, 如果存在,就不会创建,但是语句不会报错.
(3)删除数据库
drop database 数据库名;
drop database if exists 数据库名
(4)选择使用某一个数据库(切换数据库)
use 数据库名;
(5)查看数据库中有哪些表(前提,必须使用了这个数据库)
show tables;
(6)查看某一张表的结构.
desc 表名;
(7)创建一张表.
create table 表名(
列名1 列1的类型(长度),
列名2 列2的类型(长度),
列名3 列3的类型(长度)
);
create table if not exists表名(
列名1 列1的类型(长度),
列名2 列2的类型(长度),
列名3 列3的类型(长度)
);
(8)修改表名
alter table 旧表名 rename 新表名
(9)修改字段名
alter table 表名 change 旧列名 新列名 新列类型
(10)修改字段类型
alter table 表名 modify 列名 新类型;
(11)添加字段
alter table 表名 add 新列名 新列类型;
(12)删除字段;
alter table 表名 drop 列名
(13)删除表
use 库名;
drop table 表名;
drop table 数据库名.表名;
(1)向表中插入数据
方式1
往表中插入多条完整的数据.
insert into 表名 values(列1的值, 列2的值.....),(列1的值,列2的值....);
往表中插入单条完整的数据
insert into 表名 value(列1的值, 列2的值.....);
方式2
往指定列中插入多条指定列的数据
insert into 表名(列1, 列2, 列3) values(值1, 值2, 值3),(值1, 值2, 值3);
往指定列中插入单条数据
insert into 表名(列1, 列2, 列3) value(值1, 值2, 值3);
(2)查询表中的数据
a, 查询表中所有的数据.
select * from 表名;
b, 查询指定列上的数据
select 列1,列2... from 表名;
(3)删除表中的数据
a,删除指定条件的数据.
delete from 表名 where 条件;
b,删除指定表中全部的数据.
delete from 表名;
(4)删除表
drop table 表名;
(5)更新表中的数据.
a,带限定条件
update 表名 set 列1 = 值1, 列2 = 值2 where 条件;
b,不带限定条件
update 表名 set 列1 = 值1;
全表更新某列为新值
select * from student where id = 5;
b,运算符条件
=, >, <, >=, <= , !=
注意:如果和空值进行比较的, 需要用 is null 或者is not null
c,复合条件and, or
select * from students where age >= 25 and age <= 30;
select * from students where age >= 25 or sex = “女”;
d,集合条件 in
select * from students where id in (1, 2, 4, 8);
select * from students where major in (“计算科学与技术”, “软件工程”, “web前端”);
e,范围条件 between and
select * from students where age between 20 and 25;
f,模糊查询 like % _
a,匹配0个或任意多个字符 %
匹配姓名姓张的数据:
select * from students where name like "张%";
匹配所有最后一个字是明的
select * from students where name like "%明";
匹配所有中间字带庆的
select * from students where name like "%庆%";
匹配一个任意字符”_”
返回的是姓名列只有一个字符的数据
select * from students where name like "_";
返回的是姓名列是两个字符的数据
select * from students where name like "__";
(2)去除相同列结果数据distinct
select distinct num from students;
注意:如果是查询多列数据, 比如查了4个, 那么4列的数据必须全部一样的时候,才会给你去重,否则,即便3个一样,另外一列不一样, 也会认为是不重复.
(3)查询结果排序desc降序 asc升序
a,单条件排序
select * from 表名 order by 依据排序的列名 升序(降序);
select * from students order by age desc
注意,asc是升序, desc 是降序
b,多条件排序
比如我先依据一个条件进行排序,如果数据相同, 我可以再依据第二个条件排.
select * from 表名 order by 第一个依据排序的列名 升序(降序), 第一个依据排序的列名 升序(降序);
select * from students order by age asc, score desc;
c,先根据某个条件查出结果, 再进行排序
比如:
select * from students where age > 20 order by age desc;
(3)限定查询结果limit
a, 返回结果前N条
select * from 表名 limit N;
b, 返回结果是,从第M条开始(从0开始), 取N条
select * from 表名 limit m,n;
c,也配合其他条件一起使用
select * from students where age > 20 order by score desc limit 2, 4;
(4)查询有多少条数据
a,全表中数据个数
select count(*) from 表名;
b,符合某个条件的数据个数
select count(*) from 表名 where 条件;
c,查询某列非空数据的条数
select count(列名) from 表名;
数据为null的不会计算
(5)获取指定列的最大值和最小值
a, 求某列最大值
select max(列名) from 表名;
b, 求某列最小值
select min(列名) from 表名;
(6)指定列求和
select sum(列名) from 表名;
(7)指定列求平均数
select avg(列名) from 表名;
(8)数据分组group by
a,数据分组
作用, 根据某列的内容,进行分组, 分组之后可以比如计算分组后每组的数据条数.
但是, 以某一列进行分组时, 行数在减少,就不要显示,分组外的数据, 否则会产生数据混乱
select 列名 from 表名 group by 列名.
select num, count(*) from 表名 group by num;
b,分组前过滤数据
select 列名 from 表名 where 条件 group by 列名;
select age from students where age > 20 group by age;
c,分组后过滤数据
select 列名 from 表名 group by 列名 having 条件;
select age from students group by age having age > 20;
(9)给查询的列起别名
如果要给返回的结果集.列名更加见名知意, 可以给列名起别名, 使用as
第一种
select 列名 as 别名, 列名 as 别名 from 表名
select num as 学号, name as 姓名 from 表名;
第二种
as 可以省略不写
select 列名 别名, 列名 别名 from 表名
select num 学号, name 姓名 from 表名;
第三种
如果别名是多个单词,必须要用””
select num “student num”, name 姓名 from 表名;
d, 函数后面,也可以起别名
select count(*) 学生个数 from 表名;
(10)子查询
把一个查询的结果作为下一个查询的条件
select * from students where age > (select avg(age) from students);
两个不相关的表,联合查数据, 可以使用union 语句, 但是使用union的前提的两个表查询的列数一致.
注意 union 会自动去重
select name, sex from student
union
select h_name, h_sex from hero;
如果不想数据去重, 使用union all
select name, sex from student
union all
select h_name, h_sex from hero;
从多张表中查询想要的数据.
如:
select * from stus, course;
但这种查询结果,会返回笛卡尔积, 数据重复. 需要判断关联值相等进行过滤
如:
select * from stus, course where stus.id = course.cid;
一旦查询的列重复, 需要使用表名区分.
select stu.id, name, course.id, course_name from stus, course where stus.id = course.cid;
三表联查实现查询3个表里3个列的数据:
select name, course_name,score from students, courses, scores
where students.id = scores.sid and courses.id = scores.cid;
select name, course_name, score fromstudents
join scores on students.id = scores.sid
join courses on courses.id = scores.cid;
两个表查询, 需要显示左表全部的数据(左连接):
select name, score from students
left join scores on students.id = scores.sid;
两个表查询, 需要显示右表全部的数据(右连接):
select name, score from students
right join scores on students.id = scores.sid;
对于students, courses, scores这个例子, 要查询结果里面显示全部students的数据:
select name, course_name, score from students
left join scores on students.id = scores.sid
left join courses on courses.id = scores.cid;
对于students, courses, scores这个例子, 要查询结果里面显示全部scores的数据:
select name, course_name, score from students
right join scores on students.id = scores.sid
left join courses on courses.id = scores.cid
也可以用另外一种方式 把第一连接的students和scores 交换顺序:
select name, course_name, score from scores
left join students on students.id = scores.sid
left join courses on courses.id = scores.cid;
所以说使用左连接的频率更高.
Mysql里没有全连接(full join), 如果要想students和scores 没有关联的数据都显示,就需要使用联合查询.
select name, score from students
left join scores on students.id = scores.sid
union
select name, score from students
right join scores on students.id = scores.sid;
1、约束的概念
作用:给数据加上一些限制条件.来保证数据表的完整性和一致性.
完整性:
比如说, 一个用户表里面要存储用户的相关信息, 但是用户提交的时候, 手机号没有填写, 这样直接插入输入库,就导致这条信息不够完整,有数据缺失, 为了保证数据完整,我就需要对重要的信息字段加上约束,保证完整性.
一致性:
比如多张表进行信息关联的时候,第一张表的某些数据需要和另外一张表信息进行关联, 如果两张表信息不一致,就到时数据混乱,所以需要有约束,来保证数据的一致性.
常见的约束的形式:
a,非空约束, not null
某些字段插入数据时, 要求必须有数据时,该列 约束需要被 限定为not null.
b,唯一性约束unique
输入某些列的数据,不想让它重复, 可以对该列限定为unique.
unique 对null 无效, null不代表有值,null 只是一种状态
c,主键约束primary key
主键约束的列就是既不能为null, 又是唯一的unique
unique not null 虽然和主键的作用一样,但是不能称为是主键
逻辑主键:
通常我们建的表的时候, 用设计一个列,并把他定义成主键, 这个列没有实际数据意义, 用去区分每一条数据, 这种列就叫做逻辑主键.
d,外键约束foreign key
外键的作用, 是用于和另外一张表的主键建立关联关系, 一旦一张表的某个字段被确定成为外键, 那么它的取值范围, 依赖于另外一张表已存在的数据.
声明某个字段为外键
foreign key(列名) reference 另外一张表表名(另外一张表的主键)
如:
foreign key(sid) reference stus(id)
一旦有列设置成为外键, 那么默认数据不能够删除或者更新, 如果需要删除或者更新, 需要对外键有如下几种声明:
A, on delete cascade 删除的时候, 相关联的数据会被一起删除
B, on delete set null 删除的时候, 相关联的数据设置的主键会被置空
C, on update cascade 更新的时候, 相关联的数据会被一起更新
D, on update set null 更新的时候, 相关联的数据设置的主键会被置空
示例:
create table course(
id int(10) foreign key auto_increment,
sid int(10),
course_name char(20) not null,
score double(5,2),
foreign key(sid) reference stus(id) on delete cascade
);
create table course(
id int(10) foreign key auto_increment,
sid int(10),
course_name char(20) not null,
score double(5,2),
foreign key(sid) reference stus(id) on delete set null
);
也可以一起写:
create table course(
id int(10) foreign key auto_increment,
sid int(10),
course_name char(20) not null,
score double(5,2),
foreign key(sid) reference stus(id) on update cascade on delete set null
);
E,默认值约束
给指定列设置默认值, 如果插入数据时, 不给内容, 则使用默认值
如:
create table stus(
id int(10) foreign key auto_increment,
name char(10) not null,
age int(10) not null,
sex char(1) default "男"
);
但插入时, 需要使用指定字段插入:
insert into stus(name, age) value(“张三”, 19);