MYSQL数据库2

3.2.8创建数据表及表相关命令

(1)create table 表名

(

       字段1 字段类型,

       字段2 字段类型,

       字段3 字段类型……

)

(2)show tables;展示当前数据库中所有的数据表

(3)show create table 数据表。展示表的创建过程

(4)desc table;可以查看表结构

3.2.9select查询

(1)查询数据表中全部的行和列

select * from 表名

select col1,col2.... from 表名

(2)查询部分表的部分列

select col1,col3 from 表名

(3)给查询出来的数据列设置别名

select col1 as “别名1”,col2 as ‘别名2’…from 表名

      select col1 ‘别名1’,col2 ‘别名2’….from 表名

(4)DISTINCT关键字的使用

       作用:消除查询结果中的重复数据

       语法:select distinct col from 表名

       注意:要求所有的字段都相同才会去重

(5)LIMIT关键字的使用

       作用:指定结果的显示范围

       语法:

               select * from 表名 limit m,n

                   m:起始的位置

                   n:显示的数量

               select * from 表名 limit m

                   m:从第一条开始共显示m条数据

3.2.10insert插入数据

(1)所有列都插入值

语法:insert into 表名 values(v1,v2,v3….)

特点:列值同数,列值同序

(2)为指定列插入值

语法:insert into (col1,col2,col3) values(v1,v2,v3)

特点:指定顺序,列值对应

(3)一次性插入多条记录

语法:insert into 表名(co1,col2,col3…)values(v1,v2,v3),(v1,v2,v3),(v1,v3,v3)…..

3.2.11update修改数据

(1)修改指定数据

语法:update 表名 set column=xxx where expression

(2)修改全部数据

语法:update 表名 set column=xxx

3.2.12删除数据

(1)使用delete命令删除数据

语法:delete from 表名 where expression

(2)使用truncate命令删除数据

语法:truncate 表名

(3)逻辑删除

给要删除的数据做标记,实际并未删除

(4)delete和truncate的区别

①Delete语句删除数据,自动编号没有恢复到默认值。但是truncate重新设置了自动编号

通过truncate语句删除数据表数据,不能根据条件删除,而是一次性删除,delete语句可以根据条件进行删除

③truncate在清空表中数据的时候,速度要比delete语句快的多

3.2.13对列进行增、删、改、查

(1)增加一列

       alter table table_name add col_name type

(2)删除一列

       alter table table_name drop (column) col_name 注:column可有可无

(3)修改列的数据类型

       alter table table_name modify col_name type

(4)修改列的数据类型并且改名

       alter table table_name change old_colname new_colname type

3.2.14数据库常用约束

(1)添加主键约束

语法:alter table table_name add constraint PK_col_name primary key(col_name)

(2)添加外键约束

语法:alter table table_name add constraint FK_con_name foreign key(col_name) references table(col_name)

(3)添加检查约束(在mysql中检查约束不起作用)

语法:alter table table_name add constraint CK_con_name check(expression)

(4)添加默认约束

语法:alter table table_name modify col_name type default value

(5)添加自动增长

语法:alter table table_name modify col_name type auto_increment

注意:添加此约束时,被添加的列必须为主键且列中必须没有0

(6)添加非空约束

语法:alter table table_name modify col_name type not null

(7)添加唯一约束

语法:alter table table_name add constraint UQ_col_name unique(col_name)

3.2.15条件查询

(1)普通条件查询

语法:select * from table_name where expression

(2)模糊查询

语法:

①between….and….

范围查询:  in 、or

③like 通配符 %和_,注意:如果要查找真正的%,需要用\对%进行转义

(3)查询空值的运算符

is null

3.2.16数据排序

作用:对查询出的数据进行升序或降序排列

语法:select * from table_name order by col_name asc/desc

           select * from table_name order by col_name asc/desc,col_name asc/desc

3.2.17数据分组

语法:select col1..col2.. from table_name Group by col

注意:如果使用了group by分组,那么select不允许出现其他列,除非这些列包含在分组中。

3.2.18聚合函数

(1)作用:对多条数据做统计功能

(2)注意:在使用聚合函数后,select后不允许出现其他列,除非这些列包含在分组中或者聚合函数中

(3)常用聚合函数:

3.2.19Having语句

作用:having为group by之后得到数据进行进一步的筛选

类似于select 和 where的关系。Where为select前的数据进行进一步的筛选。Having为group by后的数据进行筛选

3.2.20sql语句执行顺序

(1)from 表名

(2)where

(3)group by

(4)select distinct *

(5)having

(6)order by

(7)limit

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