1.net start mysql 启动mysql服务
2.net stop mysql 关闭mysql服务
3.mysql -h 127.0.0.1 -u root -p 打开mysql输入密码
查看当前所有的数据库 show databases
创建数据库 create database database_name
创建中文字符集的数据库 create database database_name charset utf8 collote utf8_general_ci
使用这个数据库 use database_name
查看当前所有的表 show tables
新建数据表 create table table_name
修改表名 alert table <旧表名> rename <新表名>
修改字段数据类型 alert table <表名> modify <字段名> <数据类型>
修改字段名 alert table <表名> change <旧字段名> <新字段名> <数据类型>
添加字段 alert table <表名> add <新字段名><数据类型>
表的第一列添加字段 alert table <表名> add <字段名> <数据类型> first
表的指定列添加字段 alert table <表名> add <字段名> <数据类型>after name
修改字段位置 alert table <表名> add <字段名> <数据类型>first
修改字段位置在指定列之后 alert table <表名> add <字段名> <数据类型>after name
删除数据库 drop database 数据库名称
删除数据表 drop table 数据表名称
删除数据表中的内容 delete from table_name where 条件
删除字段 alert table table_name drop 字段名
insert into table_name (column_list) values (values_list);
insert into student (id,name,age ,sex) values (20190321,‘李阳’,24,‘男’);
同时插入多条数据
insert into table_name (column_list) values (values_list),(values_list),(values_list)
insert into student (name,age) values (‘李阳’,24),(‘李阳’,24),‘李阳’,24);
将查询结果插入表中
insert into table_name (column_list) select column_list from table_name where 条件
insert into student (di,name,age,sex) select id,name,age,sex from person
将多表查询结果插入表中
insert into student (id,name,age,sex)
select id,name,age,sex from person 1
union all
select id,name,age,sex from person 2
update table_name set column_name 1=value 1,
column_name 2=value 2,
column_name 3=value 3
where 条件语句
update table_name set age=15,
name='李阳',
sex='男'
where id=11
delete from table_name where 条件语句
delete from student where id=11;
delete from student where age between 15 and 20;
select * from table_name
select 字段1,字段2 ,字段3 from student where 条件语句
select id,name,age,sex from student where class=2;
select 字段1,字段2 ,字段3 from student where 字段 in(范围) order by 字段
select id,name,age,sex from student where class in (2,3) order by name;
select id,name,age,sex from student where class not in (2,3) order by name;
select id,name,age,sex from student where class between 2 and 5;
select id,name,age,sex from student where name like “李%”;
select id,name,age,sex from student where name like “李%阳”;
@is not null
select id,name,age,sex from student where name is not null;
@is null
select id,name,age,sex from student where name is null;
select name,age,sex from student where id=100 and id=200;
select name,age,sex from student where id=100 or id=200;
select distinct name from student;
@单列排序
select name,age,sex from student order by name;
@多列排序
select name,age,sex from student order by name and age;
@指定排序方向
select name,age,sex from student order by name desc;
select name,age,sex from student order by age asc;
指定多列不同顺序排列
select name,age,sex from student order by age asc,name desc;
分组查询(group by having)
@group by
select name,age,sex from student group by age;
@having过滤
select name,age,sex from student group by sex having age >22;
@with rollup
select name,age,sex from student group by sex with rollup;
@order by
select
ordernumber,
SUM(quantityOrdered)ASitemsCount,
SUM(priceeach*quantityOrdered)AStotal
FROM orderdetails
GROUPBY ordernumber
HAVING total>55000
select * from student limit 4 查询前四行
select * from student limit 4 ,3 从第四行开始向下三行
聚合函数查询
连接查询
as 字段别名
对mysql数据库新建一个user:
grant all on test.* to ‘alex’@‘%’ indentified by ‘root’;
查看新建user的权限
show grant for alex
primary key
constraint 外键名 foreign key 字段名 references 主表名 主键列
not null 非空约束
unique 唯一性约束
default 默认值约束