mysql中操作的简称_mysql常用操作

一、什么是数据库

数据库(Database)是按照数据结构来组织、存储和管理数据的仓库。

SQL( Structured Query Language)语言的全称是结构化查询语言。数据库管理系统通过SQL语言来管理数据库中的数据。

SQL语言分为三个部分:数据定义语言( Data DefinitionLanguage,简称为DDL)、数据操作语言( DataManipulation Language,简称为DML)和数据控制语言( Data Control Language,简称为DCL)。

DDL语句: CREATE、 ALTER、 DROP

DML语句: update、 insert、 delete、 select

DCL语句:是数据库控制功能,是用来设置或更改数据库用户或角色权限的语句,包括( grant,deny,revoke等)语句。

二、基本数据库操作

1.创建数据库及删除数据库

(1)create database  数据库名;

例:create database hhf charset=utf8;

(2)drop database 数据库名;

例:drop database hhf;

2.创建表

(1)创建表  create table 表名 (

列名1 列类型 [],

列名2 列类型 [],

... ...);

例:

create table students

(

id int(10) not null unique auto_increment primary key,

name varchar(20) not null,

sex varchar(4) ,

age int(10),

class varchar(20) not null,

addr varchar(50)

);

create table score

(

id int(10) not null unique auto_increment primary key,

stu_id int(10) not null,

c_name varchar(20),

grade int(10)

);

(2)删除表数据:delete  表名   删除表所有数据    truncate 表名  清空表所有数据   truncate students ;

(3)删除表  drop table 表名    例:drop table students;

(4)修改

修改表名:alter table 旧表名 rename [to] 新表名 ;   例如:alter table students rename st;

修改表的字段属性:alter table 表名  modify 属性名  数据类型;    例:alter table students  name char(20);

修改表的字段名:alter  table 表名  change 旧属性名  新属性名  新数据类型;例:alter table students name stu_name char(20);

增加字段:alter table 表名 add 属性名1 数据类型 [完整性约束条件] [first | after 属性名2] ;   例:alter table students add tel int(10) after addr;

删除字段 :alter table 表名 drop 字段名; 例:alter table school drop tel;

修改表字段排序:alter table 表名 modify 属性名1 数据类型 first|after 属性名2;

例:alter table students modify addr varchar(50) after name ;#把addr放到name后

alter table students modify addr varchar(50) first;#把addr放到第一个位置

3.数据库及表的查看

show databases; 查看系统中数据库

show create database 数据库名;  查看数据库的相关信息

use  databasename;选择数据库   例如:use  hhf ;

describe tablename; 查看表结构  或者desc  tablename

show create table students;  查看student表结构

flush privileges 刷新数据库

4.远程登录mysql

mysql  -h  主机地址   -u用户名 -p用户密码   例如:mysql -h 192.168.1.119 -u root -p123456    密码和-p之间不能有空格

退出mysql命令:exit

3.插入数据insert语句

(1)insert中不指定字段插入

insert into students values  (801,'刘海洋','男',21,'aa','北京市海淀区');

(2)insert中指定字段插入

insert into students (name,sex,age,class) values  (801,'刘海洋','男',21,'aa');

(3)inser into 表名1 (属性列表1)  select  属性列表2 from表名2 where 条件表达式;

insert into new_student select * from students;

(4)replace插入新记录

replace语句的语法格式有三种语法格式。

语法格式1: replace into 表名 [(字段列表) ] values (值列表)

语法格式2: replace [into] 目标表名[(字段列表1)]  select (字段列表2) from 源表 where 条件表达式

语法格式3:replace [into] 表名set 字段1=值1, 字段2=值2

例如:replace  into students values  (801,'刘海洋','男',21,'aa','北京市海淀区');

3.修改数据update语句

update 表名

set 属性名1=取值1, 属性名2=取值2,…,属性名n=取值n

where 条件表达式;

例:update students  set name='花花'  where  age=18;

4.删除操作delete语句

delete from 表名    where  条件表达式;

例:delete from students  where age =18;

5.查询操作select语句

select

{*|}

[

from ,...

[where ]

[group by 字段名]

[having 表达式]

[order by 字段名]

[limit [,]]

]

select [字段1,字段2,...,字段n]

from [表或视图]

where [查询条件]

(1)条件查询   条件判断符=,<>,!=,,>=,between...and(位于两者之间)     is null、  is not null    in not in

select * from students  where age between 18 and 25;#查询年龄在18-25岁之间的学生信息

select * from students  where  addr is null or addr='';#查询地址为null或者为''的学生信息

select * from students  where  id in(801,802)#查询id在801、802的学生信息

带like的字符匹配查询

1)百分号通配符' %',匹配任意长度的字符,甚至包括零字符

2)下划线通配符'_',一次只能匹配任意一个字符

select * from students where name like '李_'; #查询姓李名字为2个字的学生信息

select * from students where name like '张%;#查询姓张的学生的信息

select * from students where name like '%三%';#查询名字中含有'三'的学生信息

(2)分组

select * from students group by class;#以calss分组

聚合函数:sum(),count(),avg(),max(),min()

select avg(grade) from score; #查询score表平均分数

select sum(grade) from score;#查询score表分数总和

select min(grade) from score;#查询score表中分数最低的记录

select max(grade) from score;#查询score表中分数最高的记录

select count(*) from score;#查询score表中记录行数

(3)排序

select * from score  order by stu_id desc ;#按照stu_id降序排序

(4)多表查询

多表查询是指从多张表中查询所需要的数据,一般查询的这几张表都有一个相同的字段关联这几张表。多表连接可以通过join关键字来连接,也可以直接用关联表中相同的id来进行关联;

join:left join:左连接, 连接两张表,以左边表的数据匹配右边表中的数据,如果左边表中的数据在右边表中没有,会显示左边表中的数据。

right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边表中的数据在左边边表中没有,会显示右边表中的数据。

inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会显示匹配的数据。

select a.name 学生姓名,b.score 学生成绩 from students a left join score b on   a.id=b.student_id;

select a.name 学生姓名,b.score 学生成绩 from students a right join score b on   a.id=b.student_id;

select a.name 学生姓名,b.score 学生成绩 from students a inner join score b on  a.id=b.student_id;

select a.name 学生姓名,b.score 学生成绩 from students a,score b wherea.id=b.student_id;

连接查询:

内连接:select  表名.列名,……

from 表1 inner join 表2   on 条件

外连接:left join(左连接)

5878250.html

select  表名.列名,…表2.列名……

from 表1 left  (outer )join  表2

on 条件

right join(右连接)

select  表名.列名,…表2.列名……

from 表1 right  (outer )join  表2

on 条件

(5)子查询

比如说要把成绩低于60分的学生的名字都改成笨蛋

update students set name = '笨蛋' where id in (select a.student_id from score a where a.score<60);

三、数据库权限(均为在mysql下操作)

1.为数据库授权:grant 权限 on 数据库对象 to 用户

(1)增加一个超级用户,拥有所有的权限,只允许远程登录

grant all on *.* to 'hhf'@'%' IDENTIFIED BY '123456' with grant option;

e2ae3a18b68abb8fe68485d6678e9a22.png

(2)增加一个超级用户,拥有所有的权限,只允许本地登录

grant all on *.* to 'hhf'@'localhost' IDENTIFIED BY '123456' with grant option;

e7b7b51ee1341e0381028d6c73b839f2.png

(3)增加一个超级用户,限制登录ip为192.168.1.119

grant all  on *.* to 'dba'@'192.168.1.119' IDENTIFIED BY '123456';

10db1a5494878400807fb07562a32367.png

(4)增加一个普通用户,只有对bugfree数据库查询的修改的权限,允许远程登录

grant select, insert,update on bugfree.* to 'test'@'%' IDENTIFIED BY '123456';

a083c65095fc603684a4840e0ccd46f4.png

(5)增加一个只有查询bugfree的bug表中的bugtitle的用户

grant select(title) on bugfree.bf_bug_info to 'bugfree'@'%' IDENTIFIED BY '123456';

2.取消数据库权限:revoke 权限 on 数据库对象 from 用户

(1)取消超级用户hhf的超级权限

revoke all on *.* from 'hhf'@'localhost';

86cb63bdac1430ee006603004ec8ea0e.png

(2)取消dba用户查询权限

revoke select on *.* from 'dba'@'192.168.1.119';

6b713cbb144d192649dc1723cd389595.png

3.为数据库用设置密码或者删除用户

(1)为用户设置密码:update mysql.user set password=password('dba') where user='dba';

46c042283dacd34ce25a2ab76425dec3.png

(2)删除用户:delete from mysql.user where user='dba';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值