如下所示:
-- 建表
CREATE TABLE if not exists public.user
(
id character varying(32) NOT NULL DEFAULT sys_guid(),
name character varying(100) NOT NULL,
gender character varying(50) NOT NULL,
age character varying(10) NOT NULL,
id_no character varying(50) NOT NULL,
created_date timestamp without time zone DEFAULT now(),
created_by character varying(100) DEFAULT 'system',
updated_date timestamp without time zone DEFAULT now(),
update_by character varying(100) DEFAULT 'system',
CONSTRAINT user_pkey PRIMARY KEY (id)
)with (oids = false);
-- 注釋
COMMENT ON TABLE public.user IS '用戶表';
COMMENT ON COLUMN public.user.id IS '主鍵';
COMMENT ON COLUMN public.user.name IS '姓名';
COMMENT ON COLUMN public.user.gender IS '性別';
COMMENT ON COLUMN public.user.age IS '年齡';
COMMENT ON COLUMN public.user.id_no IS '身份證號(hào)';
COMMENT ON COLUMN public.user.created_date IS '創(chuàng)建時(shí)間';
COMMENT ON COLUMN public.user.created_by IS '創(chuàng)建人';
COMMENT ON COLUMN public.user.updated_date IS '更新時(shí)間';
COMMENT ON COLUMN public.user.update_by IS '更新人';
-- 主鍵 (如果建表語句里面沒添加主鍵就執(zhí)行該語句)
alter table public.user
add constraint user_pkey primary key (id);
-- 索引或唯一索引
drop index if exists user_name;
CREATE INDEX user_name ON user (name);
drop index if exists user_id_no;
CREATE UNIQUE INDEX user_id_no ON user (id_no);
-- 授權(quán)
GRANT ALL ON TABLE public.user TO mydata;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.user TO mydata_dml;
GRANT SELECT ON TABLE public.user TO mydata_qry;
補(bǔ)充:postgresql基本功能:創(chuàng)建表、新增列、修改列字段名稱、某列值自增或循環(huán)自增、
根據(jù)現(xiàn)有表創(chuàng)建新表:
CREATE TABLE "test04" AS ( select * from testdemo);
修改數(shù)據(jù)表名:
alter table table_name(表名) rename to new_table_name(新表名)
新增列字段:
ALTER TABLE test04 ADD gid1_type integer;
刪除列字段:
ALTER TABLE test04 DROP COLUMN gid1_type;
修改列字段名稱:
alter table test05 RENAME "gid" TO "id";
修改列字段類型:
ALTER TABLE test05 ALTER COLUMN "gid" TYPE datatype;
特殊的修改為integer:
alter table table_name(表名) alter column 字段名 type 新字段類型 using to_number(字段名,'9')
更新字段數(shù)據(jù):
update test05 set "gid1_type" = 0 where ("組分類型1" = '消防栓') or ("組分類型1" = '水表') or ("組分類型1" = '節(jié)點(diǎn)');
在postgresql中,設(shè)置已存在的某列(num)值自增:
//將表tb按name排序,利用row_number() over()查詢序號(hào)并將該列命名為rownum,創(chuàng)建新表tb1并將結(jié)果保存到該表中
create table tb1 as (select *, row_number() over(order by name) as rownum from tb);
//根據(jù)兩張表共同的字段name,將tb1中rownum對(duì)應(yīng)值更新到tb中num中
update tb set num=(select tb1.rownum from tb1 where tb.name = tb1.name);
//判斷表tb1的存在并刪除表
drop table if exists tb1;
在postgresql中,循環(huán)設(shè)置已存在的某列(num)值為0-9:
//將表tb按name排序,利用row_number() over()查詢序號(hào)并將該列命名為rownum,創(chuàng)建新表tb1并將結(jié)果保存到該表中
create table tb1 as (select *, row_number() over(order by name) as rownum from tb);
//根據(jù)兩張表共同的字段name,將tb1中rownum對(duì)應(yīng)值更新到tb中num中,由于為0-9循環(huán)自增,則%10
update tb set num=(select tb1.rownum from tb1 where tb.name = tb1.name) % 10;
//判斷表tb1的存在并刪除表
drop table if exists tb1;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Postgresql 數(shù)據(jù)庫 varchar()字符占用多少字節(jié)介紹
- 解決postgresql 數(shù)字轉(zhuǎn)換成字符串前面會(huì)多出一個(gè)空格的問題
- postgreSQL 數(shù)字與字符串類型轉(zhuǎn)換操作
- postgresql 導(dǎo)出建表語句的命令操作
- postgresql 實(shí)現(xiàn)更新序列的起始值
- postgresql修改自增序列操作
- PostgreSQL數(shù)據(jù)類型格式化函數(shù)操作