1、连接数据库

mysql -h ip地址 -P 端口号 -u 用户名 -p 密码

2、显示所有数据库

show databases;

3、创建数据库

create database 数据库名字 default charset=utf8;

4、使用数据库

use 数据库名字;

5、删除数据库

drop 数据库名字;

6、查询数据库下所有的表

use 数据库名;
show tables;

7、创建表

create table 表名(
	列名 类型,
	列名 类型,
	列名 类型
	);

8、常用的类型

数字	int,float,decimal
字符串	char,varchar,longtext
日期	date,datetime

9、删除表

drop table 表名

10、查看表的结构

desc 表名;

11、更改表的名称

rename table 原表名 to 新表名;

12、查看表的创建语句

show create table '表名';

13、约束

  1. 主键约束
  2. 非空约束
  3. 唯一约束
  4. 默认约束
  5. 外键约束
/*外键表*/
create table student(
	id int primary key,			
	name varchar(100) not null,
	idcard char(18) unique,
	addres varchar(100) default '郑州',
	gid int not null,
	foreign key(gid) references grade(id)
);

/*主键表*/
create table grade(
	id int primary key,
	name varchar(100) not null
);

14、描述表的信息

desc 表名

15、显示表的创建sql语句

show create table 表名

16、主键的生成策略

1、int 自动增长 auto_increment
2、字符串 uuid

create table grade(  
	id int auto_increment primary key,
	name varchar(100) not null
	);
	
create table grade2(  
	id char(36) primary key,
	name varchar(100) not null
	);
	
insert into grade(name) values('a');
insert into grade(name) values('b');

insert into grade2(id,name) values(uuid(),'a');
insert into grade2(id,name) values(uuid(),'b');

17、基本的增删改查

/*创建*/
create table student(
	id int auto_increment primary key,
	name varchar(100) not null,
	sex char(1) not null,
	address varchar(100) default '郑州',
	phone varchar(11),
	birthday date
	);

/*增*/
insert into student(name,sex,address,phone,birthday) values('老王','男','开封','11111111111','1998-2-2');

/*删*/
delete from student where id = 3;

/*改*/
update student set address='开封' where id = 5;
update student set sex='女',address='曼谷' where id = 5;

/*查*/
select * from student;
select name,phone from student;
select name 姓名,phone 电话 from student;

/*官方格式*/
UPDATE 
	student 
SET 
	sex='女',address='曼谷' 
WHERE 
	id = 5;

18、条件查询

/*1、查询所有EMP信息*/
select * from EMP;

/*2、查询所有job*/
select job from EMP;

/*3、去重:查询所有job*/
select distinct job from EMP;

/*4、去重:查询所有deptno,job的组合*/
select distinct deptno,job from EMP;

/*5、条件:查询工资大于2000的*/
select * from EMP where sal > 2000;

/*6、条件:查询工资大于2000的并且部门编号是10的*/
select * from EMP where sal > 2000 and deptno = 10;

/*7、条件:查询工资2000-3000之间的*/
select * from EMP where sal >= 2000 and sal <= 3000;
select * from EMP where sal between 2000 and 3000;

/*8、模糊:查询以S开头的员工信息*/
select * from EMP where ename like 'S%';

/*9、模糊:查询含有S的员工信息*/
select * from EMP where ename like '%S%';

/*10、模糊:查询含第三个字符是R的员工信息*/
select * from EMP where ename like '__R%';

/*11、范围:查询部门编号是10,20的员工信息*/
select * from EMP where (deptno = 10) or (deptno=20);
select * from EMP where deptno in (10,20);

/*12、空:查询没有有奖金的员工信息*/
select * from EMP where comm is null;

/*13、空:查询奖金大于400的员工信息*/
select * from EMP where comm > 400;

/*14、空:查询员工的编号,年薪  null参与的运算,结果还是null*/
select empno 编号,(sal+ifnull(comm,0))*12 年薪 from EMP;

/*15、聚合:统计员工的数量*/
select count(*) from EMP;

/*16、聚合:统计有奖金员工的数量*/
select count(*) from EMP where comm is not null;
select count(comm) from EMP;

/*17、聚合:最高的工资,最低的工资,平均工资,工资的总和*/
select max(sal),min(sal),avg(sal),sum(sal) from EMP;

/*分组需要注意:
	1、分组之后只能查询两种 
		1)、被分组的列  
		2)、聚合函数

	2、数据过滤
		1)、过滤的数据是分组之前,where
		2)、过滤的数据是分组之后,having

	3、关键词的顺序
		select
		from 
		where    分组之前的过滤
		group by
		having	 分组之后的过滤
		limit
*/

/*18、分组:每个部门的平均工资~~~*/
select deptno,avg(sal)
from EMP
group by deptno;

/*19、分组:每个部门员工工资高于1000的平均工资*/
select deptno,avg(sal)
from EMP
where sal > 1000
group by deptno;

/*20、分组:每个部门员工工资高于1000的平均工资,平均工资高于2000*/
select deptno,avg(sal)
from EMP
where sal > 1000
group by deptno
having avg(sal)>2000;

select deptno,avg(sal) avg_sal
from EMP
where sal > 1000
group by deptno
having avg_sal>2000;

/*21、分组:每个部门每个工种的最高工资*/
select deptno,job,max(sal)
from EMP
group by deptno,job;