数据库补充复习(一)

    xiaoxiao2022-07-13  129

    数据库补充复习(一) 1.用户授权: 创建用户 create user '用户名'@'IP地址' identifed by '密码'; 删除用户 drop user '用户名'@'IP地址'; 修改用户 rename user '用户名'@'IP地址'; 修改密码 setpassword from '用户名'@'IP地址' = Password('新密码') 2.权限: 默认,什么都没有 grant 权限 on 数据库.表 '用户'@'IP地址' --授权 3.更改数据库字符集: create database 数据库名称 default charset utf8 collate utf8_general_ci create database 数据库名称 default character set gbk collate gbk_general_ci 4.删除操作: 删除数据库:drop database 数据库名称 直接删除表:drop table 表名 清空表内容:delete from 表名 会记住清除前的最后一个值,然后之后的插入操作会以此知之为基准 清空表内容:truncate table 表名 性能高,速度快,清楚表后,自增列回归零,从起始位开始计算 5.数据操作: select * from myset; 增: insert into 表名 values ;可以一次性插入多条数据 insert into 表名 select(列名,列名,...) from 表 删: delete from 表名 where id=1 and name='eric' 改: update 表名 set name='eric' where id>1 查: select * from select * from where id>1 select nid,name,gender as gg from 表 where id>1 其它: 条件 select * from 表 where id>1 and name !='eric' and num=12; select * from 表 where id betwen 5 and 16; select * from 表 where id in(11,22,33); select * from 表 where id not in(11,22,33); select * from 表 where id in (select nid from 表) 通配符 select * from 表 where name like'eri%' -eri开头的所有(多个字符串) select * from 表 where name like'eri_' -eri开头的所有(一个字符串) 限制(分页) select * from 表 limit 5; -前5行 select * from 表 limit 4,5; -从第四行开始的5行,后面的5表示取5行 select * from 表 limit 5 offset 4; -从第四行开始的5行,功能是一模一样的 排序 select * from 表 order by 列 asc - 根据 列 从小到大排列 select * from 表 order by 列 desc - 根据 列 从大到小排列 select * from 表 order by 列1 desc,列2 asc - 根据 列1 从小到大排列,如果相同则按列2从小到大排序 分组 select num from 表 group by num select num,nid from 表 group by num,nid select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid select num from 表 group by num having max(id)>10 用聚合条件筛选时要用having distint一般是用来去除查询结果中的重复记录的,而且这个语句在select、insert、delete和update中只可以在select中使用, 具体的语法如下: select distinct expression[,expression...] from tables [where conditions]; 特别的:group by 必须在where之后,order by之前 连表 select * from a,b where a.x=b.o select * from a left join b on a.x=b.o 无对应关系则不显示 select A.num,A.name,B.name from A,B where A.nid = B.nid 无对应关系则不显示 inner过滤,如果出现null,inner会自动清除 select A.num,A.name,B.name from A inner join B on A.nid=B.nid A表所有显示,如果B中无对应关系,则值为null select A.num,A.name,B.name from A left join B on A.nid=B.nid B表所有显示,如果B中无对应关系,则值为null select A.num,A.name,B.name from A right join B on A.nid=B.nid 组合 组合,自动处理重合 select nickname from A union select name from B 组合,不处理重合 select nickname from A union all select name from B 6.事物,原子操作,回滚 默认值 是否可以为空 自增列(一张表只能有一个,数字,必须是索引-主键) 主键索引:一张表只能有一个主键,唯一不能重复,不能为null 1,2,3,4,5 一般情况下,自增列设置主键 唯一索引:可以为null,一张表可以有多个唯一列 1,2,3,4,5,null -- 约束 -- 索引,加速查找 create table xxx( nid.... primary key, ... ) create table student( name varchar(10) not null, num int not null, age int, gender int, primary key(name,num) --联合列约束 )engine=innodb default charset=utf8 约束: name num age a 88 9 a 99 0 主键:不能为空null 不能重复 一张表只有一个主键 一般用法: nid int auto_increment primary key, 外键:foreign key,一对多,两张表建立约束 创建外键:alter table 表名 add constraint 外键名称(形如:fk_从表头字母_主表头字母 指foreign key 从表 主表) foreign key 从表(外键字段) references 主表(主键字段) 删除外键:alter table 表名 drop foreign key 外键名称 数据类型:数值、时间和字符串 数值: 整数: bit 二进制 tinyint smallint int int后面加unsinged,表示无符号 bigint 范围不一样 浮点数: decimal --精确的(后台是以字符串的方式存储的) FLOAT --它不精准,数越大约不精准 DOUBLE --也不精确,但是相对于FLOAT精确一些,也是数越大越不精确 字符串: char 定长(长度指定,固定长度为8,但输入值长度为4时,就会占8个字节,不会节省空间,但是定长查找效率高) varchar 变长(长度不指定,固定长度为8,但输入值长度为4时,就只会占4个字节,节省空间,但是变长查找效率低) test mediumtext longtext 二进制数据: TinyBlob、MediumBlob、LongBlob 上传文件 Blob,强制二进制方式 varchar(65),"D:\av.avi" 将上传的文件保存在硬盘,D:\av.avi; 7.枚举类型: 编程语言中: Enum week: unauth = 2004 #未授权 x = '星期一' y = '星期二' z = '星期三' print week.x 数据库中:(单选) create table shirts( name varchar(40), size enum('x-small','small','redium','large','x-large') --枚举类别 ); insert into shirts(name,size) values('dress shirt','large'),('t-shirt','medium'),('polo shirt','small'); 集合类型:(多选) A set column can have a maximum of 64 distinct members 示例: create table myset (col set('a','b','c','d')); insert into myset(col) values('a,b'),('d,a'),('a,d,a'),('d,a,d');
    最新回复(0)