数据库

创建数据库

CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ] 用于指定新建数据库属于哪个用户,不指定则数据当前执行命令的用户
[ TEMPLATE [=] template ] 指定模板库
[ ENCODING [=] encoding ] 指定字符编码
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ] 指定关联的表空间
[ ALLOW_CONNECTIONS [=] allowconn ]
[ CONNECTION LIMIT [=] connlimit ] 指定可以接受的并发连接,默认 -1 不限制
[ IS_TEMPLATE [=] istemplate ] ]

修改数据库

alter database osdbadb connection limit 10;
alter database osdbadb rename to tesetdb01;
alter database tesetdb01 owner to cjr;
alter database testdb01 set tablespace new_tablespace;
alter database tesedb01 set enable_indexscan TO off;
alter database tesetdb01 reset all;
alter database tesetdb reset enable_indexscan;

删除数据库

drop database tesetdb01;

模式

不同的模式下可以有相同名称的表,函数等对象不会冲突,只要有权限,各个模式的对象可以互相调用。在PG中,不能同时访问不同数据库的对象,可以同时访问这个数据库中的多个模式的对象。

create schema osdba;  创建模式
drop schema osdba; 删除模式
alter schema osdba rename to osdba1; 重命名模式
alter schema osdba1 owner to cjr; 修改模式的所属用户


要创建或者访问模式中的对象,需要写一个修饰的名字,模式名+表名

schema_name.table_name

用户无法访问模式中不属于他们的对象,若要访问此类对象,模式的所有者必须在模式下赋予他们 uasge权限。在默认情况下每个人在public模式下都有create 和USAGE权限,也就是说允许所有可以连接到指定数据库上的用户在这里创建对象。可以回收该权限

revoke create on schema public from PUBLIC ;

第二个PUBLIC的意思是所有用户。


单一主键表

create table test001(id int primary key,note varchar(20));

复合主键表

需要用约束子句的语法

create table test002(id1 int,id2 int, note varchar(20),
CONSTRAINT pk_test002 primary key (id1,id2));

唯一约束

create table test003(id1 int,id2 int,id3 int,note varchar(20),
CONSTRAINT pk_test0003 primary key(id1,id2),
CONSTRAINT uk_test03_id2 UNIQUE(id3));

模板建表

create table baby (like test003); 但是没有复制约束过来,

需要加INCLUDE关键字

create table baby1 (like test003 including all);

create table baby2 as select * from test003 with no data;


临时表

会话级的临时表,另一种事务级的临时表。

会话级的临时表

create TEMPORARY table tmp_t1(id int primary key,note text);

事务级的临时表

会话级的临时表,另一种事务级的临时表。

create TEMPORARY table tmp_t1(id int primary key,note text);会话临时表

create TEMPORARY table tmp_t2(id int primary key,note text) on commit delete rows;事务提交数据消失

create TEMPORARY table tmp_t2(id int primary key,note text) on commit PRESERVE ROWS;数据一直存在整个会话当中

create TEMPORARY table tmp_t2(id int primary key,note text) on commit drop;事务提交表消失


UNLOGGED表

不记录wal日志,无法进行异常宕机恢复,正常关机不影响

create unlogged table unlogged1(id int primary key,t text);


默认值DEFAULT

upadte student set age=DEFAULT where no=2;

默认值是一个表达式,在插入默认值的时候会进行计算,而不是在创建表的时候

修改默认值alter table student alter column age set default 15;

删除默认值alter table student alter column age drop default;


约束

检查约束

create table persons
(name varchar(40),
age int check (age >=0 and age<=150),
sex boolean );

引用约束名

create table persons
(name varchar(40),
age int CONSTRAINT check_age check (age >=0 and age<=150),
sex boolean);

约束的写法

create table books(
book_no integer,
name text,
price numeric check (price >0),
discounted_price numeric check (discounted_price >0),
check(price>discounted_price));

第三个约束没有附在某个字段上,而是在逗号分隔的列表中以一个独立行的形式出现。

或者这样

create table books(
book_no integer,
name text,
price numeric
discounted_price numeric
check (price >0 and discounted_price >0 and price>discounted_price)
);

唯一约束

create table books(
book_no integer uniqe,
name text,
price numeric
);

create table books(
book_no integer,
name text,
price numeric
UNIQUE(book_no)
);


非空约束

not  null修饰字段即可


外键约束

create table class(
class_no int primary key,
class_name varchar(40)
);
create table student(
student_no int primary key,
student_name varchar(40),
age int,
class_no int references class(class_no)
);

增加约束
alter tablestudent add check (age<16);

删除约束
alter table student drop constraint constraint_name;

删除非空约束非空约束没有名称,可以这样删除

alter table student alter column student_name drop not null;


主键约束

主键可以后期创建

alter table books add constraint pk_book_book_no primary key(book_no);


表相关操作

增加字段 alter table class add column class_teacher varchar(40);

删除字段 alter tableclass drop column class_teacher;

重命名字段alter table books rename column book_no to book_id;

重命名表 alter table books rename to book;