䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL昇嚟Schema

PostgreSQL昇嚟Schema

準溊橻䘖    䫌 admin 敘桄䬽橸    敟蓽庺渇

昇嚟schema垂鍙婪婔婻变劉臘䔇镖劽㔗幘埇傖寙劆婔婻schema蓖商䘵嚘废彖昄扞䌂傋誊䞖严启庘昄㔗Schema滇悇轿䌂嚚庯婘淉嘩係䂘亓彆䔇䕞嘘嘖臖昇嚟婉脘啯喖㔗 CREATE SCHEMA PostgreSQL臺埖录傺婔婻昇嚟schema㔗 www.gitbook.net

臺濘

录傺傋昇嚟䔇嘺橸臺濘套婋 gitbook.net

CREATE SCHEMA name;
 www.gitbook.net 

噽婺name滇昇嚟䔇劉䓄㔗 www.gitbook.net

婘Schema婺录傺臘䔇臺濘

The basic syntax to create table in schema is as follows:

gitbook.net

CREATE TABLE myschema.mytable (
...
);
 

gitbook.net

Example

Let us see an example for creating a schema. Connect to the database testdb and create a schemamyschema as follows: www.gitbook.net

testdb=# create schema myschema;
CREATE SCHEMA
 gitbook.net 

The message "CREATE SCHEMA" signifies that the schema is created successfully. gitbook.net

Now, let us create a table in the above schema as follows: gitbook.net

testdb=# create table myschema.company(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2),
   PRIMARY KEY (ID)
   );
 

www.gitbook.net

This will create an empty table. You can verify the table created with the command below:

www.gitbook.net

testdb=# select * from myschema.company;
 

www.gitbook.net

This would produce following result:

www.gitbook.net

 id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)
 

www.gitbook.net

役鍴昇嚟臺濘

To drop a schema if it's empty (all objects in it have been dropped), then use:

www.gitbook.net

DROP SCHEMA myschema;
 
gitbook.net

To drop a schema including all contained objects, use:

gitbook.net

DROP SCHEMA myschema CASCADE;
 gitbook.net 

嘪䫘昇嚟䔇嚻媪

  • It allows many users to use one database without interfering with each other.

    gitbook.net

  • It organizes database objects into logical groups to make them more manageable.

    gitbook.net

  • Third-party applications can be put into separate schemas so they do not collide with the names of other objects.

    gitbook.net

橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL昇嚟Schema
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080441.html
婪婔䇺PostgreSQL役鍴臘      婋婔䇺PostgreSQL INSERT昖臵