1. 程式人生 > >postgresql命令操作

postgresql命令操作

建模 int integer sca 必須 asc rom num tab

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.

postgresql命令操作