postgreSql 入门常用命令
2015-07-22 15:51
543 查看
1. 命令行登录数据库
psql -U username -d dbname -h hostip -p port
2. 列出所有数据库
\l
3. 切换数据库
\c dbname
4. 列出当前数据库的所有表
\d
5. 查看指定表的所有字段
\d tablename
6. 查看指定表的基本情况
\d+ tablename
7. 退出操作
q
8. 新建表
create table REL_CROSS_NODE(ID integer, CROSS_ID integer);
9. 删除表
drop table REL_CROSS_NODE;
10. 清空表
delete from [表名]
11. 添加字段
alter table [表名] add column [字段名] [类型];
12. 更改字段
alter table [表名] rename column [旧字段名] to [新字段名]; 例:把表table_ex字段col_1限制非空去掉:ALTER TABLE table_eg ALTER col_1 drop not NULL
13. 删除字段
alter table [表名] drop column [字段名];
14. 表中加入一行数据
insert into [表名] (字段1,字段2) values (值1,值2);
例如:
insert into assist_info (id, maat_id, block_type) values ('F006', 'F7775', 1)
注:id,maat_id,block_type字段名字不能加引号
15. 表中删除一行数据
delete from [表名] where [该行特征];
16. 修改表中数据
update [表名] set [目标字段名]=[目标值] where [该行特征]
17. 删除表
drop table [表名];
18. 退出postgreSql
\q
19. 两个查询结果做差 except
(select node_id from node where node_id=1 or node_id=2) except (select node_id from node where node_id=1); node_id --------- 2 (1 row)
20. 复制表
CREATE TABLE test_a_copy AS SELECT * FROM test_a;
21.命令导入sql数据文件
psql -h localhost -d databaseName -U username -f filename
22. 查询结果存储到输出文件
格式:\o file_path
这样就会把查询结果存储到输出文件中。例
postgres=> \o /home/jihite/data/iu_data; postgres=> select test_id from cdb_all_iu_data limit 10; postgres=> select test_id from cdb_all_iu_data limit 5;
结果
test_id -------------- 2143 2153 2144 2156 2145 2154 2146 2157 2147 2155 (10 rows) test_id -------------- 2143 2153 2144 2156 2145 (5 rows)
23. 数据库的备份&恢复
pg_dumphttp://blog.chinaunix.net/uid-20802110-id-4825981.html
相关文章推荐
- SQL行转列(case when then )
- (实验小结)主从配置不同步的解决(小白踩雷点)
- ORACLE日期时间函数大全
- IndexedDB:浏览器里内置的数据库(转)
- mybatis按时间查询oracle数据
- 连接数据库问题
- Sqlyog11注册码\下载地址
- CentOS搭建nginx+tomcat+laravel+php+Mongodb环境1(nginx篇)
- [基本实验] 使用SQLMap
- com.mysql.jdbc.exceptions.MySQLSyntaxErrorException
- 关于Oracle表连接
- redis + Jedis
- Redis和Memcache 区别
- 遇到一个数据库链接未释放问题
- oracle sql 汉字在库中占的字节数
- MongoDB学习——持续更新
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- 基于zookeeper的redis高可用
- 处理 Oracle SQL in 超过1000 的解决方案
- Mycat(5):聊天消息表数据库按月分表实践,平滑扩展