mysql DDL,DML


mysql 数据库DDL,DML    
1 DDL 语法
DDL是数据定义语言的缩写 对数据库 表 进行操作
create  drop  alter 
创建          删除     修改
 
1创建数据库
create datebase dbname; 
 
① 使用数据库
use dbname  
 
②查看所有数据库
show datebases;
 
③查看数据库详细信息
show create database dbname ;
2删除数据库
drop datebase dbname;
 
3 创建表 
create table tablename (
                        clo_name clo_type,
                        ...
                         );
 
4 删除表
drop table tablename;
 
5查看表详细信息
方法①
desc tabelname ;
方法②
show create tablename \G;   
 
6修改表属性
①修改表名 
rename tabename to newtablename ; 
②修改表的字段类型
alter tabel tablename modify clo_name clo_type 
③修改表的字段名,和同时修改表的字段属性
alter table tablename change clo_name clo_new_name clo_type
#注意 change modify 都可以修改字段类型,但是change 要写两次字段名,而modify 不能修改字段名,只能改字段属性
#并且使用change 的改字段名称的时候还必须带上字段属性 例如 alter table tablename change clo_name new_clo_name clo_type
 
④增加表的字段
alter table tablenam add column clo_name clo_type after|first 
 

表的字段操作
DML语言
 
1增加字段 [field:字段]
insert into tablename (field ...) values();
 
2删除字段(记录)
delete form tanlename drop field where ....;
 
3修改字段(记录)
update tablename set field  where fleid ; 
 
4查询字段(记录)
①查询所有字段值
select * from tablename; 
 
②查询部分字段 值
select field ,..  from tablename
 
③查询字段的时候对值筛选(条件查询)
select * from tablename where + [=,>,<,>=,<=,!=]等判断语句
 
④排序和限制
排序 [order by:关键字排序,默认升序]   [desc : 降序]   [asc :升序] 
select *from tablename order by  field  [desc |asc],field 2 [desc |asc] ; 假如出现同值的情况就会按照第二个值来排序
 
限制[limit:限制]
select * from tablename order by field1 [desc|asc].. field n [desc|asc] limit n :  偏移量 限制显示多少记录
 
⑤聚合 对记录的操作函数 fun_name :count() ,max(),min(),sum()
select field  fun_name from tablename ...
fun_name 函数:
    count()计数, max()最大值 ,min()最小值 ,sum()求和
语法:
select [field1,field2...fieldn] fun_name from tablename [where + ] group by + [with rollup] + [having]
 
group by 分组
 
with rollup :可选语法是否对聚合后的结果进行汇总  
例如:统计各部门人数又要统计总人数
        select  deptno ,count(1) from emp group by deptno with rollup 
 
having :对聚合后的结果再次筛选 
 
 
⑥表连接
内连接:两个不同表之间的连接,选出两个表中匹配的记录
select a_table_field ,b_table_field from a_table,b_table where ...a与b表的公共关连 
外连接:左连接,右连接
               左连接                                               右连接                      
 
 
⑦子查询
两张表中,一张表的所查询结果后,在另外一张表中匹配查询结果
子查询和连接都可以进行查询
例如
假设a_table 和 b_table 都有共同的字段 id 字段 要查询a表和b表的都有id的记录
 
写成连接
连接:
select a_tabel.* from a_tabel,b_table where a_table.id = b_table.id 
 
##   a_table.*----->表示  结果显示所有a_table 的字段和匹配后的结果
 
写成子查询
子查询:
select id from a_table where id in (select id from b_table)
 
⑧集合 union ,union all  [union:集合]
将两个表的数据查询出后的一个集合【union all】,或者去重集合 【union】
假设 a , b 表都有 name 字段
 
语法:
select name from a_table
union [union all]
select name from b_table;  
 
 

猜你喜欢

转载自www.cnblogs.com/pianzzq/p/11566591.html