`
zgqynx
  • 浏览: 1355633 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 Database 11g Introduction to SQL

    Oracle Database 11g是Oracle公司推出的一款关系型数据库管理系统,其Introduction to SQL教程主要针对初学者,旨在帮助用户理解并掌握SQL语言的基础知识,同时也会涉及到与Oracle数据库相关的PL/SQL编程。...

    introduction to no-sql

    NoSQL,泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的...

    Introduction to SQL (1).ppt

    在"Introduction to SQL (1).ppt"中,我们主要探讨了SQL的基础知识,包括快速理解SQL、关系代数以及更深入的SQL概念。 首先,SQL在45分钟内的概述(对应Chapter 5)旨在让读者快速掌握SQL的基本用法。这部分可能...

    加州大学伯克利分校的《Introduction to SQL》课件

    "加州大学伯克利分校的《Introduction to SQL》课件" 《Introduction to SQL》课件是加州大学伯克利分校出品的SQL教学资源,总共165页,纯英文,PDF格式。下面是该课件中所涉及的知识点: 1. 什么是SQL?SQL...

    Introduction to SQL Injection

    ### SQL注入简介与防范策略 #### 一、SQL注入概览 SQL注入是一种常见的网络攻击方式,通过在应用程序中插入恶意SQL代码,攻击者能够操纵数据库执行非授权操作,如篡改数据、泄露敏感信息或破坏数据库结构。本文将...

    Introduction To ORACLE9i:SQL3

    《Oracle9i: SQL3入门指南》一书深入探讨了Oracle9i数据库系统中的SQL语言高级应用,旨在为读者提供全面、深入的SQL知识体系,适用于Oracle认证专业人员(OCP)的学习与备考。以下是对该书籍核心知识点的详细解析: ...

    introduction to PL-sql

    introduction to PL-sql

    Introduction to SQL - Stanford University

    【SQL简介】 SQL(Structured Query Language)是用于管理和处理关系数据库的标准编程语言。它以其简单易学和高效性著称,允许用户无需关注具体的数据处理细节,而是专注于描述想要执行的操作。SQL的设计哲学是让...

    introduction to -sql-plsql1

    从给定的文件信息来看,文档标题和描述均标记为"introduction to -sql-plsql1",这表明文档旨在介绍SQL与PL/SQL的基本概念和技术。然而,文档的实际内容并未提供具体关于SQL或PL/SQL的信息,而是包含了版权、使用...

    Introduction to Oracle SQL and PL/SQL

    《Introduction to Oracle SQL and PL/SQL》是一本专为初学者设计的教程,全面涵盖了Oracle数据库管理系统中的核心语言——SQL(结构化查询语言)和PL/SQL(过程化语言/SQL)。这本书分为两卷,旨在帮助读者从基础到...

    D33051 Introduction to oracle 9i_sql v1.pdf

    D33051 Introduction to oracle 9i_sql v1.pdf

    Introduction To ORACLE9i:SQL2

    根据提供的文件信息,我们可以从中提炼出与Oracle 9i SQL2相关的多个重要知识点。下面将对这些知识点进行详细的阐述。 ### Oracle 9i SQL2简介 #### 1. Oracle 9i SQL2背景 Oracle 9i SQL2是Oracle公司推出的一款...

    Introduction to Oracle9i : SQL

    Introduction to Oracle9i: SQL Electronic Presentation 英文版的

    Introduction to Oracle9i: SQL

    Introduction to Oracle9i: SQL Electronic Presentation 40049GC11 Production 1.1 October 2001

    D33052 Introduction to oracle 9i_sql v2.pdf

    D33052 Introduction to oracle 9i_sql v2.pdf

    Introduction to Oracle - Sql Plsql (Vol2)

    本文档“Introduction to Oracle - Sql Plsql (Vol2)”介绍了Oracle数据库的核心组件:SQL和PL/SQL的基础知识及其高级应用。作为Oracle系列教程的一部分,它适用于那些已经对Oracle环境有所了解并希望进一步深入学习...

    Introduction To ORACLE9i PL/SQL(所有讲解及习题)

    本资料“Introduction To ORACLE9i PL/SQL”提供了全英文的学习资源,适合对数据库编程有一定基础的读者深入学习。 PL/SQL全称为Procedural Language/Structured Query Language,即过程化结构化查询语言。它是...

    introduction to oracle - sql plsql

    ### Oracle SQL与PL/SQL简介 #### 一、Oracle数据库概览 Oracle数据库是全球领先的数据库管理系统之一,广泛应用于各种企业级应用环境之中。它以其卓越的性能、可靠性及安全性而闻名于世。本篇文章旨在介绍Oracle...

Global site tag (gtag.js) - Google Analytics