how to modify a constraint in sql plus? - oracle

I have created a table as follows:
create table emp( emp_id number(5) primary key
, emp_name varchar(20) not null
, dob date );
After the table has been created how would I change the constraint not null to unique or any other constraint in SQL*Plus?

You don't change a constraint from one type to another. You can add a unique constraint to the table
ALTER TABLE emp
ADD ( COSTRAINT uk_emp_name UNIQUE( emp_name ) );
That is independent of whether emp_name is allowed to have NULL values.

Just use ALTER TABLE command. For details look here: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_3001.htm#i2103817

We cant be able to modify the already added constraint. Just we have to drop the constraint and add the new one with the same name, but add it with the necessary changes.
ALTER TABLE table_name drop constraint contraint_name;
alter table tablename add constraint containt_name CHECK (column_name IN (changes in the contraint)) ENABLE;

Related

Oracle primary key with multiple column

I have seen the below create table statement :
create table cruise_orders
(cruise_order_id number,
order_date date,
constraint pk_or_id primary key (cruise_order_id,order_date));
so here the primary_key has two columns. But in case if I try the below statement and try to alter the table cruise_orders and add the column order_date to the primary key pk_or_id it throws an error saying "A table can have only one Primary key".
So how does it works with the first statement ? and why it doesn't work with the second alter statement ?
create table cruise_orders
(cruise_order_id number,
order_date date,
constraint pk_or_id primary key (cruise_order_id));
If you want it so that the cruise_order_id must be unique and, separately, that the order_date must be unique then use two constraints:
CREATE TABLE cruise_orders(
cruise_order_id NUMBER,
order_date DATE,
CONSTRAINT cruise_orders__or_id__pk PRIMARY KEY(cruise_order_id)
);
ALTER TABLE cruise_orders ADD CONSTRAINT cruise_orders__order_date__u UNIQUE(order_date);
If you really do want to have a single constraint with both columns the drop the first constraint and create a second with both columns:
CREATE TABLE cruise_orders(
cruise_order_id NUMBER,
order_date DATE,
CONSTRAINT cruise_orders__or_id__pk PRIMARY KEY(cruise_order_id)
);
ALTER TABLE cruise_orders DROP CONSTRAINT cruise_orders__or_id__pk;
ALTER TABLE cruise_orders ADD CONSTRAINT cruise_orders__coi__od__pk
PRIMARY KEY(cruise_order_id, order_date);
Then you will be able to have duplicate cruise_order_id values provided the order_date is different and vice-versa.

Alter Table , adding a Foreign key constraint at a column ORA-02253

Hello there i am studying for the Oracle Certification of SQL Associate .
And trying to do some Examples .
I have an issue where i cannot find easily a reference on this .
create table employees
(employee_id number NOT NULL,
first_name varchar(20),
last_name varchar(30),
constraint employee_pk primary key (employee_id));
create table employee_notes
(employee_notes_id number,
employee_notes varchar(500),
constraint pk_employee_notes primary key (employee_notes_id));
create sequence employee_notes_seq start with 1 increment by 1
Now i want to add a new column at employee_notes table with a foreign key constraint .
I can't find out in syntax where is the problem .
****alter table employee_notes
add employee_id number
constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);****
i get this error
ORA-02253: constraint specification not allowed her
I also tried to alter the table and add column and then the constraint but cannot
alter table employee_notes
add employee_id number;
--
alter table employee notes
add constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);
ORA-02253: constraint specification not allowed here
I would like to know how i can do this
and why this syntax is wrong :)
You did something wrong because - it works OK:
SQL> CREATE TABLE employees
2 (
3 employee_id NUMBER NOT NULL,
4 first_name VARCHAR (20),
5 last_name VARCHAR (30),
6 CONSTRAINT employee_pk PRIMARY KEY (employee_id)
7 );
Table created.
SQL>
SQL> CREATE TABLE employee_notes
2 (
3 employee_notes_id NUMBER,
4 employee_notes VARCHAR (500),
5 CONSTRAINT pk_employee_notes PRIMARY KEY (employee_notes_id)
6 );
Table created.
SQL> ALTER TABLE employee_notes ADD employee_id NUMBER;
Table altered.
SQL> ALTER TABLE employee_notes ADD CONSTRAINT fk_employee_notes
2 FOREIGN KEY (employee_id)
3 REFERENCES employees (employee_id);
Table altered.
SQL>
When you use ALTER TABLE ... ADD in order to add a column and a constraint in one statement, do the following:
-- notice the () and the comma!
alter table employee_notes
add (
employee_id number
, constraint fk_employee_notes
foreign key (employee_id) references employees (employee_id)
) ;
That should do the trick. See dbfiddle. The syntax is similar to CREATE TABLE, where you'd also write all column names, data types etc in (), separated by commas.

Do I need to drop a foreign key on one table to delete a row on another using oracle?

I have two tables
Parent table
(account_number varchar(15) not null,
branch_name varchar(50) not null,
balance number not null,
primary key(account_number));
Child table
account_number varchar(15) not null,
foreign key(account_number) references parent table(account_number));
I am trying this:
DELETE FROM parent table
WHERE balances > 1000;
I am deleting accounts by balances on the parent but I get an error message about the child relationship.
My assumption is a DELETE CASCADE has to be added to the foreign key in the child table. All the documentation shows how to alter the table when the constraint is named. I do not have that situation. Is there a way to do it, or do I have to specify the cascade in the delete statement I am writing?
Every constraint in Oracle has a name. If a name isn't specified when the constraint is created, Oracle will autogenerate a name for the constraint. If you don't know what the name of a constraint is, try running a SQL statement that violates the constraint and reading the constraint name from the error message:
SQL> delete from parent where account_number = 1234;
delete from parent where account_number = 1234
*
ERROR at line 1:
ORA-02292: integrity constraint (LUKE.SYS_C007357) violated - child record
found
In this case the name of the constraint is SYS_C007357.
If that doesn't work, you can query the data dictionary view user_constraints:
SQL> select constraint_name from user_constraints where table_name = 'CHILD' and constraint_type = 'R';
CONSTRAINT_NAME
------------------------------
SYS_C007357
As far as I can tell, you can't modify a foreign key constraint to enable ON DELETE CASCADE. Instead you must drop the constraint and recreate it.
I don't believe you can apply the CASCADE option to a DELETE statement either, but you can delete the child rows before deleting from the parent:
DELETE FROM child
WHERE account_number IN (SELECT account_number FROM parent WHERE balance > 1000);
DELETE FROM parent
WHERE balance > 1000;
However, I don't know how many other tables you have with foreign key constraints referencing your parent table, nor in how many places you are deleting from the parent table, so I can't say how much work it would be to use this approach.
yes you can set DELETE CASCADE
see more info here FOREIGN KEYS WITH CASCADE DELETE
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
for example
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);

oracle change a column to unique

Table has been created in system this way
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The new crete table statement is as below:
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The differnce is INSTANCEID has UNIQUE in it. How do i Alter the table? I used the below statement and it did not work for me.
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID);
It gave an error:
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID)
Error report:
SQL Error: ORA-02261: such unique or primary key already exists in the table
02261. 00000 - "such unique or primary key already exists in the table"
*Cause: Self-evident.
*Action: Remove the extra key.
Please help me to Alter the table as required above. Thanks!
Here is the output of SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES';
"CONSTRAINT_NAME","COLUMN_NAME","CONSTRAINT_TYPE"
"SYS_C0016531","DM","C"
"SYS_C0016532","INSTANCEID","C"
"SYS_C0016533","INSTANCENAME","C"
"SYS_C0016534","HOSTNAME","C"
"PK_INSTANCES","HOSTNAME","P"
"PK_INSTANCES","INSTANCEID","P"
"SYS_C0016536","INSTANCENAME","U"
You have already stated that INSTANCEID is supposed to be UNIQUE, so a constraint has been created.
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE, -- UNIQUE constraint
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
Edit: Ok, after reading your comment, try this:
SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES'
AND con.constraint_type = 'U'
;
It will list UNIQUE constraints and associated columns for INSTANCE table. Please check if there is a unique constraint on the INSTANCEID column (and if that constraint has no other associated columns).
Example at SQLFiddle: http://sqlfiddle.com/#!4/43b43/6
Edit #2: creating named constraints, all options:
-- CREATE TABLE - "In Line" Constraints
CREATE TABLE ports (
ID NUMBER CONSTRAINT PORT_ID_PK PRIMARY KEY,
NAME VARCHAR2(20)
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_NN NOT NULL
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_UQ UNIQUE
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER CONSTRAINT SHIP_PORT_FK
REFERENCES PORTS (ID)
);
-- CREATE TABLE - "Out of Line" Constraints
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT PORT_ID_PK PRIMARY KEY (ID)
);
-- NOT NULL constraints can not be created "Out of Line"!
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT NAME_UQ UNIQUE (NAME)
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER,
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER,
CONSTRAINT SHIP_PORT_FK FOREIGN KEY
(HOME_PORT_ID) REFERENCES PORTS (ID)
);
-- ALTER TABLE - "In Line" Constraints
ALTER TABLE PORTS MODIFY ID
CONSTRAINT PORT_ID_PK PRIMARY KEY;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_NN NOT NULL;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_UQ UNIQUE;
ALTER TABLE SHIPS MODIFY HOME_PORT_ID
CONSTRAINT SHIP_PORT_FK REFERENCES PORTS (ID);
-- ALTER TABLE - "Out of Line" Constraints
ALTER TABLE PORTS ADD CONSTRAINT
PORT_ID_PK PRIMARY KEY (ID);
-- NOT NULL constraints can not be created "Out of Line"!
ALTER TABLE PORTS ADD CONSTRAINT
NAME_UQ UNIQUE (NAME);
ALTER TABLE PORTS ADD
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5));
ALTER TABLE SHIPS ADD CONSTRAINT SHIP_PORT_FK
FOREIGN KEY (HOME_PORT_ID)
REFERENCES PORTS (ID);
NOT NULL constraints cannot be create of out line.

Trigger to enter deleted entries to a new table - SQL plus

I am trying to create a trigger which will enter values into a table terminated_employees when we delete values from the nm_employees table. I have written the trigger but it does not work. Is my trigger format right? Any ideas?
CREATE TABLE nm_departments(
dept2 varchar(20),
CONSTRAINT empPK PRIMARY KEY (dept2)
);
CREATE TABLE nm_employees(
name varchar(20),
dept varchar(20),
CONSTRAINT departments FOREIGN KEY (dept) REFERENCES nm_departments (dept2)ON DELETE CASCADE
);
CREATE TABLE terminated_employees(
te_name varchar(20),
te_dept varchar(20)
);
CREATE TRIGGER term_employee AFTER DELETE ON nm_employee
FOR EACH ROW
BEGIN
INSERT INTO terminated_employees (NEW.te_name, NEW.te_dept) VALUES (OLD.name,OLD.dept)
END;
You should not be specifying the NEW. on the column names of your INSERT statement. These are the columns in the terminated_employees table, NOT the new values. i.e.
INSERT INTO terminated_employees (te_name, te_dept)
VALUES (OLD.name,OLD.dept)
You can use show errors (or show err) in SQL*Plus to see the exact error.
You have a number of problems:
Wrong table name on create trigger (missing the s)
Missing ; after instert statement
The OLD. need to have : prefix. i.e. :OLD.name

Resources