Let's do it :)
--- 1. METHOD ) Name implicit - Creation implicit
CREATE TABLE customer
(
id NUMBER PRIMARY KEY,
first_name VARCHAR2(40),
last_name VARCHAR2(40)
);
SELECT cons.constraint_name
FROM all_constraints cons,
all_cons_columns cc
WHERE cons.constraint_name = cc.constraint_name
AND cons.owner = cc.owner
AND cons.table_name = 'CUSTOMER'
AND cc.column_name = 'ID';
--- SYS_C007758
INSERT INTO customer
(id)
VALUES (1);
INSERT INTO customer
(id)
VALUES (1);
--- ORA-00001: unique constraint (HR.SYS_C007758) violated
-------------------------------
DROP TABLE customer;
--- 2. METHOD ) Name explicit - Creation implicit
CREATE TABLE customer
(
id NUMBER,
first_name VARCHAR2(40),
last_name VARCHAR2(40),
CONSTRAINT pk_customer PRIMARY KEY(id)
);
SELECT cons.constraint_name
FROM all_constraints cons,
all_cons_columns cc
WHERE cons.constraint_name = cc.constraint_name
AND cons.owner = cc.owner
AND cons.table_name = 'CUSTOMER'
AND cc.column_name = 'ID';
--- PK_CUSTOMER
INSERT INTO customer
(id)
VALUES (1);
INSERT INTO customer
(id)
VALUES (1);
--- ORA-00001: unique constraint (HR.PK_CUSTOMER) violated
-------------------------------
DROP TABLE customer;
--- 3. METHOD ) Name implicit - Creation explicit
CREATE TABLE customer
(
id NUMBER,
first_name VARCHAR2(40),
last_name VARCHAR2(40)
);
ALTER TABLE customer
MODIFY id PRIMARY KEY;
SELECT cons.constraint_name
FROM all_constraints cons,
all_cons_columns cc
WHERE cons.constraint_name = cc.constraint_name
AND cons.owner = cc.owner
AND cons.table_name = 'CUSTOMER'
AND cc.column_name = 'ID';
--- SYS_C007753
-------------------------------
DROP TABLE customer;
--- 4. METHOD ) Name explicit - Creation explicit
CREATE TABLE customer
(
id NUMBER,
first_name VARCHAR2(40),
last_name VARCHAR2(40)
);
ALTER TABLE customer
ADD CONSTRAINT pk_customer PRIMARY KEY(id);
SELECT cons.constraint_name
FROM all_constraints cons,
all_cons_columns cc
WHERE cons.constraint_name = cc.constraint_name
AND cons.owner = cc.owner
AND cons.table_name = 'CUSTOMER'
AND cc.column_name = 'ID';
--- PK_CUSTOMER
No comments:
Post a Comment