不理论只实战SQL语言核心四剑客:DDl、DML、DQL、DCL指南
一、SQL语言概述
SQL(Structured Query Language)是操作数据库的标准语言。尽管有些资料会将SQL分为五类(增加TCL事务控制语言),但本文聚焦最常见的四类:
DDL(Data Definition Language):定义数据库结构(库、表、索引等)
DML(Data Manipulation Language):操作数据(增、删、改)
DQL(Data Query Language):查询数据(查)
DCL(Data Control Language):管理权限
二、连接MySQL命令行界面
启动MySQL客户端:
mysql -u root -p # 输入密码后进入交互模式
查看所有数据库:
show databases;
退出MySQL:
exit;
三、DDL(数据定义语言)
用于定义数据库、表、索引等结构。
1. 数据库操作
创建数据库:
create database shop;
删除数据库:
drop database shop; -- 不可恢复!
修改数据库字符集:
alter database shop charset=utf8mb4;
切换当前数据库:
use shop;
2. 表操作
创建表(含完整约束):
create table users (
id int primary key auto_increment,
name varchar(50) not null,
email varchar(100) unique,
age int check (age >= 18),
created_at datetime default current_timestamp
);
添加列:
alter table users add column address varchar(200);
删除列:
alter table users drop column age;
修改列类型:
alter table users modify name varchar(100);
重命名表:
alter table users rename to customers;
删除表:
drop table customers;
清空表数据:
truncate table logs; -- 快速清空且不可回滚
3. 索引操作
添加普通索引:
create index idx_email on users(email);
添加唯一索引:
create unique index uni_phone on users(phone);
删除索引:
drop index idx_email on users;
四、DML(数据操作语言)
用于操作表中的数据。
1. 插入数据
插入单条数据:
insert into users (name, email)
values ('张三', 'zhangsan@example.com');
插入多条数据:
insert into products (name, price)
values ('鼠标', 99.9),
('键盘', 199.9);
插入查询结果:
insert into user_backup (name, email)
select name, email from users where is_deleted = 1;
2. 更新数据
更新单个字段:
update users set email = 'new@example.com' where id = 1;
批量更新:
update products set price = price * 0.9 where category = 'electronics';
3. 删除数据
条件删除:
delete from users where last_login < '2023-01-01';
清空表数据(可回滚):
delete from logs; -- 逐行删除,保留表结构
五、DQL(数据查询语言)
唯一语句 select,但功能最复杂。
1. 基础查询
查询所有字段:
select * from users;
指定字段并别名:
select name as 姓名, email as 邮箱 from users;
去重查询:
select distinct department from employees;
2. 条件过滤
比较运算符:
select * from products where price > 100;
逻辑运算符:
select * from users where age >= 18 and is_verified = 1;
模糊匹配:
select * from articles where title like '%数据库%';
3. 聚合与分组
统计总数:
select count(*) from orders;
分组统计:
select department, avg(salary)
from employees
group by department
having avg(salary) > 10000;
4. 多表连接
内连接:
select u.name, o.order_date
from users u
inner join orders o on u.id = o.user_id;
左连接:
select u.name, o.amount
from users u
left join orders o on u.id = o.user_id;
5. 子查询
IN子查询:
select * from products
where id in (select product_id from orders where amount > 500);
EXISTS子查询:
select * from users u
where exists (select 1 from orders o where o.user_id = u.id);
六、DCL(数据控制语言)
管理用户权限。
1. 用户管理
创建用户:
create user 'dev_user'@'localhost' identified by 'password123';
删除用户:
drop user 'dev_user'@'localhost';
2. 权限管理
授予权限:
grant select, insert, update on shop.* to 'dev_user'@'localhost';
收回权限:
revoke delete on shop.orders from 'dev_user'@'localhost';
刷新权限:
flush privileges;
七、综合示例:电商系统
-- 1. ddl:创建数据库和表
create database ecommerce;
use ecommerce;
create table products (
id int primary key auto_increment,
name varchar(100) not null,
price decimal(10,2) check (price > 0),
stock int default 0
);
-- 2. dml:插入商品
insert into products (name, price, stock)
values ('无线耳机', 299.0, 50),
('智能手表', 899.0, 30);
-- 3. dql:查询低库存商品
select name, stock from products where stock < 10;
-- 4. dcl:授予库存管理员权限
grant select, update(stock) on ecommerce.products to 'stock_manager'@'%';
总结
DDL:定义结构(create/alter/drop)
DML:操作数据(insert/update/delete)
dql:查询数据(select)
DCL:管理权限(grant/revoke)
每条语句都像积木,组合使用即可构建强大的数据库应用!