时间:2021-07-01 10:21:17 帮助过:2人阅读
drop user linger; //删除用户alter user linger with password ‘xxxxxx‘; //更改用户密码create database DB; //创建数据库DBdrop database DB; //删除数据库create table teacher(
   Tno varchar(10),
   Tname varchar(10) unique not null,
   Tsex char(2) check(Tsex in(‘男‘,‘女‘)),
   Tage smallint,
   constraint C1 primay key(Tno)
   );     //创建数据表alter table teacher add column Location char(10); //修改数据表drop table test;//删除数据表create view v(sno,sname,sdept) as sno,sname,sdept from student; //创建视图create or replace view v as select sno,sname,sdept from student where sdept!=‘MA‘; //修改视图drop view v; //删除视图grant all privileges on database weibo to linger;
revoke all privileges on database weibo from linger; //分配、释放权限alter user linger valid until ‘2016-08-08‘; //给定用户的有效日期 
create role father login nosuperuser nocreatedb nocreaterole noinherit encrypted password ‘xxxxxx‘; //创建组角色create role son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password ‘xxxxxx‘;
grant father to son1; //创建成员角色并分配权限(一)create role son2 login nosuperuser nocreatedb nocreateroel inherit encrypted password ‘xxxxxx‘ in role father; //创建成员角色并分配权限(二) 
DDL数据操作语言是数据库学习的主要内容,它包括select、insert、delete、update、call、explain plan、lock table等内容。这里主要介绍一些基本用法。
SELECT [ ALL | DISTINCT | DISTINCT ON (distinct_expressions) ]
expressions
FROM tables
[WHERE conditions]
[GROUP BY expressions]
[HAVING condition]
[ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS FIRST | NULLS LAST ]]
[LIMIT [ number_rows | ALL]
[OFFSET offset_value [ ROW | ROWS ]]
[FETCH { FIRST | NEXT } [ fetch_rows ] { ROW | ROWS } ONLY]
[FOR { UPDATE | SHARE } OF table [ NOWAIT ]]; //这归纳了select几乎所有的用法,由于select用法极其复杂,下面只给出一些比较常见的用例。关于比较高级的用法我会在后续博客中进行详细分析。select sno,sname,ssex,sdept from student where sage<20 or sdept=‘MA‘;  //条件查询select * from student where sage<20 union select * from student where sdept=‘MA‘; //集合查询select sno,sname from student where sno in(select sno from student where sdept=‘MA‘); //嵌套查询UPDATE table
SET column1 = expression1 | DEFAULT,
    column2 = expression2 | DEFAULT,
    ...
[WHERE conditions]; //update用法很简单update student set sname=‘李勇‘ where sno=‘01‘;
update student set sname=‘王芳‘ where sno=‘02‘;
update student set sname=‘张立‘ where sno=‘03‘;
update student set sname=‘王敏‘ where sno=‘04‘;
update student set sname=‘刘晨‘ where sno=‘05‘;
update student set sname=‘黎勇‘ where sno=‘06‘;
select * from student; 
DELETE FROM table
[WHERE conditions]; //delete也很简单delete from student where sno=‘06‘;INSERT INTO table
(column1, column2, ... )
VALUES
(expression1 | DEFAULT, expression2 | DEFAULT, ... ),
(expression1 | DEFAULT, expression2 | DEFAULT, ... ),
...; //增删查改四种操作,只有select比较复杂,别的都很简单。insert into student values(‘06‘,‘李伟‘,‘M‘,18,‘IS‘);explain select sno,sname from student where sage<20 and sdept=‘MA‘;postgreDB之学习笔记(一)
标签: