您的位置:首页 > 数据库 > Oracle

从SQL Server向Oracle迁移的技术实现方案(三)表(主键、外键、CHECK、UNIQUE、DEFAULT、INDEX)

2007-11-07 10:30 501 查看

<三> 表(主键、外键、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定段,要先将其记录下来,建完表之后,马上建相应的序列和表触发器,并作为记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: