INTRODUCTION TO SQL

建议看原文,格式比较好:
http://www.cs.rpi.edu/~sibel/dbs/FALL2003/system_info/oracle/sql_ddcmd.htm#alter
SQL Data Definition Language Commands (DDL)

This page contains some useful SQL DDL commands. Each command's description is taken and modified from the SQL*Plus help. They are partially described. So, if you want more detail or other commands, please use HELP in the SQL*Plus directly.

ALTER TABLE

PURPOSE:

To alter the definition of a table in one of these ways:

    *

      to add a column
    *

      to add an integrity constraint
    *

      to redefine a column (datatype, size, default value)
    *

      to modify storage characteristics or other parameters
    *

      to enable, disable, or drop an integrity constraint or trigger
    *

      to explicitly allocate an extent
    *

      to allow or disallow writing to a table
    *

      to modify the degree of parallelism for a table 

SYNTAX:

     ALTER TABLE [schema.]table 
        [ADD {    { column datatype [DEFAULT expr] [column_constraint] ... 
                  | table_constraint} 
             |  ( { column datatype [DEFAULT expr] [column_constraint] ... 
                  | table_constraint} 
               [, { column datatype [DEFAULT expr] [column_constraint] ... 
                  | table_constraint} ] ... ) } ] 
        [MODIFY {   column [datatype] [DEFAULT expr] [column_constraint] ... 
                |  (column [datatype] [DEFAULT expr] [column_constraint] ... 
     [, column datatype [DEFAULT expr] [column_constraint] ...] ...) } ] 
        [DROP drop_clause] ... 

Where:

    *

      schema : is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema.
    *

      table : is the name of the table to be altered.
    *

      ADD : adds a column or integrity constraint.
    *

      MODIFY : modifies a the definition of an existing column. If you omit any of the optional parts of the column definition (datatype, default value, or column constraint), these parts remain unchanged.
    *

      column : is the name of the column to be added or modified.
    *

      datatype : specifies a datatype for a new column or a new datatype for an existing column.
    *

      DEFAULT : specifies a default value for a new column or a new default for an existing column. Oracle assigns this value to the column if a subsequent INSERT statement omits a value for the column. The datatype of the default value must match the datatype specified for the column. A DEFAULT expression cannot contain references to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that are not fully specified.
    *

      column_constraint : adds or removes a NOT NULL constraint to or from and existing column.
    *

      table_constraint : adds an integrity constraint to the table. 

PREREQUISITES:

The table must be in your own schema or you must have ALTER privilege on the table or you must have ALTER ANY TABLE system privilege. 

Example:

To add an advisor column into the Student table, enter:


    SQL> ALTER TABLE Student ADD (advisor VARCHAR2(30));

    Table altered.

See also: CONSTRAINT, CREATE TABLE, DISABLE, DROP, ENABLE, STORAGE

CREATE TABLE

PURPOSE:

To create a table, the basic structures to hold user data, specifying this information are:

    *

      column definitions
    *

      integrity constraints
    *

      the table's tablespace
    *

      storage characteristics
    *

      an optional cluster
    *

      data from an arbitrary query 

SYNTAX:

 
     CREATE TABLE [schema.]table 
          ( { column datatype [DEFAULT expr] [column_constraint] ... 
            | table_constraint} 
         [, { column datatype [DEFAULT expr] [column_constraint] ... 
            | table_constraint} ]...) 
         [AS subquery] 

Where:

    *

      schema : is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema.
    *

      table : is the name of the table to be created.
    *

      column : specifies the name of a column of the table. The number of columns in a table can range from 1 to 254.
    *

      datatype : is the datatype of a column.
    *

      DEFAULT : specifies a value to be assigned to the column if a subsequent INSERT statement omits a value for the column. The datatype of the expression must match the datatype of the column. A DEFAULT expression cannot contain references to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, and ROWNUM, or date constants that are not fully specified.
    *

      column_constraint : defines an integrity constraint as part of the column definition.
    *

      table_constraint : defines an integrity constraint as part of the table definition.
    *

      AS subquery : inserts the rows returned by the subquery into the table upon its creation. If you include this clause, the column definitions can only specify column names, default values, and integrity constraints, not datatypes. Oracle derives column datatypes and lengths from the subquery. Oracle also automatically defines NOT NULL constraints on columns in the new table if they existed on the corresponding columns of the selected table and the subquery does not modify the column value with a SQL function or operator. A CREATE TABLE statement cannot contain both the AS clause and a referential integrity constraint definition.
      The number of columns must equal the number of expressions in the subquery. If all expressions in the subquery are columns, you can omit the columns from the table definition entirely. In this case, the names of the columns of table are the same as the columns in the subquery. 

PREREQUISITES:

To create a table in your own schema, you must have CREATE TABLE system privilege. To create a table in another user's schema, you must have CREATE ANY TABLE system privilege. Also, the owner of the schema to contain the table must have either space quota on the tablespace to contain the table or UNLIMITED TABLESPACE system privilege.

Example:

To add an advisor column into the Student table, enter:


    SQL> CREATE TABLE Student 
         ( Name  VARCHAR2(30), 
           StudentNumber NUMBER(4) NOT NULL, 
           Class NUMBER(4), 
           Major VARCHAR2(4), 
           Primary key (StudentNumber) 
         ); 


    Table created. 

See also: ALTER TABLE, CONSTRAINT, CREATE CLUSTER, CREATE INDEX, CREATE

CREATE VIEW

PURPOSE:

To define a view, a logical table based on one or more tables or views.

SYNTAX:

     CREATE [OR REPLACE] [FORCE | NOFORCE] VIEW [schema.]view
         [(alias [,alias]...)]
         AS subquery
         [WITH CHECK OPTION [CONSTRAINT constraint]]

Where:

    *

      OR REPLACE : recreates the view if it already exists. You can use this option to change the definition of an existing view without dropping, recreating, and regranting object privileges previously granted on it.
    *

      FORCE : creates the view regardless of whether the view's base tables exist or the owner of the schema containing the view has privileges on them. Note that both of these conditions must be true before any SELECT, INSERT, UPDATE, or DELETE statements can be issued against the view.
    *

      NOFORCE : creates the view only if the base tables exist and the owner of the schema containing the view has privileges on them. The default is NOFORCE.
    *

      schema : is the schema to contain the view. If you omit schema, Oracle creates the view in your own schema.
    *

      view : is the name of the view.
    *

      alias : specifies names for the expressions selected by the view's query. The number of aliases must match the number of expressions selected by the view. Aliases must follow the rules for naming schema objects. Aliases must be unique within the view.
      If you omit the aliases, Oracle derives them from the columns or column aliases in the view's query. For this reason, you must use aliases if the view's query contains expressions rather than only column names.
    *

      AS subquery : identifies columns and rows of the table(s) that the view is based on. A view's query can be any SELECT statement without the ORDER BY or FOR UPDATE clauses. Its select list can contain up to 254 expressions.
    *

      WITH CHECK OPTION : specifies that inserts and updates performed through the view must result in rows that the view query can select. The CHECK OPTION cannot make this guarantee if there is a subquery in the query of this view or any view on which this view is based.
    *

      CONSTRAINT : is the name assigned to the CHECK OPTION constraint. If you omit this identifier, Oracle automatically assigns the constraint a name of this form:

          SYS_Cn :
          where
          n
          is an integer that makes the constraint name unique within the database. 

PREREQUISITES:

To create a view in your own schema, you must have CREATE VIEW system privilege. To create a view in another user's schema, you must have CREATE ANY VIEW system privilege.

Example:

To create a view CSStudent (computer science students) from the Student table, enter:


    SQL> CREATE OR REPLACE VIEW CSStudent 
         AS SELECT *
            FROM  Student 
            WHERE Major = 'CS';


    View created. 

See also: CREATE TABLE, CREATE SYNONYM, DROP VIEW, RENAME

DROP TABLE

PURPOSE:

To remove a table and all its data from the database.

SYNTAX:


     DROP TABLE [schema.]table 
         [CASCADE CONSTRAINTS] 

Where:

    *

      schema : is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema.
    *

      table : is the name of the table to be dropped.
    *

      CASCADE CONSTRAINTS : drops all referential integrity constraints that refer to primary and unique keys in the dropped table. If you omit this option, and such referential integrity constraints exist, Oracle returns an error and does not drop the table. 

PREREQUISITES:

The table must be in your own schema or you must have DROP ANY TABLE system privilege.

Example:

To drop the Student table, enter:


    SQL> DROP TABLE Student;

    Table dropped.

See also: ALTER TABLE, CREATE INDEX, CREATE TABLE, DROP CLUSTER

DROP VIEW

PURPOSE:

To remove a view from the database.

SYNTAX:


     DROP VIEW [schema.]view

Where:

    *

      schema : is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema.
    *

      view : is the name of the view to be dropped. 

PREREQUISITES:

The view must be in your own schema or you must have DROP ANY VIEW system privilege.

Example:

To drop the CSStudent view, enter:


    SQL> DROP VIEW CSStudent;

    View dropped.

See also: CREATE SYNONYM, CREATE TABLE, CREATE VIEW 

你可能感兴趣的:(oracle,sql,UP)