Oracle primary key with multiple column - oracle

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.

Related

drop foreign key constraint on DB2 table through alter table

I have a DB2 (for IBMi) table created as below. I would like to drop the forign key constraint while running in an SQLRPGLE program. Is this possible?
create table grid_action_details(id integer not null
generated always as identity
(start with 1 increment by 1)
PRIMARY KEY,grid_details_id integer,foreign key(grid_details_id)
references grid_details(id),action_code_details_id integer,
foreign key(action_code_details_id)
references action_code_details(id),action_code_status varchar(2),
created_date date default
current_date,created_by varchar(30),
last_updated_date date default current_date,updated_by
varchar(30),required_parameter clob);
I tried the below syntax but it just doesn't seem to work for me:
ALTER TABLE table-name
DROP FOREIGN KEY foreign_key_name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_id
ACTION_CODE_DETAILS_ID in IESQAFILE type *N not found.
You drop the FK constraint via name, not via column.
Since you didn't specify one during create, you'll need to look to see what name the system generated.
Always a best practice to name things yourself.
CONSTAINT <name> FOREIGN KEY (<columns>)
create table grid_action_details (
id integer not null generated always as identity (
start with 1 increment by 1
)
,constraint grid_action_details_pk
primary key
,grid_details_id integer
,constraint grid_details_fk
foreign key (grid_details_id)
references grid_details (id)
,action_code_details_id integer
,constraint action_code_details_fk
foreign key (action_code_details_id)
references action_code_details (id)
,action_code_status varchar(2)
,created_date date default current_date
,created_by varchar(30)
,last_updated_date date default current_date
,updated_by varchar(30)
,required_parameter clob
);
Now you can drop by the known name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_fk
EDIT
To find the generated name use:
the ACS Schema component
DSPFD
SQL against one of the catalog views (QSYS2.SYSCST, SYSIBM.SQLFOREIGNKEYS, SYSIBM.REFERENTIAL_CONSTRAINTS )

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.

I'm trying to create foreign key between two tables but got: ORA-02270

Maybe I'm burnout, but I don't understand this. I have two tables in Oracle: TBL_a and TBL_x. I'm trying to create a foreign key between thos two tables as follows and get error
ORA-02270: no matching unique or primary key.
CREATE TABLE tbl_a (
cod_op integer,
cod_dni char(8),
cod_correl integer,
varchar2(50)
);
CREATE TABLE tbl_x (
cod_op integer,
cod_dni char(8),
blabla varchar2(50)
);
CREATE UNIQUE INDEX TBL_A_PK ON TBL_A (COD_OP);
CREATE UNIQUE INDEX TBL_x_PK ON TBL_x (COD_OP);
ALTER TABLE TBL_a ADD CONSTRAINT TBL_a_R01
FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP);
The problem is that you've created unique INDEXES on your tables, but you didn't create a unique or primary key CONSTRAINT. Oracle requires that the constraints exist in order to establish a foreign key relationship.
If you drop your existing indexes and add the appropriate constraints you can establish your foreign key relationship:
DROP INDEX TBL_A_PK;
DROP INDEX TBL_x_PK;
ALTER TABLE TBL_A
ADD CONSTRAINT UQ_A
UNIQUE(COD_OP)
USING INDEX;
ALTER TABLE TBL_X
ADD CONSTRAINT UQ_X
UNIQUE(COD_OP)
USING INDEX;
dbfiddle here
The table that is referred by the foreign key (here, tbl_x) must have a primary key or a unique constraint.
In your use case, as you are declaring a unique index on cod_op, you could simply make cod_op the primary key of tbl_x instead: that would make the error disappear.
Demo on DB Fiddle
In general, it is a good practice to have a primary key on any table. Extending the principe of turning your unique indexes to primary keys, your DDL statements could be simplified as follows:
CREATE TABLE tbl_x (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
blabla VARCHAR2(50)
);
CREATE TABLE tbl_a (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
cod_correl INTEGER,
blabla VARCHAR2(50),
CONSTRAINT TBL_a_R01 FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP)
);
Demo on DB Fiddle

MODIFY or ADD to add NOT NULL constraint to a column? Oracle sql

ORDERS table in the Oracle Database:
ORDERS
ORDER_ID NOT NULL NUMBER(4)
ORDATE_DATE DATE
CUSTOMER_ID NUMBER(3)
ORDER_TOTAL NUMBER(7,2)
The ORDERS table contains data and all orders have been assigned a customer ID. I'm trying to add a NOT NULL constraint to the CUSTOMER_ID column. Would I use MODIFY CONSTRAINT or ADD CONSTRAINT? I was told you have to drop the constraint and ADD the new one, but if there is no existing constraint to Customer ID number, would it be MODIFY?
alter table orders modify customer_id not null;
Just MODIFY the column:
alter table orders modify customer_id not null;
Alternatively, you could add an [overkill] constraint in the form:
alter table orders add constraint nn1 check (customer_id is not null);
Just use the first form.
As a side note, some databases (such as Oracle) consider those two constraint different and somewhat separate: the former is a column constraint, while the latter is a table constraint. Oracle keeps track in case you drop one, while the other is still in effect.

how to modify a constraint in sql plus?

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;

Resources