数据库操作手册

数据库的基本操作

  1. show databases; 查看数据库
  2. create database +数据库名 charset=utf8;创建数据库
  3. use +数据库名称 ; 进入数据库
  4. create table +表名 (id int primary key auto_increment,类+char(30),类+int);创建表
  5. show tables; 查看表
  6. desc +表名 ; 查看表结构
  7. insert into(表名)(类,类)value( “数据 ”, “数据 ”); 添加表数据
  8. select * from(表名) 查看表数据
  9. select name from person; 查询当前表中name字段的所有数据
  10. drop table +表名 ; 删除表
  11. order by id 正序
  12. order by id desc 倒序

数据库数据修改

表:

​ 修改表名

alter table student rename students;

字段(column):

​ 添加字段

alter table students addcolumn phone char(22);

​ 删除字段

alter table students drop column phone

​ 修改字段类型

alter table students modify column name char (32);

​ 修改字段名称类型

alter table students change column name namechar(32);

update students set name =“老刘” where id=1;

如果没有where 该所有

delete from student where id =1

数据库关联查询

创建用户

create user laobiao@localhostt;

创建用户并添加密码

create user laobian@localhost identified by “123”;

创建用户允许远端登录

create user laobian @10.10.65.250 identified by “123”; 允许10.10.65.250以laobian登录mysql

create user laobian@1% identified by“123”;允许10.10.65.0-10.10.65.255以laobian登录mysql

crrete user [email protected]_identified by “123”;允许10.10.65.250-10.10.65.255以laobian登录mysql

删除用户

drop user laobian @10.10.65.250

授权

常规授权

select 查询权限

insert 插入权限

update 更新权限

delete 删除权限

create 创建权限

主键 :

主键是一个表里的唯一标示 假如一个表没有主键,查询是遍历查询,如果有主键,会以平衡数据格式去找

外键

外键就是一表唯一字段为内容的关联字段,约束

定义的时候创建外键

关联查询:

1552910643262

lenner 内关联查询 取左右俩表的交集 取所有有宿舍的学生

left join 左关联查询 取左表的所有和左右俩表的交集

right join 右关联查询 取右表的所有和左右俩表的交集

llike 模糊查询

select * from student where name like “李%”;

数据库的索引与试图

索引的设立

索引使用数据结构 ,可以加快我们的查询效率 但是创建索引需要复制数据,会占用资源

索引分类:

普通索引 就是一个普通的索引,可以为空,可以重复。

alter table teacher add index(column);

唯一索引 可以为空 不可以重复

alter table teacher add unque(column);

主键索引 不可以为空 不可以重复;

alter table teacher add primary key(column);

多列索引

alter table teacher add index(column1,column2,column3)

view 视图

视图优点:

1,简单

2,安全

3,数据独立

视图缺点:

视图会降低查询的效率,尤其在视图当中的查询当中再次使用试图。

视图的设立

需求:

创建:

使用:

查看所有试图

show table status where comment =“view”;

删除试图:

drop view student_sun;

猜你喜欢

转载自blog.csdn.net/weixin_44239431/article/details/88649362