postgres學習筆記

字段基本類型

int
char(num)
varchar(num)
serial
更多詳情

限制

not null
primary key
unique

登錄

psql -U dbuser  默認登錄進入名爲dbuser的數據庫中,如果不存在請-d指定相應進入的數據庫
psql -U username -p port -h localhost -d dbname

命令行

創建數據庫用戶具有超級權限
createuser dbuser -s ;

createdb dbname ;
password dbuser ;

psql 進入postgres

基本命令
    \l  顯示當前所有的數據庫
    \c dbname   切換進入數據庫
    \c  查看當前的連接信息
    \d  當前庫的所有表信息
    \d tablename 當前表的所有字段信息
    \q  退出登錄

創建用戶

create user dbuser ;
password dbuser ;

create user dbuser with password 'pwd' ;
create database dbname owner dbuser ;

授權

grant all privileges on database dbname to dbuser ;

創建數據庫

create database dbname ;
drop database dbname ;

備份數據

pg_dump -h host -U user dbname >  file.sql.bak

還原數據

psql -h host -U user -d dbname < file.sql.bak

創建新表

//serial 指定自增
create table users(id serial primary key,name varchar(20) not null,info text,lasttime date,age int not null default 23) ;

插入數據

INSERT INTO user_tbl(name, signup_date) VALUES(';張三';, ';2013-12-22';);

選擇記錄

select * from tablename limit length offset start ;

更新數據

UPDATE user_tbl set name = '李四'  WHERE name = '張三' ;

刪除記錄

DELETE FROM user_tbl WHERE name = '李四' ;

清空表數據

truncate table tset1
delete from tset1

添加欄位

ALTER TABLE user_tbl ADD email VARCHAR(40);

更新結構

ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
alter table gdt_accounts alter system_industry_id type varchar(12) ;

更名欄位

ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;

刪除欄位

ALTER TABLE user_tbl DROP COLUMN email;

表格更名

ALTER TABLE user_tbl RENAME TO backup_tbl;

刪除表格

DROP TABLE IF EXISTS backup_tbl;

sql優化

postgreSQL獲取數據庫中所有table表名

SELECT   tablename   FROM   pg_tables  
WHERE   tablename   NOT   LIKE   'pg%'  
AND tablename NOT LIKE 'sql_%'
ORDER   BY   tablename;

postgreSQL獲取某個表tablename所有字段名稱 , 類型,備註,是否爲空 等

SELECT a.attname as name, pg_type.typname as typename, col_description(a.attrelid,a.attnum) as comment,a.attnotnull as notnull
FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid
where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0

postgreSQL獲取某個表tablename的主鍵信息

select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from 
pg_constraint  inner join pg_class 
on pg_constraint.conrelid = pg_class.oid 
inner join pg_attribute on pg_attribute.attrelid = pg_class.oid 
and  pg_attribute.attnum = pg_constraint.conkey[1]
inner join pg_type on pg_type.oid = pg_attribute.atttypid
where pg_class.relname = 'tablename' 
and pg_constraint.contype='p'

PostgreSQL獲取數據庫中所有view名視圖

SELECT   viewname   FROM   pg_views  
WHERE     schemaname ='public'  

添加註釋

postgresql給列添加註釋信息
    create table user( userid int not null, phonenumber int); comment on column user.userid is 'The user ID';
postgresql給表添加註釋信息
    comment on table user is 'Our session logs';
postgresql 查詢註釋信息
    select description from pg_descriptionjoin pg_class on pg_description.objoid = pg_class.oidwhere relname = 'user'
查詢指定schema下注釋信息
    select description from pg_description join pg_class on pg_description.objoid = pg_class.oid join pg_namespace on pg_class.relnamespace = pg_namespace.oid where relname = '' and nspname=''
添加索引

創建索引

CREATE INDEX indexname ON test1 (column);    

分析優化

explain analyze  select 1 from tbl_user where user_spell like '%CYL%';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章