PostgreSQL通过SQL语句创建表结构
在没有图形客户端,或图形客户端比较难用时,可以直接使用SQL语句来创建PostgreSQL的建表语句,下面以具体的创建user表的语句来进行演示。
建表语句:
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 '身份证号'; COMMENT ON COLUMN public.user.created_date IS '创建时间'; COMMENT ON COLUMN public.user.created_by IS '创建人'; COMMENT ON COLUMN public.user.updated_date IS '更新时间'; COMMENT ON COLUMN public.user.update_by IS '更新人';
主键
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);
授权
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;
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接