一、Postgresql的基本操作
-----------------------------------------------------------------------------------------------------
--目录:
--1. 数据库
----1.1 创建数据库
----1.2 删除数据库
--2. 架构
----2.1 创建架构
----2.2 删除架构
--3. 表
----3.1 创建表
------3.1.1 多个字段的联合唯一性
------3.1.2 主键和外键
----3.2 删除表
----3.3 修改表
----------------------------------------------------------------------------------------------------- --1. 数据库
----1.1 创建数据库
create database test;
----1.2 删除数据库
drop database test;
----1.3 修改数据库
alter database test rename to testdb; --2. 架构
----2.1 创建架构
create schema testschema;
----2.1 删除架构
drop schema testschema; --3. 表
----3.1 创建表
create table test_table1(
sid serial, --serial表示字段为自增字段
stu_number integer not null, --not null表示非空约束
name text unique, ----unique表示唯一性约束,指定的字段不能插入重复值
math_score numeric default 59.99, ----defalut表示设置该字段的默认值
english_score numeric check(english_score > 0), ----check表示该字段的值必须符合其内的表达式
description text,
UNIQUE(description) --唯一性约束也可以这样写
); ------3.1.1 多个字段的联合唯一性
create table example3(
a integer,
b integer,
c integer,
UNIQUE(a,c)
);
insert into example3 values(1,1,1);
insert into example3 values(1,1,2);--pased
insert into example3 values(1,2,1);--failed: duplicate key value violates unique constraint "example3_a_c_key"
------3.1.2 主键和外键
create table example4(
a integer,
b integer,
c integer,
primary key(b,c)-- 主键可以同时作用于多个字段,形成联合主键
); create table example5(
a integer primary key,
b integer,
c integer,
foreign key(b,c) references example4(b,c)--该外键的字段数量和被引用表中的主键的数量必须保持一致
);
----Description----
--(1) 当多个表之间存在主外键参考性约束关系的时候,如果想删除主键的某行数据,由于该行的记录的主键字段值可能正在被其引用表中的某条记录所关联,将会导致删除操作的失败
insert into example4 values(1,1,1);
insert into example4 values(1,2,2);
insert into example5 values(1,3,3);--failed: insert or update on table "example5" violates foreign key constraint "example5_b_fkey". Key (b, c)=(3, 3) is not present in table "example4".
insert into example5 values(2,1,1);
insert into example5 values(3,2,2);
select * from example4;
select * from example5;
delete from example4 where a = 1;--failed: update or delete on table "example4" violates foreign key constraint "example5_b_fkey" on table "example5". Key (b, c)=(1, 1) is still referenced from table "example5".
--(2) Psql提供了限制和级联删除来解决(1)中的问题
create table a
(
q integer primary key,
w integer,
e integer,
t integer
);
insert into a values(1,1,1,1);
insert into a values(2,1,1,1);
create table b(
a integer references a(q) on delete cascade, --cascade删除主表中一个被引用的行,所有的引用它的行也会被自动删除
b integer
);
insert into b values(1,1);
insert into b values(1,2);
insert into b values(2,1);
delete from a where q = 1;
select * from b ;
create table c(
a integer references a(q) on delete restrict, --restrict 禁止删除被引用的行
--a integer references a(q) on update cascade
b integer
);
insert into c values(2,1);
delete from a where q = 2; --ERROR: update or delete on table "a" violates foreign key constraint "c_a_fkey" on table "c" DETAIL: Key (q)=(2) is still referenced from table "c".
-------------------
----3.2 删除表
drop table tablename;
----3.3 修改表
alter table test_table1 add column add_column text not null; --增加一个字段
alter table test_table1 drop column add_column;
--如果该表是主表,该字段是被引用字段,那么该操作将会失败
--如果想要在删除的时候删除引用字段的所有关联数据,可以采用以下方式
alter table a drop column q cascade
一、Postgresql的基本操作的更多相关文章
- PostgreSQL基本配置
记一下Postgresql的基本操作,在Ubuntu下使用apt-get安装是不会像MySQL那样都配置好了,而是要安装后再配置: 1. 基本安装 # 安装postgresql和pgadmin(一个管 ...
- PostgreSQL自学笔记:3 数据库的基本操作
3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...
- PostgreSQL基础知识与基本操作索引页
磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 luckyjackgao@gmail.com 本页记录所有本人所写的PostgreSQL的基础知识和基本操作相关文摘和 ...
- postgresql基本操作:查看数据库、索引、表、表空间大小
一.简介 PostgreSQL 提供了多个系统管理函数来查看表,索引,表空间及数据库的大小,下面详细介绍一下. 二.数据库对象尺寸函数 函数名 返回类型 描述 pg_column_size(any) ...
- postgreSql 基本操作总结
0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 命令行登录数据库 1 psql -U username -d dbname -h hostip -p po ...
- PostgreSQL基本操作
列出当前数据库所有表 \dt 列出表名 SELECT tablename FROM pg_tables; WHERE tablename NOT LIKE 'pg%' AND tablename NO ...
- postgreSql基础命令及linux下postgreSql命令
(1)用户实用程序: createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同) createuser 创建一个新的PostgreSQL的用户(和 ...
- 关于ubuntu服务器上部署postgresql 以及安装pgadmin4管理工具(web版)
进入目录:cd pgadmin4 source bin/activate cd pgadmin4-1.6/ 启动pgadmin4:python web/pgAdmin4.py pgadmi ...
- Centos 7.3 安装配置 PostgreSQL 9.x
一.安装 PostgresSQL Centos 7 自带的 PostgresSQL 是 9.2 版的.因为,yum 已经做了国内源,速度飞快,所以直接就用 yum 安装了.依次执行以下命令即可,非常简 ...
随机推荐
- 「NOIP2017」「LuoguP3959」 宝藏(爆搜
题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 nn 个深埋在地下的宝藏屋, 也给出了这 nn 个宝藏屋之间可供开发的mm 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏. ...
- 【Python】numpy 数组拼接、分割
摘自https://docs.scipy.org 1.The Basics 1.1 numpy 数组基础 NumPy’s array class is called ndarray. ndarray. ...
- Python定时任务-schedule vs. Celery vs. APScheduler
在Python开发过程中我们经常需要执行定时任务,而此类任务我们通常有如下选项: 自己造轮子 使用schedule库 使用Celery定时任务 使用APScheduler 自己造轮子实现,最大的优势就 ...
- windows下vs2012用gsoap开发webservice实例
零:说明 1.本文是根据网上前人经验结合自己动手操作写成,开发工具用的vs2012,gsoap用的是gsoap-2.8: 2.gsoap提供的工具简单介绍 1)wsdl2h.exe:根据WSDL文件生 ...
- Day04:函数参数、对象、嵌套、闭包函数和装饰器
上节课复习: 1.什么是函数 函数就是具备某一功能的工具 2.为何用函数 1.程序的组织结构和可读性 2.减少代码冗余 3.扩展性强 ...
- java 基础知识学习 JVM虚拟机参数配置
1) 设置-Xms.-Xmx相等: 2) 设置NewSize.MaxNewSize相等: 3) 设置Heap size, PermGen space: Tomcat 的配置示例:修改%TOMCAT_H ...
- python中列表元组字符串相互转换
python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示: >>> s = "xxxxx ...
- JVM StackOverflowError vs. OutOfMemoryError
if the computation in a thread needs a larger Java Virtual Machine stack than is permitted, the Java ...
- GroupItem ContextMenu Command mvvm
参照:https://stackoverflow.com/questions/38009642/how-do-i-bind-the-menuitem-of-a-contextmenu-of-a-dat ...
- Elasticsearch检索分类详解
前言 Elasticsearch中当我们设置Mapping(分词器.字段类型)完毕后,就可以按照设定的方式导入数据. 有了数据后,我们就需要对数据进行检索操作.根据实际开发需要,往往我们需要支持包含但 ...