数据库命令总结

来源:互联网 发布:java invoke可变参数 编辑:程序博客网 时间:2024/06/14 13:57

                                         命令总结

修改字符集

set character_set_client=gb2312;

set character_set_results=gb2312;

 

 

1、创建数据库

create database 数据库名;

create database if not exists数据库名;(如果存在用 if not exists)

 

2、显示所有数据库

show databases;

 

3、显示创建数据库的定义信息

show create database数据库名;

 

4、修改数据库,只能修改参数 

alter database 数据库名 character set gb2312

collate gb2312_chinese_ci;

 

5、删除数据库(永久的删除)

drop database 数据库名;

drop database if exists数据库名;

 

5、创建表(先打开一个库)

use 库名;

create table 表名

(字段名1类型,

 字段名2类型,

 ……);

 

create table if not exists表名

(字段名1类型,

 字段名2类型,

 ……);

 

6、查看表结构

desc 表名;

 

7、显示当前数据库有哪些表

show tables;

 

8、修改表的结构——增加字段

alter table 表名

add 字段名,类型;

 

9、修改旧字段

alter table 表名

modify 字段名新类型;

 

10、删除字段

alter table 表名

drop 字段名;

 

11、修改列名

alter table 表名

change 旧的列名新的列名新列名的类型;

12、修改表的名

rename table 旧表名 to 新表名;

 

13、复制表

create table 新表 like 就表;

 

14、删除表

drop table 表名;

drop table if exists表名;

 

15、插入表数据(——插入的值和字段名相应)

insert into 表名(字段名1,字段名2,字段名3,字段名4,……)

values(1,2,3,4,……);

 

insert into 表名 values(……);16、删除表记录

delete from 表名——删除表中所有的记录,此表变成一个空表;

 

delete from 表名

where 条件——删除表中符合条件的记录;

 

比较:drop table表名——删除整张表,结构和记录;

truncate table 表名——快速删除表中的所有记录,无法恢复;

 

17、修改表数据

update 表名 set字段名1=新值1,……

where 条件;

 

18、查看插入后的表

select * from 表名;

 

19、数据库的查询——选择列,查看所有字段

select * from 表名;

 

20、数据库的查询——选择列,查看部分字段

select 字段名1,字段名2,…… from表名;

 

21、定义列别名(给列加标题)

select  字段名 [as]列标题;

select  字段名 [as] ' 标题';——如果列标题用空格隔开,用单引号。

 

22、替换查询结果中的数据

select 字段名1,字段名2,case

when 条件1 then表达式1

when 条件2 then表达式2

when 字段名 is not null then表达式3

……

end as 列标题 from表名;

 

23、计算列值

select 表达式 from表名;

select 学号,总学分+5 as新学分 from 表名;

 

24、消除结果集中的重复行

select distinct 字段名 from 表名;

 

25、聚合函数

select count(*) as 新列名 from 表名;——统计记录人数

select count(字段名) from表名;——统计字段中有值的记录个数(不考虑空值)

select count(distinct字段名) from 表名;去掉重复值后的计算个数

select max(字段名) as新列名 from 表名;显示最大值

select min(字段名) as新列名 from 表名;显示最小值

select avg(字段名) as新列名 from 表名;显示平均值

……

 

26、查询数据源

from 表名;

 

27、全连接

select 字段名1,字段名2,……

from 表名1,表名2,……

where 等值连接;

 

内连接

select 字段名1,字段名2,……

from 表名1 [inner] join表名2

on 等值连接

where 条件;

 

28、查询的条件

where 条件

(1)比较运算:<>>=<=<=>!=<>=;

(2)匹配模式:字段名 like '%_';

(3)范围比较:between in

select 字段名 from表名

where 字段名 between条件 and 条件;

 

select 字段名 from表名

where 字段名 in(条件,条件,……);

 

(4)空值比较

select * from 表名

where 字段名 is not null;

 

29order by排序(默认升序)

select 字段名 from表名

where 表达式

order by 字段名;

 

30group by对列进行分组

select 字段名 from表名

where 表达式

group by 字段名;

 

31having子句对列进行分组后进行选择

select 字段名 from表名

where 表达式

group by 字段名

having 条件;

 

32union子句将结果组合到一个结果集中

select 字段名 from表名

where 表达式

union

select 字段名 from表名

where 表达式;

 

 

33、子查询

select 字段名 from表名

where 字段名 in

(select 字段名 from表名);

 

34、创建视图

create view 视图名

as

select 字段名 from表名

where 条件;

create or replace view试图名

as

select 字段名 from表名

where 条件;

 

35、查看视图

show tables;

select * from 视图名;

 

36with check option:限定通过视图修改的数据,应该满足视图定义的条件,

也就是通过视图的修改,修改后的数据会出现在视图中。

 

37、插入数据

insert into 视图名 values();

38、修改数据

update 视图名 set字段名=新值

where 条件;

39、删除视图

drop view 视图名;

 

删除视图记录

delete from 视图名

where 条件;

 

40、修改视图定义

alter view 视图名

as

select 语句;

 

41、创建索引

create index 索引名

on 表名(字段名,……);——表已经建好

 

alter table 表名 add index索引名(字段名,……);

 

create table 表名

(

字段名类型,

……

index  索引名(字段名,……)——创建表的时候创建索引

);

 

42、查看索引

show index from 表名;

43、删除索引

drop index 索引名 on表名;

 

原创粉丝点击