生活随笔
收集整理的這篇文章主要介紹了
总结SQL常用语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SQL常用語句
1. 表
建表 create table student( sname varchar(30) not null, sno varchar(20) not null,birthday date not null,syear int check(syear between 1 and 100) not null,sex char(1) check ( sex in ('F','M')) not null,city varchar(100) check ( city in ('上海','北京')),primary key(sno), foreign key(dno) references department(dno) );
向表中插入數據
insert into student
(sname
,sno
,birthday
,dno
,syear
,sex
,city
)
values("小明","201826010326","2020-01-12 12:00:00","A1020",20,"F","上海");
修改表數據
update student
set sname
="小明明"
where sno
="201826010326";
修改表 – 向表中添加一列:tel
alter table student
add tel
varchar(11);
刪除表一行數據
delete from student
where sno
="201826010326";
刪除表
drop table student
;
查詢表中數據
select sno
,sname
from student
where dno
like 'A%'
order by sno
desc;
2. 權限
2.1 角色和用戶
創建用戶
create user Tom identified
by "201826010326";
創建角色
create role
'group_leader';
刪除用戶
drop user Tom
;
刪除角色
drop role
'group_leader';
2.2 授予權限與收回權限
將表student的所有權限授予用戶Tom
grant all privileges
on student
to Tom
;
將表的查詢權限、刪除數據權限和對屬性sname的更改權限授予用戶Tom
grant select,delete,update(sname
)
on student
to Tom
;
將表student的查詢權限授予全部角色,并允許角色將得到的權限授予其他用戶或角色
grant select
on student
to public with grant option;
將角色group_leader授予用戶Tom
grant 'group_leader'
to Tom
;
收回權限
將上述4個授權語句中的grant替換為revoke,將to替換為from
revoke select
on student
from Tom
;
3. 視圖
創建視圖
create view view_name
( vsname
, vson
) as
select sname
,sno
from student
;
刪除視圖
drop view view_name
;
4. 索引
創建索引
create index sno_index
on student
(sno
);
create index tel_syear_index
on student
(tel
desc,syear
);
刪除索引
drop index sno_index
;
修改索引
alter index sno_index
rename to sno_i
;
總結
以上是生活随笔為你收集整理的总结SQL常用语句的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。