0
点赞
收藏
分享

微信扫一扫

mysql 语句与实例

小迁不秃头 2022-04-21 阅读 43
linux

文章目录

1. mysql语句

SQL语句类型对应操作
DDLCREATE:创建
DROP:删除
ALTER:修改
DMLINSERT:向表中插入数据
DELETE:删除表中数据
UPDATE:更新表中数据
SELECT:查询表中数据
DCLGRANT:授权
REVOKE:移除授权

2. mysql 语句语法与实例

2.1 DDL操作 create,drop,alter

2.1.1 数据库操作

//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
//创建数据库xiaoxie
mysql> create database xiaoxie;
Query OK, 1 row affected (0.00 sec)
//再次创建
mysql> create database sanjin;
Query OK, 1 row affected (0.00 sec)
//查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sanjin             |
| sys                |
| xiaoxie            |
+--------------------+
6 rows in set (0.00 sec)
//创建重名后报错
mysql> create database sanjin;
ERROR 1007 (HY000): Can't create database 'sanjin'; database exists
//加上if not exists更严谨 
mysql> create database if not exists sanjin ;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sanjin             |
| sys                |
| xiaoxie            |
+--------------------+
6 rows in set (0.00 sec)

//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库sanjin
mysql> drop database  xyx;
ERROR 1008 (HY000): Can't drop database 'xyx'; database doesn't exist
mysql> drop database if exists xyx;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> drop database if exists sanjin;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xiaoxie            |
+--------------------+
5 rows in set (0.00 sec)



2.1.2 表操作

//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
//进入xiaoxie数据库 在数据库里创建表boom
mysql> create table boom(id int(100) primary key auto_incrementt ,idcard varchar(13)  not null,name tinyint(4));
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_xiaoxie |
+-------------------+
| boom              |
+-------------------+
1 row in set (0.00 sec)

//删除表
//语法:DROP TABLE [ IF EXISTS ] 'table_name'
mysql> drop table if exists boom;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
Empty set (0.00 sec)

//修改表
mysql>use xieyanxin;
Database changed
mysql> alter table student add column nihaoma int(3);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+----------+------+--------+
| id | name     | age  | nihaoma |
+----+----------+------+--------+
|  1 | zhangsan |   12 |   NULL |
|  2 | lisi     |   14 |   NULL |
|  3 | wangwu   |   15 |   NULL |
|  4 | lili     |   11 |   NULL |
|  5 | zhaoliu  |   13 |   NULL |
+----+----------+------+--------+
5 rows in set (0.01 sec)

//在表中,指定地方插入列
mysql> alter table student add column chifanle int(3) after id;        
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+--------+----------+------+
| id | chifanle | name     | age  |
+----+--------+----------+------+
| 1  |   NULL | zhangsan |   12 |
| 2  |   NULL | lisi     |   14 |
| 3  |   NULL | wangwu   |   15 |
| 4  |   NULL | lili     |   11 |
| 5  |   NULL | zhaoliu  |   13 |
+----+--------+----------+------+
5 rows in set (0.00 sec)

//删除表结构
mysql> alter table student  drop column chifanle;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
| 1  | zhangsan |   12 |
| 2  | lisi     |   14 |
| 3  | wangwu   |   15 |
| 4  | lili     |   11 |
| 5  | zhaoliu  |   13 |
+----+----------+------+
5 rows in set (0.00 sec)

2.2 DML操作 insert,update,select,delete

//创建数据库 xieyanxin
mysql> create database xieyanxin;
Query OK, 1 row affected (0.00 sec)

//进入该数据库
mysql> use xieyanxin;
Database changed

//创建名为student的表 
mysql> create table student (id int(11) primary key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.01 sec)

//查看当前表内容
mysql> select * from student;
Empty set (0.00 sec)

# insert
语法:INSERT [INTO] table_name [(column_name,…)] {VALUES | VALUE} (value1,…),(…),…
//增加表内容
mysql> insert student(name,age) values('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangsan',26),('zhangsan',20),('lisi',null),('chengshuo',10),('wangwu',3),('qiuyi',15),('qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

//验证
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangsan    |   26 |
|  6 | zhangsan    |   20 |
|  7 | lisi        | NULL |
|  8 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

#update
语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT[m,]n];
//修改表内数据
mysql> update student set age=50 where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

//验证修改结果
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangsan    |   26 |
|  6 | zhangsan    |   20 |
|  7 | lisi        |   50 |
|  8 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

# select
语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
//按年龄降序查看表内容
mysql> select * from student order by age desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  7 | lisi        |   50 |
|  4 | sean        |   28 |
|  5 | zhangsan    |   26 |
|  3 | wangqing    |   25 |
|  2 | jerry       |   23 |
|  1 | tom         |   20 |
|  6 | zhangsan    |   20 |
| 11 | qiuxiaotian |   20 |
| 10 | qiuyi       |   15 |
|  8 | chengshuo   |   10 |
|  9 | wangwu      |    3 |
+----+-------------+------+
11 rows in set (0.00 sec)

//按名字逆序查看表内容
mysql> select * from student order by name desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  5 | zhangsan    |   26 |
|  9 | wangwu      |  100 |
|  3 | wangqing    |   25 |
|  1 | tom         |   20 |
|  4 | sean        |   28 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
|  7 | lisi        |   50 |
|  2 | jerry       |   23 |
|  8 | chengshuo   |   10 |
+----+-------------+------+
10 rows in set (0.00 sec)

//查看年龄最大的四位同学
mysql> select * from student order by age desc limit 4;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  7 | lisi     |   50 |
|  4 | sean     |   28 |
|  5 | zhangsan |   26 |
|  3 | wangqing |   25 |
+----+----------+------+
4 rows in set (0.00 sec)

//查看名字叫张三且年龄大于20岁的同学
mysql> select * from student where name ='zhangsan' and age > 20;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  5 | zhangsan |   26 |
+----+----------+------+
1 row in set (0.00 sec)

//查看年龄介于23到30岁的同学
mysql> select * from student where age between 23 and 30;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  2 | jerry    |   23 |
|  3 | wangqing |   25 |
|  4 | sean     |   28 |
|  5 | zhangsan |   26 |
+----+----------+------+
4 rows in set (0.00 sec)



#删除
语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
//删除名字为张三 年龄小于 20岁的 一条 记录
mysql> delete from student where name='zhangsan' and age <=20; 
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangsan    |   26 |
|  7 | lisi        |   50 |
|  8 | chengshuo   |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
10 rows in set (0.00 sec)

2.3 DCL操作 grant,revoke

2.3.1 用户操作

mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
(‘USERNAME’@‘HOST’)的HOST值可为:

IP地址,如:192.168.160.130
通配符:
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符

//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建用户xiaoxie
mysql> create user  'xiaoxie'@'192.168.177.1'  IDENTIFIED BY  'Xyx123456!.';
Query OK, 0 rows affected (0.02 sec)

//新用户登录
[root@rookie ~]# mysql -uxiaoxie -p'Xyx123456!' -h192.168.177.1

mysql> 

 //删除数据库用户
//语法:DROP USER 'username'@'host'; 

//删除数据库用户
mysql> drop user 'xiaoxie'@'192.168.177.1';
Query OK, 0 rows affected (0.01 sec)

2.3.2 授权操作

  • 可授予用户的权限类型
权限类型代表什么?
ALL所有权限
SELECT查询数据
INSERT插入新增数据
UPDATE更新修改数据
DELETE删除数据
  • 可授予用户的数据库对象
表示对象表示意义
* .*所有库的所有表
db_name指定库的所有表
db_name.table_name指定库的指定表
语法:GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];

//授权xiaoxie用户在本机上登录访问所有数据库
	mysql> grant all on  *.* to  'xiaoxie'@'localhost' idenfied by  'Xyx123456!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

//授权xiaoxie用户在192.168.177.222上登录 查看 alibaba数据库
mysql>grant select  on  db.alibaba  to  'xiaoxie'@'192.168.177.222' idenfied by  'Xyx123456!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

//授权xiaoxie用户在所有位置上远程登录查看alibaba数据库
mysql> grant select  on  db.alibaba to 'xiaoxie'@'%' idenfied by  'Xyx123456!';
Query OK, 0 rows affected, 1 warning (0.00 sec)


取消授权REVOKE
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> revoke all on  *.* from 'xiaoxie'@'%';
Query OK, 0 rows affected (0.00 sec)
 
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
举报

相关推荐

0 条评论