概覽
SQL(Structured Query Language叁温,結構化查詢語言)祷膳,分類如下:
- DDL:CREATE客叉、ALTER、DROP
- DML:INSERT脯宿、UPDATE念颈、DELETE
- DCL:定義訪問權限和安全級別
- DQL:數(shù)據(jù)查詢語言,用來查詢記錄连霉。SELECT
DDL
【操作數(shù)據(jù)庫】
// 創(chuàng)建數(shù)據(jù)庫
create database test;
create database test character set gbk;
show create database test;
// 修改數(shù)據(jù)庫字符集
alter database test character set utf8;
// 刪除數(shù)據(jù)庫
drop database test;
select database();
// 切換數(shù)據(jù)庫
use test;
【操作數(shù)據(jù)表】
// 創(chuàng)建表
create table people(
id int,
name varchar(5),
birthday date,
remark text
);
// 查看表格的創(chuàng)建細節(jié)
show create table 表名;
// 顯示所有表
show tables;
// 查看表信息
desc 表名;
alter table 表名 character set gbk;
// 增加一列
alter table 表名 add 列名 類型;
// 修改列的長度
alter table 表名 modify 列名 類型(長度);
// 刪除列
alter table 表名 drop 列名;
// 修改列名
alter table 表名 change 列1 列2 類型(長度);
// 修改表名
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ù)逗號隔開
// 更新表中所有該列對應的數(shù)據(jù)
update 表名 set 列名='2019-07-24';
// 更新指定數(shù)據(jù)
update 表名 set 列名='2019-07-25' where 列名='lwd';
【刪除操作】
delete from 表名 where 列名='';
// 刪除表中所有記錄(表結構還在)
delete from 表名;
// 刪除表
truncate table 表名;
DQL
select 列名
from 表名
where 行條件