posts - 58,  comments - 121,  trackbacks - 30

<三> 表(主键、外键、CHECK、UNIQUE、DEFAULT、INDEX)

    <1>、SQL SERVER端语法说明

有如下SQL SERVER语句:

/* ------------------------ 创建employee 表------------------------ */

IF EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME = ‘employee’

          AND TYPE = ‘U’)

    DROP TABLE employee

GO

 

CREATE TABLE employee

(

emp_id   empid    /*empid为用户自定义数据类型*/

/*创建自命名主键约束*/

    CONSTRAINT PK_employee PRIMARY KEY NONCLUSTERED

/*创建自命名CHECK约束*/

    CONSTRAINT CK_emp_id CHECK (emp_id LIKE

                     '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' or

                     emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]'),

    /* CHECK约束说明:Each employee ID consists of three characters that

    represent the employee's initials, followed by a five

    digit number ranging from 10000 to 99999 and then the

    employee's gender (M or F). A (hyphen) - is acceptable

    for the middle initial. */

fname     varchar(20)      NOT NULL,

minit     char(1)         NULL,

lname     varchar(30)      NOT NULL,

 

ss_id     varchar(9)        UNIQUE,    /*创建唯一性约束*/

 

job_id    smallint            NOT NULL

    DEFAULT 1,            /*设定DEFAULT值*/

job_lvl tinyint

   DEFAULT 10,            /*设定DEFAULT值*/

    /* Entry job_lvl for new hires. */

pub_id   char(4)         NOT NULL

    DEFAULT ('9952')        /*设定DEFAULT值*/

    REFERENCES publishers(pub_id),  /*创建系统命名外键约束*/

    /* By default, the Parent Company Publisher is the company

    to whom each employee reports. */

hire_date        datetime       NOT NULL

    DEFAULT (getdate()),        /*设定DEFAULT值*/

    /* By default, the current system date will be entered. */

CONSTRAINT FK_employee_job FOREIGN KEY (job_id)

    REFERENCES jobs(job_id)        /*创建自命名外键约束*/

)

GO

   

/* --------------------- 创建employee表上的index --------------------- */

IF EXISTS (SELECT 1 FROM sysindexes

               WHERE name = 'emp_pub_id_ind')

DROP INDEX employee. emp_pub_id_ind

GO

 

CREATE INDEX emp_pub_id_ind

    ON employee(pub_id)

GO

 

    <2>、ORACLE端语法说明

在ORACLE端的语法如下:

/* ---------------------- 创建employee 表---------------------- */

DROP TABLE employee;

 

CREATE TABLE employee

(

emp_id    varchar2(9)  /*根据用户自定义数据类型的定义调整为varchar2(9)*/

/*创建自命名主键约束*/

    CONSTRAINT PK_employee PRIMARY KEY NONCLUSTERED

/*创建自命名CHECK约束*/

    CONSTRAINT CK_emp_id CHECK (emp_id LIKE

                     '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' or

                     emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]'),

    /* CHECK约束说明:Each employee ID consists of three characters that

    represent the employee's initials, followed by a five

    digit number ranging from 10000 to 99999 and then the

    employee's gender (M or F). A (hyphen) - is acceptable

    for the middle initial. */

fname     varchar2(20)     NOT NULL,

minit     varchar2(1)      NULL,

lname     varchar2(30)     NOT NULL,

 

ss_id     varchar2(9)      UNIQUE,    /*创建唯一性约束*/

 

job_id    number(5,0)      NOT NULL

    /*这里考虑了SMALLINT的长度,也可调整为number*/

    DEFAULT 1,            /*设定DEFAULT值*/

job_lvl     number(3,0)

    /*这里考虑了TINYINT的长度,也可调整为number*/

   DEFAULT 10,            /*设定DEFAULT值*/

    /* Entry job_lvl for new hires. */

pub_id  varchar2(4)        NOT NULL

    DEFAULT ('9952')        /*设定DEFAULT值*/

    REFERENCES publishers(pub_id),  /*创建系统命名外键约束*/

    /* By default, the Parent Company Publisher is the company

    to whom each employee reports. */

hire_date        date            NOT NULL

    DEFAULT SYSDATE,        /*设定DEFAULT值*/

    /*这里,SQL SERVER的getdate()调整为ORACLE的SYSDATE*/

    /* By default, the current system date will be entered. */

CONSTRAINT FK_employee_job FOREIGN KEY (job_id)

    REFERENCES jobs(job_id)        /*创建自命名外键约束*/

);

   

/* -------------------- 创建employee表上的index -------------------- */

DROP INDEX employee. emp_pub_id_ind;

CREATE INDEX emp_pub_id_ind ON employee(pub_id);

 

<3>、从SQL SERVER向ORACLE的迁移方案

比较这两段SQL代码,可以看出,在创建表及其主键、外键、CHECK、UNIQUE、DEFAULT、INDEX时,SQL SERVER 与ORACLE的语法大致相同,但时迁移时要注意以下情况:

(1) Oracle定义表字段的default属性要紧跟字段类型之后,如下:

Create table MZ_Ghxx

( ghlxh  number primay key ,

rq     date   default sysdate not null,

  ….

而不能写成

Create table MZ_Ghxx

( ghlxh  number primay key ,

rq     date   not null default sysdate,

  ….

2)T-SQL定义表结构时,如果涉及到用默认时间和默认修改人员,全部修改如下:

 ZHXGRQ     DATE   DEFAULT SYSDATE NULL,

 ZHXGR      CHAR(8) DEFAULT ‘FUTIAN’ NULL,

3)如表有identity定段,要先将其记录下来,建完表之后,马上建相应的序列和表触发器,并作为记录。

posted on 2007-11-07 10:30 datasky 阅读(269) 评论(0)  编辑 收藏 网摘 所属分类: Sql ServerOracle

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接:


 

欢迎加入Asp.net高手MSN群

  • asp.net.group#hotmail.com
  • asp.net_group#hotmail.com

与我联系

搜索

 

常用链接

留言簿

我管理的小组

我的标签

随笔分类(71)

相册

blogs链接

积分与排名

  • 积分 - 35317
  • 排名 - 1299

最新评论