总结-《My SQL数据库(享精品公开课)》

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a786150017/article/details/78926905

数据库的操作

创建数据库和删除数据库

create database 库名    
delete database 库名
use 库名    #切换到某数据库  

数据表的操作

1.创建数据表

主键约束primary key、外键约束references 表B(表B_列名)、检查约束check(条件)、默认约束default和自增列auto_increment

create table 表A
(
列名1 varchar(20) not null primary key auto_increment,
列名2 char(11) not null references 表B(表B_列名),
列名3 int check(列名3 > 0),
列名4 int default '嘻嘻',
...
)

2.编辑数据表

(1)查看表的基本结构

describe 表名
des 表名
show create table 表名 \G

(2)修改表

修改表名rename to、字段名change、字段数据类型modify、增加字段、删除字段drop

alter table old_name rename to new_name     #修改表名
alter table table_name change old_col new_col  newcol_type    #修改字段名
alter table table_name modify col_name new_type    #修改字段数据类型
alter table table_name add new_col new_type    #增加字段
alter table table_name drop col_name    #删除字段

(3)为列添加约束add constraint con_name

主键约束、外键约束、检查约束、默认约束和自增列

扫描二维码关注公众号,回复: 3466206 查看本文章

alter table table_name add constraint con_name primary key(col_name)  
alter table 表A add constraint con_name foreign key(A_列) references 表B(B_列)  
alter table table_name add constraint con_name check(条件)  
alter table table_name alter col_name set default value  
alter table table_name modify column col_name col_type col_NOTNULL auto_increment 

(4)删除表

drop table  表A,表B
#删除有关联的数据表
alter table 表A drop foreign key con_name
drop table 表A,表B

数据的操作

1.插入数据

insert [into] 表名[ (列名1,列名2,...列名n)  ] values(V11,V12...V1n),(V21,V22...V2n)...      #列插入值

2.修改数据

update 表名 set {列名= 表达式}[,...n] where 条件表达式	#{列名= 表达式}[,...n]表示{列名 = 表达式}可以出现n次

3.删除数据

delete from 表名 where 条件    #删除数据表中的数据,而不是删除数据表
truncate table 表名    #不能添加where;一次性清空所有数据

查询和排序的操作

1.简单的查询语句

select * from 表名
select 列1,列2 from表名
select 列 [as] 别名 from表名
select distinct 列  from 表名    #从查询结果集中消除重复行
select * from users limit 2,3    #[起始位置的索引(从0开始)],长度

2.条件查询语句

select * from 表名 where 条件
select * from 表名 where 列 between A and B    #模糊查询(A≤列≤B)
select * from 表名 where 列 like 通配符条件    #注意:通配符用like
select * from 表名 where 列 is null/not null    #注意:是否为空值用is

【通配符】一个字符_    任意字符%    指定范围内字符[]    不在括号内[^]

3.对查询结果排序

select * from 表名
order by 列名1 [asc|desc],列名2 [asc|desc]

4.连接查询

内连接(两张表地位平等)、外连接(两张表地位不相等,其中一张是基础表-数据必须出现)

select 列1,列2 from 表1,表2 where 表1.列 = 表2.列    #隐式内连接
select 列1,列2 from 表1 inner join 表2 on 表1.列 = 表2.列    #显示内连接
#外连接
select 列1,列2  
from 表1 left|right join 表2  
on 表1.列 = 表2.列

5.子查询

in关键字、exists关键字

select * from SCORES where GNO in/not in  
(select GNO from GAMES where GTYPE ='棋牌' )   #查询棋牌类游戏的编号  

select * from SCORES  
where exits (select * from USERS where USER_NAME ='孙悟空')     #如果存在昵称为‘孙悟空’,则查询分数表中的数据

6.联合查询

select语句 
union [all] 
select语句 ... 

汇总分组操作

1.聚合函数:sum  avg  min  max  count

2.在结果集中分组:group by

3.筛选分组结果:having    order by

select   
sum(SCORE)  as ‘总分数’,  
avg(SCORE)  as ‘平均分数’,    
from SCORES 
where USER_QQ = 12302    #第一层过滤
group by USER_QQ    #划分为多个分组
having avg(SCORE) > 3000    #筛选分组
order by avg(SCORE),desc    #排序

【章节8】常用函数:日期和时间函数、数学函数、字符串函数和系统函数:略













猜你喜欢

转载自blog.csdn.net/a786150017/article/details/78926905
今日推荐