目录
- 一、数据库常用的基本操作命令
- 1.1 查看数据库列表信息
- 1.2 查看数据库中包含哪些表
- 1.3 查看数据表的结构(字段)
- 1.4 查看当前状态;
- 二、SQL语句
- 2.1 DDL语句
- 2.1.1 创建数据库
- 2.1.2 创建数据表
- 2.1.3 删除数据库和表
- 2.2 DML 语句
- 2.2.1 插入新数据
- 2.2.2 修改更新表中的数据记录
- 2.2.3 删除表中数据记录
- 2.3 DQL 语句
- 2.4 DCL 语句
- 2.4.1 设置用户权限
- 2.4.2 查看用户的权限
- 2.4.3 撤销用户权限
- 三、数据表高级操作
一、数据库常用的基本操作命令
1.1 查看数据库列表信息
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| students |
| sys |
+--------------------+
5 rows in set (0.01 sec)
1.2 查看数据库中包含哪些表
mysql> use students;
mysql> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| test |
| test01 |
| test02 |
| test03 |
| test04 |
| xuesheng |
+--------------------+
6 rows in set (0.00 sec)
1.3 查看数据表的结构(字段)
- 进入指定数据库
- describe 表名(也可简写为desc 表名)
mysql> describe test;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(20) | YES | | 未知 | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
1.4 查看当前状态;
--------------
mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database: students
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.17 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /usr/local/mysql/mysql.sock
Uptime: 8 min 33 sec
Threads: 1 Questions: 22 Slow queries: 0 Opens: 114 Flush tables: 1 Open tables: 107 Queries per second avg: 0.042
--------------
二、SQL语句
- Structured Query Language的缩写,即结构化查询模块
- 关系型书库里语言
- 用于维护管理数据库,包括数据查询、数据更新、访问控制、对象管理等功能。
- SQL 分类
DDL:数据定义语言
DML:数据操纵语言
DQL:数据查询语言
DCL:数据控制语言
2.1 DDL语句
2.1.1 创建数据库
mysql> create database student1;
2.1.2 创建数据表
- 基本语法
create table 表名(字段01名称 字段01类型 字段01约束,字段02名称 字段02类型 字段02约束)存储引擎,字符集 ## 定义多个类型用逗号分隔
create table xuesheng(id int(10) auto_increment,
name varchar(15) NOT NULL,
score decimal(5,2) NOT NULL,
address varchar(50) NOT NULL default ‘未知’,
PRIMARY KEY(id));
2.1.3 删除数据库和表
- 删除指定的数据表
drop table 数据库名.表名;
mysql> drop table students.test04;
Query OK, 0 rows affected (0.00 sec)
- 删除指定的数据库
- 基本格式
drop database 数据库名;
mysql> drop database student1;
Query OK, 0 rows affected (0.01 sec)
2.2 DML 语句
- 用于对表中的数据进行管理
- 包括的操作
insert: 插入新数据
update:更新原有数据
delete:删除不需要的数据
2.2.1 插入新数据
- 基本格式:
方式一:
INSERT INTO 表名(字段1,字段2,…………) VALUES(字段1的值,字段2的值……) ## 有序,表名和VALUES 要一 一对应,数量也要对应
方式二:
insert into info values (字段1的值,字段2的值,字段3的值) ## 不跟字段名,数值写全了,每一个都得写,不加字段名,默认所有字段
方式三:
insert into info (name,score,address) values (‘赵六’,99,suzhou),(‘tianqi’,60,default) ## 多行插入
mysql> select * from xuesheng;
+----+----------+-------+---------+
| id | name | score | address |
+----+----------+-------+---------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
+----+----------+-------+---------+
2 rows in set (0.00 sec)
- 方式一插入数据:
mysql> insert into xuesheng(name,score,address) values('wangwu',86,'shanghai');
Query OK, 1 row affected (0.01 sec)
- 方式二插入数据:
mysql> insert into xuesheng values(4,'zhaoliu',73,'beijing');
Query OK, 1 row affected (0.00 sec)
方式三插入数据:
mysql> insert into xuesheng(name,score,address) values('tianqi',99,'hangzhou'),('laoba',92,'guangzhou');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from xuesheng;
+----+----------+-------+-----------+
| id | name | score | address |
+----+----------+-------+-----------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
| 3 | wangwu | 86 | shanghai |
| 4 | zhaoliu | 73 | beijing |
| 5 | tianqi | 99 | hangzhou |
| 6 | laoba | 92 | guangzhou |
+----+----------+-------+-----------+
6 rows in set (0.00 sec)
2.2.2 修改更新表中的数据记录
- 基本格式
UPDATE 表名 SET 字段名1 =值1 WHERE 条件表达式
mysql> update xuesheng set name='xiaoba',score=87.5 where id=6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from xuesheng;
+----+----------+-------+-----------+
| id | name | score | address |
+----+----------+-------+-----------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
| 3 | wangwu | 86 | shanghai |
| 4 | zhaoliu | 73 | beijing |
| 5 | tianqi | 99 | hangzhou |
| 6 | xiaoba | 88 | guangzhou |
+----+----------+-------+-----------+
6 rows in set (0.00 sec)
2.2.3 删除表中数据记录
- 基本格式
delete from 表名 where 条件表达式 ## 不带where即删除表中所有记录
mysql> delete from xuesheng where id=6;
Query OK, 1 row affected (0.00 sec)
mysql> select * from xuesheng;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
| 3 | wangwu | 86 | shanghai |
| 4 | zhaoliu | 73 | beijing |
| 5 | tianqi | 99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)
2.3 DQL 语句
- 是数据查询语句,命令只有SELECT
- 用于从数据表中查找指定符合条件的数据记录
- 查询时可不指定条件
- 基本格式
select 字段名1,字段名2 …… from 表名
mysql> select name,score from xuesheng;
+----------+-------+
| name | score |
+----------+-------+
| zhangsan | 90 |
| lisi | 88 |
| wangwu | 86 |
| zhaoliu | 73 |
| tianqi | 99 |
+----------+-------+
5 rows in set (0.00 sec)
2.4 DCL 语句
2.4.1 设置用户权限
- 基本格式
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [IDENTIFIED by ‘密码’]
grant all on *.* to 'jerry'@'localhost' identified by 'abc123'
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.4.2 查看用户的权限
- 基本格式
show grants for 用户名@来源地址;
mysql> show grants for 'jerry'@'localhost';
+----------------------------------------------------+
| Grants for jerry@localhost |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jerry'@'localhost' |
+----------------------------------------------------+
1 row in set (0.00 sec)
2.4.3 撤销用户权限
- 基本格式
revoke 权限列表 on 数据库名.表名 from 用户名@来源地址;
mysql> revoke all on *.* from 'jerry'@'localhost' ;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'jerry'@'localhost';
+-------------------------------------------+
| Grants for jerry@localhost |
+-------------------------------------------+
| GRANT USAGE ON *.* TO 'jerry'@'localhost' |
+-------------------------------------------+
1 row in set (0.00 sec)
三、数据表高级操作
2.1 临时表
- 临时建立的表,用于保存一些临时数据,不会长期存在(在内存中,关机重启后就不存在了)
- 基本格式
create temporary table 表名(字段01名称 字段01类型 字段01约束,字段02名称 字段02类型 字段02约束)存储引擎,字符集
create temporary TABLE mytmp1 (
id int(10) NOT NULL AUTO_INCREMENT,
NAME varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
level int(10) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.2 克隆表
- 方法一:
create table 克隆生成表表名 as select * from 被克隆表表名;(一气呵成法) ## 可在select后跟属性 不是克隆整张表
- 方法二:
分两步法:
create table test like mytmp;(复制一份表结构,生成新表 mytmp是被复制的)
insert into test select * from mytmp;(把原表当中的数据导入新表当中)
- 方式一:
mysql> create table tmp as select * from xuesheng;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tmp;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
| 3 | wangwu | 86 | shanghai |
| 4 | zhaoliu | 73 | beijing |
| 5 | tianqi | 99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)
- 方式二:
mysql> create table tem1 like xuesheng;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into tem1 select * from xuesheng;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tem1;
+----+----------+-------+----------+
| id | name | score | address |
+----+----------+-------+----------+
| 1 | zhangsan | 90 | nanjing |
| 2 | lisi | 88 | chengdu |
| 3 | wangwu | 86 | shanghai |
| 4 | zhaoliu | 73 | beijing |
| 5 | tianqi | 99 | hangzhou |
+----+----------+-------+----------+
5 rows in set (0.00 sec)
2.3 清空表
- 基本格式
truncate table 表名 ##
delete from 表名
drop 和truncate 的区别 drop直接删除表,表不在了,truncate是清空表,表结构还在,数据不在
mysql> truncate table tem1;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from tem1;
Empty set (0.00 sec)
mysql> drop table students.tmp;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from tmp;
ERROR 1146 (42S02): Table 'students.tmp' doesn't exist