一、SQL介绍
二、SQL分类之DQL、DDL、DML、DCL介绍
三、DDL(Data Definition Language)数据定义语言
开发、运维都得会!
1、操作库
MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create database if not exists db1;
Query OK, 1 row affected, 1 warning (0.00 sec)
MariaDB [(none)]> show databases;
+
| Database |
+
| information_schema |
| db1 |
| kings |
| mysql |
| performance_schema |
| ultrax |
+
6 rows in set (0.00 sec)
MariaDB [mysql]> show databases like 'kings%';
+
| Database (kings%) |
+
| kings |
+
1 row in set (0.00 sec)
MariaDB [(none)]> show create database db1;
+
| Database | Create Database |
+
| db1 | CREATE DATABASE `db1` |
+
1 row in set (0.00 sec)
MariaDB [(none)]> alter database db1 character set utf8;
Query OK, 1 row affected (0.03 sec)
MariaDB [(none)]> drop database db1;
Query OK, 0 rows affected (0.03 sec)
2、操作表
MariaDB [(none)]> create database learn;
Query OK, 1 row affected (0.00 sec)
use 数据库名;
MariaDB [(none)]> use learn
Database changed
MariaDB [learn]> create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
Query OK, 0 rows affected (0.01 sec)
show tables;
MariaDB [learn]> show tables;
+
| Tables_in_learn |
+
| students |
+
1 row in set (0.00 sec)
desc 表名
MariaDB [learn]> desc student;
+
| Field | Type | Null | Key | Default | Extra |
+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,1) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| insert_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+
6 rows in set (0.00 sec)
show create table 表名;
MariaDB [learn]> show create table student;
+
| Table | Create Table |
+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`score` double(4,1) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`insert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+
1 row in set (0.00 sec)
alter table 表名 rename to 新的表名;
MariaDB [learn]> alter table student rename to students;
Query OK, 0 rows affected (0.01 sec)
alter table 表名 add 列名 数据类型;
MariaDB [learn]> alter table students add grade int;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [learn]> desc students;
+
| Field | Type | Null | Key | Default | Extra |
+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,1) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| insert_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| grade | int(11) | YES | | NULL | |
+
7 rows in set (0.00 sec)
alter table 表名 drop 列名;
MariaDB [learn]> alter table students drop grade;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [learn]> desc students;
+
| Field | Type | Null | Key | Default | Extra |
+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,1) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| insert_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+
6 rows in set (0.01 sec)
drop table 表名;
drop table if exists 表名 ;
MariaDB [learn]> show tables;
+
| Tables_in_learn |
+
| students |
+
1 row in set (0.00 sec)
MariaDB [learn]> drop table students;
Query OK, 0 rows affected (0.00 sec)
MariaDB [learn]> show tables;
Empty set (0.00 sec)
四、DCL(Data Control Language)数据控制语言
运维重点掌握,开发了解!
语法大小写不敏感,注意加空格
管理用户
1、添加用户
语法:create user '用户名'@'主机名' identified by '密码';
MariaDB [learn]> create user 'xuyuhan'@'%' identified 'xuyuhan';
Query OK, 0 rows affected (0.00 sec)
2、删除用户
语法:drop user '用户名'@'主机名';
MariaDB [mysql]> DROP USER 'xuyuhan'@'%';
Query OK, 0 rows affected (0.00 sec)
权限管理
1、查询权限
show grants for '用户名'@'主机名';
MariaDB [mysql]> show grants for 'xuyuhan'@'%';
+
| Grants for xuyuhan@% |
+
| GRANT USAGE ON *.* TO 'xuyuhan'@'%' IDENTIFIED BY PASSWORD '*F29AA23ACEA90407097D68FFEEEF8C2D5274BA94' |
+
1 row in set (0.00 sec)
2、授予权限
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
grant all on *.* to 'xuyuhan'@'localhost';;
MariaDB [mysql]> grant all on *.* to 'xuyuhan'@'localhost';
Query OK, 0 rows affected (0.00 sec)
3、撤销权限
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
五、DML(Data Manipulation Language)数据操作语言
开发重点掌握,运维了解!
1、增加 insert into
MariaDB [learn]> create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
Query OK, 0 rows affected (0.01 sec)
MariaDB [learn]> desc student;
+
| Field | Type | Null | Key | Default | Extra |
+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | double(4,1) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| insert_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+
6 rows in set (0.00 sec)
insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
MariaDB [learn]> insert into student (id,name,age) values (1,2,3);
Query OK, 1 row affected (0.00 sec)
insert into 表名 values(值1,值2,...值n);
insert into 表名(列名1,列名2) values(值1,值2);
2、删除 delete
delete from 表名 where 列名 = 值;
MariaDB [learn]> delete from student where id=1;
Query OK, 2 rows affected (0.00 sec)
delete from 表名;
MariaDB [learn]> delete from student;
Query OK, 0 rows affected (0.00 sec)
truncate table 表名;
MariaDB [learn]> truncate table student;
Query OK, 0 rows affected (0.00 sec)
修改 update
update 表名 set 列名 = 值;
update 表名 set 列名 = 值 where 列名=值;
六、DQL(Data Query Language)数据查询语言
开发重点掌握,运维了解!
select database();
MariaDB [mysql]> select database();
+
| database() |
+
| mysql |
+
1 row in set (0.00 sec)
MariaDB [mysql]> select user,host from mysql.user;
+
| user | host |
+
| xuyuhan | % |
| root | 127.0.0.1 |
| root | ::1 |
| | docker |
| root | docker |
| | localhost |
| root | localhost |
| xuyuhan | localhost |
+
8 rows in set (0.00 sec)
|