以管理员身份运行mysql:【windows+命令提示符+鼠标右键管理员身份运行】
- 停止mysql服务:net stop mysql 【net stop mysqls】
- 启动mysql服务:net start mysql 【net start mysqls】
连接mysql数据库的几种方式:
- Mysql -uroot -p*** (***是密码)
- Mysql -h127.0.0.1 -uroot -proot
- 直接通过mysql的 安装后的doc窗口,点击开,直接输入密码。
- mysql -uroot -p 用户名密码方式登录mysql
- exit; 退出
- quit; 退出
- show databases; 显示当前mysql服务器中的所有数据库
- use 数据库名; 进入数据库
- show tables; 显示库中所有的表
怎么打开mysql?
- 以管理员身份打开cmd
- 启动mysql服务器:net start mysql
- 连接mysql服务:mysql -u root -p root
- 以管理员身份运行命令行:
打开mysql服务: net start mysql
关闭mysql服务: net stop mysql
- 显示所有的数据库:show databases;
- 创建数据库:create database 数据库名;
- 退出mysql: exit;
- 退出mysql: quit;
create(创建数据库) drop(删除数据库) alter(修改数据库的列)
数据类型:
- 整形:int (整数:1 6 101)
- 浮点型:float(单精度)、double(双精度) (小数: 3.3 7.9 101.2)
- 字符串类型:char和varchar (汉字 英文字母 你好 asdfgh)
- 日期类型:date(年月日)例子:2022-02-02
time(时分秒) 例子:21:21:21
datetime(年月日和时分秒)例子:2021-05-12 12:02:09
float(m,d)
单精度浮点型 8位精度(4字节) m总个数,d小数位
double(m,d)
双精度浮点型 16位精度(8字节) m总个数,d小数位
设一个字段定义为float(5,3),如果插入一个数123.45678,实际数据库里存的是123.457,但总个数还以实际为准,即6位。
创建表:
0. 创建数据库
1.使用数据库
2.在数据库里创建表。
- 使用数据库:use 数据库的名字;
- 创建表:
Create table 表名(列名1 数据类型(长度),
列名2 数据类型(长度),
列名 数据类型(长度) );
- 查看表:show tables;
- 查看表的详细字段(信息):DESC 表名; 【前提:使用数据库】
- 查看表的详细字段(信息):show columns from 表名 from 数据库名;
- 添加字段(表头):
添加一个字段:alter table 表名 add 字段名 字段类型;
添加多个字段:alter table 表名 add 字段名 字段类型,
add 字段名 字段类型…;
- 删除字段:
删除一个字段:alter table 表名 drop 字段名;
删除多个字段:alter table 表名 drop 字段名,
drop 字段名;
- 修改字段类型:alter table 表名 modify 字段名 要修改的数据类型;
- 修改字段名(和字段类型):
alter table 表名 change 原字段名 修改后字段名 修改之后字段类型;
- 删除表:drop table 数据表名;
- 修改表名:
第一种方法:rename table 旧数据表名 to 新数据表名;
第二种方法:alter table 旧表名 rename 【to】 新表名;
- 插入记录:【插入中文报错了:set names gbk;】
第一种方法:insert into 表名 (字段1,字段2,...字段n)
Values (值1,值2,...值n),(值1,值2,...值n);
第二种方法:insert into 表名
Set 字段1=值1,字段2=值2…;
例子:insert into student set sid=1,sname=”lulu”,ssex=”女”;
第三种方法:insert into 表名
Values (值1,值2,值3);
- 查看表的记录:select * from 表名;
- 修改单条记录的一个字段:
Update 表名 set 字段名=值 where 条件;
- 修改单条记录的多个字段:
Update 表名 set 字段1=值,字段2=值 where 条件;
- 删除记录:
Delete from 表名 where 条件;
- 删除记录:
两个条件同时满足:delete from 表名 where 条件1 and 条件2;
【条件:字段1=值】
两个条件满足一个就行:delete from 表名 where 条件1 or 条件2;
【条件:字段1=值】
- 查询指定字段对应的记录
Select 字段1,字段2,字段3 from 表名;
- 去重查询(筛除重复的记录):
Select distinct 列1,列2 from 表的名称; 【筛除列1和列2 同时 重复的值。】
- 列运算:
Select 字段1+字段2 from 表名;
- 比较运算符的应用:
比较运算符:> < >= <= = != <>
大于 小于 大于等于 小于等于 不等于
Select * from 表名 where 条件; 【条件:字段>100】
- 集合查询(in)
select * from 表名 where 字段 [not] in(元素1,元素2,...,元素n);
in(元素1,元素2,...,元素n)表示在这个集合里
not in(元素1,元素2,...,元素n)表示不在这个集合里
- 范围查询:
Select * from表名 where 字段 [not] between 取值1 and 取值2;
- 查询字段为空:
select 字段1,字段2 from 表名 where 字段 is null;
- 查询字段非空:
select 字段1,字段2 from 表名 where 字段 is not null;
- 添加主键:
第一种方法:
mysql> create table stu2(
-> id int(10) primary key,
-> name char(20),
-> age int(10));
第二种方法:
mysql> create table stu3(
-> id int(10),
-> name char(10),
-> age int(10),
-> primary key(id,name));
第三种方法:
alter table 表名 add primary key(字段);
- 自增键:auto_increment
注意事项:自增键必须与主键(primary key)搭配使用
mysql> create table stu4(
-> id int(10) primary key auto_increment,
-> name char(10),
-> age int(10));
- 删除主键:
提示:如果字段既是主键(primary key)又是自增键(auto_increment),想删除主键,必须先去掉自增之后才能删除主键。
- 判断有没有自增键(auto_increment)
- 有自增键:alter table 表名 modify 字段名 数据类型;
alter table 表名 drop primary key;
(3) 无自增键:alter table 表名 drop primary key;
36. 非空约束:
第一种方法:
mysql> create table s1(
-> id int(10) not null);
第二种方法:
alter table 表名 modify 字段名 数据类型 not null;
37.候选键:unique 候选键的值必须是唯一的(不能有重复值),且不能为空(NULL)
第一种方法:
create table s2(
-> id int(10) unique,
-> age int(10));
第二种方法:
alter table 表名 add unique(字段);
38.删除候选键:alter table 表名 drop index 候选键的字段名;
39. 默认约束:default 【当被default修饰的字段没有插记录时,会显示默认值】
第一种方法:
mysql> create table s3(
-> id int(10),
-> name char(10) default "qq");
第二种方法:
alter table 表名 modify 字段名 字段类型 default ’未知' ;
40.删除默认约束:
alter table 表名 modify 字段名 字段类型;
41.给表添加外键
主表:
mysql> create table dept(
-> deptno int(10) primary key auto_increment,
-> dname char(10));
从表:
mysql> create table emp(
-> id int(10) primary key auto_increment,
-> name char(10),
-> sex char(10),
-> deptno1 int(10), 【外键名 外键的数据类型要和主表的主键数据类型一致】
-> constraint wai foreign key(deptno1) references dept(deptno));
外键: 【自己起的名字】
-> constraint 外键名
-> foreign key(外键字段名) references 主表(主表里的主键)
42.升序排序:select * from 表名 order by 字段名 ASC;
43 降序排序:select * from 表名 order by 字段名 DESC;
44. 在指定的一个字段后面添加一个字段:
Alter table 表名 add 要添加的字段 字段类型 after 要跟随的字段名;
45.在指定的一个字段前面添加一个字段:
Alter table 表名 add 要添加的字段 字段类型 before 要跟随的字段名;
46.count函数
Select count(*) from 表名; 【查看表一共有几条记录】
Select count(字段) from 表名; 【查看这个字段有几个非空记录】
47.sum() 函数
Select sum(字段) from 表名; 【字段求和】
48. max() 函数
Select max(字段名) from 表名; 【最大值】
49. min() 函数
select min(字段名) from 表名; 【最小值】
50. avg() 平均值
Select avg(字段) from 表名; 【求字段的平均值】
Round() :四舍五入
Floor(): 向下取整
Ceil(): 向上取整
用法:Select round(avg(字段)) from 表名;
保留两位小数:select cast(avg(字段) as decimal(5,2)) from 表名;
51. ifnull函数
select 【字段名1,字段名2,】 ifnull(字段,设置字段为空显示的结果值) from 表名
解释:当查询字段值为空时,可以显示预先设置的结果
52. 模糊查询:
select * from 表名 where 字段 like "计%";
解析:"计%" 开始是“计”这个字, %表示后面有0或多个字符。
select * from 表名 where 字段 like "计_";
解析:_表示匹配1和字符。
题目:查询student表里的name字段,是姓张的学生有几个?
Select count(*) from student where name like “张%”;
53. 分组查询
select * from 表名 group by 字段名1; 把字段名1重复的值筛除。
select 字段,字段,group_concat(字段名1) from 表名;
解析:把字段名1对应的记录一行显示
合体:
select 字段,字段,字段,group_concat(字段1) from 表名 group by 字段2;
【字段2不一样的内容显示出来,字段2对应的字段1一行显示】
54. limit子句 【限制】
select * from 表名 order by 字段名 asc limit 3 ;
解析:升序排序输出3条记录
55. having子句【和where基本一致,区别:group by后跟having,不能跟where】
select 字段1,字段2 from 表名 group by 字段 having 条件;
56. 合并结果集:union
select 字段名 from 表1的名字
union
select 字段名 from 表2的名字;
解析:合并后将所有结果合并到了一起,并去除了重复值。
注意:两个表可以是一样的字段名链接,也可以是不一样的字段名链接(name和id也可以的)
57. 合并结果集:union all
select 字段名 from 表1的名字
union all
select 字段名 from 表2的名字;
注意:1.重复的记录也会显示
58. 内连接查询:
Select 字段1,字段2 from 表1,表2 where 表1.字段=表2.字段;
解析:表1.字段 != 表2.字段 的记录被舍弃。
59. 左外连接查询
mysql> select 字段,字段,字段 from 表1名
-> left join 表2名
-> on biao2.name=biao1.name;
解析:满足条件的表1和表2的拼接字段会有值,不满足条件时,表2的字段为null,可以是表1和表2的混合字段
60. 添加主键和自增键
alter table 表名change 旧字段名 新字段名 数据类型(长度) primary key auto_increment;
61. 右外连接查询
mysql> select 字段,字段,字段 from 表1名
-> right join 表2名
-> on biao2.name=biao1.name;
62. 复合条件查询
select 字段名 from 表1名,表2名
where 表1.字段名=表2.字段名 and(or) 条件;
63. 给表起别名(小名):
第一种方法:
select 字段名 from 表名 表的别名;
第二种方法:
select 字段名 from 表名 as 表的别名;
64. 把两个表字段名里记录相同的数据筛选出来
select * from 表名 where 字段名 in(select 同样字段名 from 表名);
65.子查询里的any关键字
当biao1里的字段1 < 任意一个biao2里的字段2时返回的值
select 字段名 from 表1 where 字段1< any(select 字段2 from 表2);
66. 子查询里的:all关键字
当biao1里的字段1 < 所有biao2里的字段2时返回的值
select 字段名 from 表1 where 字段1< all(select 字段2 from 表2);
//distinct //去重
//between...and... //范围
// ordrer by 列名 asc //升序排序
// ordrer by 列名 DESC //降序排序
// group by //分组查询
//--聚合函数--
// count()计数
//max()最大值
//min ()最小值 //null不参与函数计算
//sum()求和
//avg()平均值
//group_concat 分组查询显示名字
//=============
// 执行顺序 where -->聚合函数-->having //
//
分页查询 0起始索引 x查询条数
//limit 0,x // 计算公式:起始索引=(当前页码-1)*每页显示条数
//---约束---
// not null //非空约束
//unique //唯一约束
//primary key //主键约束
//check //检查约束
//default //默认约束
//foreign key //外键约束