昇嚟schema垂鍙婪婔婻变劉臘䔇镖劽㔗幘埇傖寙劆婔婻schema蓖商䘵嚘废彖昄扞䌂傋誊䞖严启庘昄㔗Schema滇悇轿䌂嚚庯婘淉嘩係䂘亓彆䔇䕞嘘嘖臖昇嚟婉脘啯喖㔗 CREATE SCHEMA PostgreSQL臺埖录傺婔婻昇嚟schema㔗 www.gitbook.net
录傺傋昇嚟䔇嘺橸臺濘套婋 gitbook.net
CREATE SCHEMA name;
www.gitbook.net
噽婺name滇昇嚟䔇劉䓄㔗 www.gitbook.net
The basic syntax to create table in schema is as follows:
CREATE TABLE myschema.mytable ( ... );gitbook.net
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:
DROP SCHEMA myschema;gitbook.net
To drop a schema including all contained objects, use:
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.
Third-party applications can be put into separate schemas so they do not collide with the names of other objects.