博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Chatper 10-Using DDL Statements to Create and Manage Tables
阅读量:4626 次
发布时间:2019-06-09

本文共 3144 字,大约阅读时间需要 10 分钟。

Objectives

  • After completing this lesson,you should be able to do the following:
  • Categorize the main database objects
  • Review the table structure
  • List the data types that are available for columns.
  • Create a simple table
  • Explain how constraints are created at the time of table creation.
  • Describe how schema objects work.

Lesson Agenda

  • Database objects
    •  -Naming rules
  • CREATE TABLE statement
    •  -Access another user`s tables
    •  -DEFAULT option
  • Data types
  • Overview of constrains:NOT NULL,PRIMARY KEY,FOREIGN KEY,CHECK constraints
  • Creating a table using a subquery
    • ALTER TABLE
    •  -Read-only tables
  • DROP TABLE statement 

Naming Rules

Table names and column names:

  • Must begin with a letter
  • Must be 1-30 character long
  • Must contain only A-Z,a-z,0-9,_,$,and #
  • Must not duplicate the name of another object owned by the same user
  • Must not be an Oracle server-reserved word

CREATE TABLE Statement

  • You must have:
    •  -CREATE TABLE privilege.假如你想在别人的schema里面创建表的话,你必须用后CREATE ANY TABLE privilege.
    •  -A storage area 
CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]);
  • You specify:
    •  -Table name
    •  -Column name,column data type,and column size
  • Demo:CREATE A SMIPLE TABLE

Referencing Another User`s Tables

  • Tables belonging to other users are not in the user`s schema.
  • You should use the owner`s name as a prefix to those tables.

DEFAULT Option

  • Specify a default value for a column during an insert
...hire_date DATE DEFAULT SYSDATE,...
  • Literal values,expressions,or SQL functions are legal values
  • Another column`s name or a pseudocolumn are illegal values
  • The default data type must match the column data type. 
demo:CREATE TABLE T(        ID NUMBER(8),        NAME CHAR(10),        SEX CHAR(1) DEFAULT '1'    );

Oracle Data Types

可参考号文章

Including Constraints

  • Constraints enforce rules at he table level
  • Constraints prevent the deletion of a table if there are dependencies.
  • The following constraint types are valid:
    • -NOT NULL
    • -QUIQUE
    • -PRIMARY KEY
    • -FOREIGN KEY
    • -CHECK

Constraint Guidelines

  • You can name a constraint,or the Oracle server generates a name by using SYS_Cn format.
  • Create a constraint at either of the following times:
    •  -At the same time as the creation of the table
    •  -After the creation of the table
  • Define a constraint at he column or table level
  • View a constraint in the data dictionary

Define Constraints

Syntax:   

CREATE TABLE [schema.]table (  column datatype [DEFAULT expr]  [column_constraint],  ...  [table_constraint][,...] );

   Column-level constraint syntax: column [CONSTRAINT constraint_name] constraint_type,

   Table-level constaint syntax: column,... [CONSTRAINT constraint_name] constraint_type (column,...),

Demo

Example of a column-level constraint:   

CREATE TABLE employees(  employee_id NUMBER(6) CONSTRAINT emp_emp_id_pk PRIMARY KEY,  first_name VARCHAR2(20),  ... )

Example of a table-level constaint:   

CREATE TABLE employees(  employee_id NUMBER(6),  first_name VARCHAR2(20),  ...  job_id  VARCHAR2(10) NOT NULL,  CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID) );

UNIQUE Constraint 假如一列是Unqiue constraint,那么如果这一列中存在两个以上的NULL值得话,是否 违反Unique constraint呢?答案,否;

 

 

 

 

 

 

转载于:https://www.cnblogs.com/arcer/archive/2013/04/11/3014228.html

你可能感兴趣的文章
SpringBoot------ActiveMQ安装
查看>>
详细了解 int? 类型
查看>>
字符串匹配 ?kmp : hash
查看>>
mongod.service: control process exited, code=exited status=1
查看>>
c# 发送邮件、附件 分类: C# 2014-12-...
查看>>
对360来说,江湖上再无“搜狗”这个传说
查看>>
composer
查看>>
OpenCV特征点检测——ORB特征
查看>>
mysql的csv数据导入与导出
查看>>
leetcode笔记:Pascal's Triangle
查看>>
ASP.NET性能优化之构建自定义文件缓存
查看>>
apicloud UISearchBar 使用方法
查看>>
【spring+websocket的使用】
查看>>
mongo二维数组操作
查看>>
localStorage之本地储存
查看>>
Archlinux 交换左Ctrl和Cap键
查看>>
#openstack故障处理汇总
查看>>
搜索旋转排序数组 II
查看>>
20、docker swarm
查看>>
psp工具软件前景与范围文档
查看>>