--create/drop/alter语句
create table 表名
(列名1 数据类型 完整性约束条件,
列名n 数据类型,
表完整性约束条件,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno)
);
--修改基本表 alter table
--增加列
alter table 表名 add 新列名 数据类型 完整性约束条件;
--增加完整性约束条件
alter table 表名 add 完整性约束条件(列名);--unique(Cname)
alter table 表名 add foreign key(Cno) references Student(Cno);
--删除列
alter table 表名 drop 列名 cascade;//级联
alter table 表名 drop 列名 restrict;//限制
--修改列
alter table 表名 alter column 列名 数据类型;
--删除基本表
drop table 表名 cascade;
drop table 表名 restrict;
---------------------------------
--索引
--建立唯一索引
create unique index 索引名 on 表明(列名1 次序,列名n 次序);--次序ASC or DESC
--修改索引
alter index 旧索引名 rename to 新索引名;
--删除索引
drop index 索引名;
---------------------------------
--select
select distinct/all 目标函数列
from 表名/视图名
where 条件表达式
group by 列名 having 条件表达式--在分组的条件下再where
order by 列名 次序;
--count( )统计某一列值的个数
count(distinct/all 列名)
--sum()
--max/min()
--where常用比较=、>、<、<=、>=、!=/<>
--记住判断是否为空时要用is null 、is not null
where 列名 between 最小值 and 最大值
where 列名 not between 最小值 and 最大值
where 列名 in( )
where 列名 like '_小%';
where 列名 like 'BD\_%' escape '\';--当要找的有_时用\转义
--not like也行
--and 和 or
----------------------------------
--嵌套查询
--in
where 列名 in(子查询);
--比较运算符
where 列名 比较运算符(子查询);
--exists 不返回数据只返回true 或 false
--用可能比较难理解
-----------------------------------
--insert/update/delete语句
--insert
insert
into 表名(列名1,列名n)
values(常量1,常量n);
--插入子查询的结果
INSERT INTO target_table (id, name, age)
SELECT id, name, age
FROM source_table
WHERE age > 28;
--update
update 表名
set 列名1=表达式1,列名n=表达式n
where 条件;
--delete
delete
from 表名
where 条件;
-------------------------------
--view视图
create view 视图名(列名1,列名n)
as 子查询
grant 和 revoke
-- 授予用户对特定数据库中特定表的某些权限
GRANT {ALL PRIVILEGES | SELECT | INSERT | UPDATE | DELETE |... }
ON [数据库名].[表名]
TO '用户名'@'主机名'
[WITH GRANT OPTION];
-- 收回用户对特定数据库中特定表的某些权限
REVOKE {ALL PRIVILEGES | SELECT | INSERT | UPDATE | DELETE |... }
ON [数据库名].[表名]
FROM '用户名'@'主机名';