Basic operations (define, modify, delete)

1. Mode definition and deletion
(1) Definition statement: CREATE SCHEMA <mode name> AUTHORIZATION <user name>;
Example: define a student-course mode ST for user WANG

  CREATE SCHEMA “S-T” AUTHORIZATIONA WANG;

(2) If <mode name> is not specified, then <mode name> is implicitly <user name>.
(3) To create a pattern, the user calling this command must have database management authority, or have the authority of CREATE SCHEMA granted by the database administrator.
(4) CREATE TABLE, CREATE VIEW and GRANT clause
examples can be received in CREATE SCHEMA : Create a mode TEST for user ZHANG, and define a table TAB1 in it.

CREATE SCHEMA TEST AUTHORIZATIONA ZHANG
   CREATE TABLE TEB1(COL1 SMALLINT,
                   COL2 INT,
                   COL3 CHAR(20),
                   COL4 NUMERIC(10,3),
                   COL5 DECIMAL(5,2)
                   );  

(5) Delete mode
DROP SCHEMA <schema name><CASCADE|RESTRICT>
//CASCADE (cascade), RESTRICT (restriction)
2. Definition and deletion of
basic table (1) Define basic table:

CREATE TABLE<表名>(<列名><数据类型>[列级完整性约束条件]
                                   [,<列名><数据类型>[列级完整性约束条件]]
                                     …………
                                   [,<表级完整性约束条件>];
      例子:建立一个学生表
CREATE TABLE Student
                 (Son CHER(9) PRIMARYKEY,   //Son是主码
                 Sname CHAR(20) UNIQUE,   //Sname取值唯一
                 Ssex CHER(2),
                 Sage SMALLINT,
                 Sdept CHAR(20)
                 ); 

(2)
Relationship between schema and table : each basic table belongs to a schema, and a schema can contain multiple basic tables
. The method of defining basic table schema:
1. The schema name is clearly given in the table name: CREATE TABLE "ST" .Student //Student belongs to ST
2. Create the table at the same time in the create mode statement
3. Set the mode, so that when creating the table, it is not necessary to give the mode name in the table name.
Display the current search path: SHOW search_path;
administrator settings Search path: SET search_path TO “ST”, PUBLIC;
(3) Modify the basic table:

            ALTER TABLE<表名>//修改表
            [ADD [COLUMN]<新列名><数据类型>[完整性约束]//增加新列
            [ADD <表级完整性约束>]//添加表级完整性约束
            [DROP [COMULN] <列名> [CASCADE|RESTRICT]//删除列
            [DROP CONSTRAINT<完整性约束名> [CASCADE|RESTRICT]]//用于删除指定的完整性约束条件
               [ALTER COLUMN <列名><数据类型> ];//用于修改原有的列定义,包括修改列明和数据类型

Examples:
1. Add the "Enrollment Time" column to the Student table, the data type is date type

 ALTER TABLE Student ADD S_entrance DATE;

2. Change the data type of age from character type (assuming the original data type is character type) to integer

  ALTER TABLE Student ALTER COLUMN Sage INT;

3. Add the condition that the course name must take a unique value

ALTER TABLE Conurse ADD UNIQUE(Cname);

Guess you like

Origin blog.csdn.net/weixin_43244265/article/details/105685338