oracle建立 索引index,添加主键/外键

CREATE UNIQUE INDEX  索引名 on 表名(想要创建索引的列名) TABLESPACE 表空间名;

创建表举例

 -- 1.建表
	CREATE TABLE BM_SAMEROUTEANALYSETASK(
	ID NUMBER(9), 
	NAME VARCHAR2(255), 
	CODE VARCHAR2(255), 
	MEMO VARCHAR2(255), 
	VERSION NUMBER(9), 
	CREATEDATE DATE, 
	CREATOR VARCHAR2(100), 
	UPDATEDATE DATE, 
	UPDATER VARCHAR2(100), 
	prefectureName VARCHAR2(255), 
	countyName VARCHAR2(255), 
	alarmNo VARCHAR2(255), 
	alarmName VARCHAR2(255), 
	alarmDate DATE, 
	caller NUMBER(4), 
	taskStatus NUMBER(4), 
	analyseResult NUMBER(4), 
	failedReason VARCHAR2(2555), 
	outAlarmOnuRate NUMBER(7,4) 
	);
	
--2.创建序列
	CREATE SEQUENCE SEQ_BM_SAMEROUTEANALYSETASK;
	
--3.给相应的字段添加备注
	COMMENT ON TABLE BM_SAMEROUTEANALYSETASK IS '江西PON告警设备同路由分析任务表';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.ID IS '标识';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.NAME IS '名称';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.CODE IS '编码';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.MEMO IS '备注';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.VERSION IS '乐观锁';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.CREATEDATE IS '新增时间';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.CREATOR IS '新增人';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.UPDATEDATE IS '结束时间';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.UPDATER IS '修改人';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.prefectureName IS '地市';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.countyName IS '区县';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.alarmNo IS '告警号';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.alarmName IS '告警名称';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.alarmDate IS '告警发生日期';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.caller IS '任务发起方';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.taskStatus IS '当前状态';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.analyseResult IS '分析结果';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.failedReason IS '失败原因描述';
	COMMENT ON COLUMN BM_SAMEROUTEANALYSETASK.outAlarmOnuRate IS '告警以外设备占比';
	
--4.创建索引
--  create index 索引名 on 表名(字段名) tablespace 表空间名
	CREATE INDEX IDX_BMSAMEROUTEANALYSETASKID ON BM_SAMEROUTEANALYSETASK(ID) TABLESPACE IRM_INDEX; 
	
--5.给创建的表指定  外键约束 
--  alter table 表1 add constraint 外键名称 foreign key (表1要设为外键的列名) references 表2(列名);

---6.给创建的表添加  主键约束
--	alter table 表1 add constraint 主键名称  primary key(要设为主键的表1列名); 
    alter table BM_SAMEROUTEANALYSETASK add constraint PK_BM_SAMEROUTEANALYSETASK primary key (ID);
附:

主键名称一般以 "pk_ "开头;
外江名称一般以"fk_"开头

你可能感兴趣的:(oracle)