命令行操作
如果你键入你的
SQL语句(通过命令行的工具psql),你需要在末尾加入一个分号。分号告诉psql已经到达命令的末尾了,因为很长的命令可能扩展到不止一行。
psql的基本命令
命令
|
描述
|
\?
|
获得帮助消息
|
\do
|
列出操作类型
|
\dt
|
列出表
|
\dT
|
列出类型
|
\h
|
列出SQL命令的帮助;用实际的命令代替
|
\i
|
执行文件里头的命令
|
\q
|
退出psql
|
使用psql工具:
/opt/PostgresPlus/9.2AS/bin/psql -U [-d]
-
[root@edb ~]# /opt/PostgresPlus/9.2AS/bin/psql -U enterprisedb edb
-
Password for user enterprisedb:
-
psql (9.2.1.3)
-
Type "help" for help.
edb=# \l 查看全部数据库名
如何创建TABLE?在不知道语法的时候,可以通过 \h 命令名 查询。
-
edb=# \h create table
-
Command: CREATE TABLE
-
Description: define a new table
-
Syntax:
-
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
-
{ column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
-
| table_constraint
-
| LIKE source_table [ like_option ... ] }
-
[, ... ]
-
] )
-
[ INHERITS ( parent_table [, ... ] ) ]
-
[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
-
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
-
[ TABLESPACE tablespace_name ]
建立一个空的TABLE
-
edb=# create table textnew();
-
CREATE TABLE
-
edb=#
通过\dt查看数据库里面的全部表
在空的TABLE里增加字段
alter table add ;
-
edb=# \dt textnew;
-
List of relations
-
Schema | Name | Type | Owner
-
--------------+---------+-------+--------------
-
enterprisedb | textnew | table | enterprisedb
-
(1 row)
-
-
edb=# select * from textnew;
-
--
-
(0 rows)
-
-
edb=# alter table textnew add uid serial;
-
NOTICE: ALTER TABLE will create implicit sequence "textnew_uid_seq" for serial column "textnew.uid"
-
ALTER TABLE
-
edb=# alter table textnew add u_name character(10);
-
ALTER TABLE
-
edb=# select * from textnew;
-
uid | u_name
-
-----+--------
-
(0 rows)
删除字段:
alter table drop ;
-
edb=# alter table textnew add u_passwd character(10);
-
ALTER TABLE
-
-
edb=# select * from textnew;
-
uid | u_name | u_passwd
-
-----+--------+----------
-
(0 rows)
-
-
edb=# alter table textnew drop u_passwd;
-
ALTER TABLE
建立主键
alter table add constraint primary key(column_name);
-
edb=# alter table textnew add constraint pk_uid primary key(uid);
-
ALTER TABLE
删除表:
-
edb=# drop table textnew;
-
DROP TABLE
提高建表的效率:(通过以下方法,可以快速批量地建立数据库和表结构)
第一种:
-
edb=# create table testnew(uid serial,u_name varchar(10),u_passwd varchar(10),constraint pk_uid primary key(uid));
-
NOTICE: CREATE TABLE will create implicit sequence "testnew_uid_seq" for serial column "testnew.uid"
-
CREATE TABLE
-
edb=# select * from testnew;
-
uid | u_name | u_passwd
-
-----+--------+----------
-
(0 rows)
第二种:
在当前目录下编写一个.sql文件
-
[root@edb /]# cat createTB.sql
-
CREATE TABLE customer(
-
customer_id serial,
-
title char(4),
-
fname varchar(32),
-
lname varchar(32) NOT NULL,
-
addressline varchar(64),
-
town varchar(32),
-
zipcode char(10) NOT NULL,
-
phone varchar(16),
-
CONSTRAINT customer_pk PRIMARY KEY (customer_id)
-
);
-
edb=# i createTB.sql
-
psql:createTB.sql:11: NOTICE: CREATE TABLE will create implicit sequence "customer_customer_id_seq" for serial column "customer.customer_id"
-
CREATE TABLE
-
edb=# dt customer;
-
List of relations
-
Schema | Name | Type | Owner
-
--------------+----------+-------+--------------
-
enterprisedb | customer | table | enterprisedb
-
(1 row)
第三种:选择数据库 --> 运行SQL语句
编写SQL语句:
运行指定的SQL语句:
阅读(2928) | 评论(0) | 转发(0) |