MySQL 數據庫查常用命令總結

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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章