概覽
SQL(Structured Query Language,結(jié)構(gòu)化查詢語(yǔ)言),分類如下:
- DDL:CREATE、ALTER、DROP
- DML:INSERT、UPDATE、DELETE
- DCL:定義訪問(wèn)權(quán)限和安全級(jí)別
- DQL:數(shù)據(jù)查詢語(yǔ)言,用來(lái)查詢記錄。SELECT
DDL
【操作數(shù)據(jù)庫(kù)】
// 創(chuàng)建數(shù)據(jù)庫(kù)
create database test;
create database test character set gbk;
show create database test;
// 修改數(shù)據(jù)庫(kù)字符集
alter database test character set utf8;
// 刪除數(shù)據(jù)庫(kù)
drop database test;
select database();
// 切換數(shù)據(jù)庫(kù)
use test;
【操作數(shù)據(jù)表】
// 創(chuàng)建表
create table people(
id int,
name varchar(5),
birthday date,
remark text
);
// 查看表格的創(chuàng)建細(xì)節(jié)
show create table 表名;
// 顯示所有表
show tables;
// 查看表信息
desc 表名;
alter table 表名 character set gbk;
// 增加一列
alter table 表名 add 列名 類型;
// 修改列的長(zhǎng)度
alter table 表名 modify 列名 類型(長(zhǎng)度);
// 刪除列
alter table 表名 drop 列名;
// 修改列名
alter table 表名 change 列1 列2 類型(長(zhǎng)度);
// 修改表名
rename table 表名1 to 表名2;
// 刪除表
drop table 表名;
DML
【插入操作】
insert into user (id,num,birthday,remark) values
(2,'mi','2019-07-23','你好,mi'),
(3,'ali','2019-07-23','你好,ali'),
(4,'bd','2019-07-23','你好,bd');
【修改操作】
// 修改多列數(shù)據(jù)逗號(hào)隔開
// 更新表中所有該列對(duì)應(yīng)的數(shù)據(jù)
update 表名 set 列名='2019-07-24';
// 更新指定數(shù)據(jù)
update 表名 set 列名='2019-07-25' where 列名='lwd';
【刪除操作】
delete from 表名 where 列名='';
// 刪除表中所有記錄(表結(jié)構(gòu)還在)
delete from 表名;
// 刪除表
truncate table 表名;
DQL
select 列名
from 表名
where 行條件