Mysql基础(一):SQL基础

 

Mysql基础(一):SQL基础


一、SQL分类
SQL语句可以分为以下三类
1)、DDL(Data Definition Languages)数据定义语言,如create、drop、alter等
2)、DML(Data Manipulation Languages)数据操纵语句,如insert、update、delete、select等
3)、DCL(Data Control Languages)数据控制语句,如grant revoke等
二、DDL 基础
1)、创建数据库
mysql>create database dbname;

2)、打开数据库
mysql>\u mysql    
mysql>use mysql;        (二条命令效果一样)
3)、显示数据表
mysql>show tables;
4)、删除数据库
mysql>drop database dbname;

5)、创建表
mysql>create table tablename(column_name column_type);
column_name 表示字段名,如id
column_type  表示字段类型,如int
6)、查看表结构
mysql>desc tablename;

7)、查看全面表定义信息
mysql>show creae table tablename\G; \G表示数据纵向显示

8)、删除表
mysql>drop table tablename;

9)、修改表类型
alter table tablename modiry column_definition [first|after col_name]
first 让修改的表字段显示在表的第一行
after 让修改表的字段显示在表after后边字段的后面
如修改test表中的id字段为varchar(20),命令如下
mysql>alter table test modify varchar(20);
10)、增加表字段
alter table tablename add [column] column_definition [first | after col_name]

如对test表中增加usename 字段,字段类型为 int(20),命令如下:
mysql>alter table test add useranme int(20);

11)、删除表字段
alter table tablename drop [column] col_name

如删除test表中的id字段,命令如下:
mysql>alter table test drop id;

12)、字段改名
alter table tablename change [column] old_col_name column_definition [first|after col_name]
如给test表username 修改为user,命令如下
 
mysql>alter table test change username user;

13)、 更改表名
alter table tablename rename [to] new_tablename
如,修改表test名为test_new.命令如下:
mysql>alter table test rename  test_new;
三、DML语句
1)、插入记录
insert into tablename(field1,field2,......fieldn) values(values1,values2,......valuesn);
如向test表中插入以下记录:id为50,username为wb,命令如下:
mysql>insert into test (id,username) values(50,'wb');
2)、更新记录
update tablename set field1=valuel,field2,=value2,......fieldn=valuen [where condition]
如将test表中id为50更新为100,命令如下:
mysql>update table test set id=100 where id=50;
注:如果update不带where条件,则把所有表中的id修改为100
3)、删除记录
delete from tablename [where condition]
如将test表中的username为wb的记录删除,命令如下:
mysql>delete from test where username='wb';
注:如果delete不带where条件,则把test表中的所有记录删除
4)、查询记录
select * from tablename [where condition]
4.1)、查询不重复记录
如查询test表中username为wb记录不重复的记录,命令如下:
mysql>select distinct username from test;
利用 distinct  关键字来实现
4.2)、条件查询
where 后面的条件是一个字段的=比较,分别有=、>、<、>=、<=、!=等比较运算符.
条件还可使用or、and等逻辑运算符进行多条件查询.
4.3)、排序和限制
select * from tablename [where condition] [order by field1 [desc|asc],field2 [desc|asc],.....]
 

desc 是按降充排列

 

asc   是按升充排列,不带排序关键字,默认是按升充排列

如将test表中id按高到低进行排列,命令如下:
mysql>select * from test order by id desc;
如将test表中的id按高到低进行排列,并取前三条记录,命令如下:
mysql>select * from test order by id desc limit 3;
如将test表中的id按高到低进行排列,从第二条记录开始,并取前三条记录,命令如下:
mysql>select * from test order by id desc limit 1,3;
4.4)、聚合
select [field1,field2....] fun_name
from tablename
[where where_contition]
[group by field1,field2...
[with rollup]]
[having where_contition]
sum ()求和函数
count(*)记录数
max()最大
min()最小
如要统计test表中学生人数,命令如下:
mysql>select count(1) from test;
如要统计test表中各个班级的人数,命令如下:
mysql>select class count(1) from test group by class;
如既要统计各班级人数,又要统计总人数,命令如下:
mysql>select class count(1) from test group by class with rollup;
4.5)、表连接
如查询test表中学生id 等于class表中的id的记录,命令如下:
mysql>select * from test ,class where test.id=class.id;
4.6)、子查询
子查询主要包括 in、not in、=、!=、exists、not exists等
如查询test表中所有id在class表中的所有记录,命令如下:
mysql>select * from test where id in(select id from class);
4.7)、记录联合
select * from t1
union|union all
select * from t2
.......
union|union all
select * from tn;
union 和union all的主要区别是union all 是把结果集直接合并在一起,而union 是将unon all后的结果进行一次distinct ,去除重复记录后的结果。
如将test表和class表中的id号集合显示
mysql>select id from test union all select id from class;
 

四、DCL语句

DCL语句主要是DBA用来管理系统中对像权限时使用.
如创建一个用户test,具有对test数据库中所有表的select/insert权限,密码为123456,命令如下:
mysql>grant select,insert on test to 'test@localhost' identified by '123456';
如将用户test权限变更,收回insert权限,只能对数据库test进行select操作
mysql>reovke insert on test.* from 'test@localhost';


你可能感兴趣的:(mysql,数据库,职场,休闲)