postgresql命令操作

1.连接数据库
psql -h Server -p Port -U Username DatabaseName

2.创建数据库
postgres=# create database testdb;

3.查看数据库
postgres=# \l

4.删除数据库
postgres=# drop database testdb;

5.进入数据库
postgres=# \c testdb;

6.列出当前库所有表
testdb=# \dt

7.创建表
testdb=# create table account(
testdb(# user_id serial primary key,
testdb(# username varchar(50) unique not null,
testdb(# password varchar(50) not null);

create table products (
testdb(# product_no integer,
testdb(# name varchar(20),
testdb(# price numeric);

8.删除表
testdb=# drop table account;

9.创建模式
testdb=# CREATE SCHEMA myschema;

10.指定模式创建表
create table myschema.mytable(
user_id serial primary key,
username varchar(50) unique not null,
password varchar(50) not null);

11.删除模式里的对象
drop schema myschema CASCADE;

12.删除模式
drop schema myschema

13.插入数据
testdb=# insert into products values (1, 'chiness', 9.99);

14.查询数据
testdb=# select * from products;

15.更新数据
testdb=# update products set name = 'hyh' where product_no = 1;

16.删除表数据
testdb=# delete from products where name = 'hyh'; 字符串必须单引号

17.order by 升序,降序排列
testdb=# select from products order by price asc;
testdb=# select
from products order by price desc;

18.order by 多列排序
select * from products order by price,product_no asc;

19.group by分组
testdb=# select name, sum(price) from products group by name;
按名字分组统计每个名字下的价格总额

20.

猜你喜欢

转载自blog.51cto.com/haoyonghui/2376807