数据库总结

数据库:

增:create database 数据库名称;

删:drop database 数据库名称;

查:show databases;

进入库:use 数据库名;

 

数据表:

增:create table 表名(

id  int auto_increment primary key,

name varchar(32)  not null  default '',

pwd  char(32)  not null  default ''

constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id))   #外键

)engine=Innodb charset=utf8;  

删:drop table 表名

改:

修改字段:
alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');
新增字段:
alter table 表名(t3) add 新列(pwd char(32) not null default '');
删除字段:
alter table 表名(t3) drop 列名(pwd);

查:show tables;

查看表结构:desc 表名

查看表的创建过程: show create table 表名

 

数据行:

增:insert into  t3 (id, name) values (1, '你好');  增加单个

insert into t3 (id, name) values ('lxxx', 12), ('xxxxx', 13), ('xxxxxx', 13); 增加多个

insert into t1 (name, age) select name, age from t2; 批量增加

删:

delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID

delete from 表名(t3) where name = 'xxxxx';

truncate 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始

truncate 速度快

改:

update t3 set username='zekai';    # 全部改

update t3 set username='xxxx' where id=3;   # 改某一行

update t3 set username='xxxx', pwd='xxxxx' where id=3;  # 改某一行的某个数据

 

外键:constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),

 

唯一索引:unique()目的:加速查找,不能重复

查:

select * from 表名;查看去全部

a. where + 条件 查询 限制查询条件

select * from 表名 where id >10;

b. between  and 闭区间查询

select * from 表名 where 列名 bentween   and 

c. in 在某一集合中

select * from 表名 where 列名 in (集合)

d. 通配符

select * from 表名 where 列名 like "ale%"; 匹配以某某开头的所有

select * from 表名 where 列名 like "ale_";  匹配以某某开头的一个字符

e. 限制取几条

select * from 表名 limit 索引偏移量,取出多少条数据;

f. 排序

select * from 表名 order by 列名 desc/asc;

多列的时候逗号隔开,表示 如果前一列的值相等的话, 会按照后一列的值进行进一步的排序

g. 分组

select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;

having 可以多group分组后的数据进行二次删选

where 和 having的区别:
1). having与where类似,可筛选数据
2). where针对表中的列发挥作用,查询数据
3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用

h.左连接

select * from 左表名 left join 右表名 on userinfo.depart_id=department.id;
左边的表全部显示, 右边没有用到不显示

 

posted @ 2019-06-19 15:14  Huanghongzheng  阅读(125)  评论(0编辑  收藏  举报