当前位置:Gxlcms > 数据库问题 > postgreDB之学习笔记(一)

postgreDB之学习笔记(一)

时间:2021-07-01 10:21:17 帮助过:2人阅读

user linger with password ‘xxxxxx‘; //创建用户

技术分享

drop user linger; //删除用户

技术分享

alter user linger with password ‘xxxxxx‘; //更改用户密码

技术分享

create database DB; //创建数据库DB

技术分享

drop 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; //创建成员角色并分配权限(二)

技术分享
技术分享

DML(数据操作语言)

DDL数据操作语言是数据库学习的主要内容,它包括select、insert、delete、update、call、explain plan、lock table等内容。这里主要介绍一些基本用法。

  • select基本用法
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基本用法
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基本用法
DELETE FROM table
[WHERE conditions]; //delete也很简单
delete from student where sno=‘06‘;

技术分享

  • insert基本用法
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1 | DEFAULT, expression2 | DEFAULT, ... ),
(expression1 | DEFAULT, expression2 | DEFAULT, ... ),
...; //增删查改四种操作,只有select比较复杂,别的都很简单。
insert into student values(‘06‘,‘李伟‘,‘M‘,18,‘IS‘);

技术分享

  • explain plan用法
explain select sno,sname from student where sage<20 and sdept=‘MA‘;

技术分享

postgreDB之学习笔记(一)

标签:

人气教程排行