How to add primary key constraint on oracle? - oracle

CREATE TABLE buses(Bus_no NUMBER(11) NOT NULL ,Bus_name Varchar2(20),Type VARCHAR2(20),Total_seats Number(11),Avail_seats Number(11));
ALTER TABLE buses
ADD CONSTRAINT PK_BUSES UNIQUE(Bus_no);
This is the table I need to create .. and this should be the output:
Name Null? Type
----------------------------------------- -------- ----------------------------
BUS_NO NOT NULL NUMBER(11)
BUS_NAME VARCHAR2(20)
TYPE VARCHAR2(20)
TOTAL_SEATS NUMBER(11)
AVAIL_SEATS NUMBER(11)
CONSTRAINT_NAME
------------------------------
PK_BUSES
However I am getting this extra line SYS-C00403053 along with my output table,please help to remove this.
CONSTRAINT_NAME
------------------------------
SYS_C00403053
PK_BUSES

You haven't added a primary key, you've added a unique constraint. While a unique constraint and a not-null constraint are effectively the same, they are not actually the same as an actual primary key.
As #GurwinderSingh said, the SYS_C00403053 is a system-generated name for the not-null constraint. It is possible, but unusual, to name a not-null constraint:
-- just to clean up what you have in the question, remove the unique constraint
ALTER TABLE buses DROP CONSTRAINT PK_BUSES;
ALTER TABLE buses MODIFY Bus_no NULL;
ALTER TABLE buses MODIFY Bus_no CONSTRAINT BUS_NO_NOT_NULL NOT NULL;
desc buses
Name Null? Type
----------- -------- ------------
BUS_NO NOT NULL NUMBER(11)
BUS_NAME VARCHAR2(20)
TYPE VARCHAR2(20)
TOTAL_SEATS NUMBER(11)
AVAIL_SEATS NUMBER(11)
select constraint_name, constraint_type, search_condition
from user_constraints where table_name = 'BUSES';
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------------ - --------------------------------------------------------------------------------
BUS_NO_NOT_NULL C "BUS_NO" IS NOT NULL
But as you want a primary key anyway, you can drop the separate not-null check, as it's implied by a (proper) primary key:
ALTER TABLE buses MODIFY Bus_no NULL;
ALTER TABLE buses ADD CONSTRAINT PK_BUSES PRIMARY KEY (Bus_no);
desc buses
Name Null? Type
----------- -------- ------------
BUS_NO NOT NULL NUMBER(11)
BUS_NAME VARCHAR2(20)
TYPE VARCHAR2(20)
TOTAL_SEATS NUMBER(11)
AVAIL_SEATS NUMBER(11)
select constraint_name, constraint_type, search_condition
from user_constraints where table_name = 'BUSES';
CONSTRAINT_NAME C SEARCH_CONDITION
------------------------------ - --------------------------------------------------------------------------------
PK_BUSES P
You now only see the primary key constraint listed, but the column is still marked as not nullable, and you get the same error if you try to insert null:
insert into buses (bus_no) values (null);
ORA-01400: cannot insert NULL into ("MY_SCHEMA"."BUSES"."BUS_NO")

SYS_C00403053 is the system generated name given to the NOT NULL constraint on Bus_no column. Your result is as expected only.

Related

I cannot display the values of the 2 table Ceremony and Referee

create table STADIUM
( stad_location varchar2(20) primary key
, stad_name varchar2(10)
, stad_capacity number(5)
, match_ID char(8)
, stall_ID char(4)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
create table TRANSPORTATION
(registra_no char(6) primary key
, transp_type varchar2(10)
, capacity number(2)
);
create table CEREMONY
(cerem_type varchar2(10) primary key
, cerem_name varchar2(15)
, FIFA_theme_song varchar2(20)
, p_ID char(8)
, stad_location varchar2(20)
, foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
, foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
);
create table REFEREE( ref_ID char(8) primary key
, ref_name varchar2(20)
, yo_exp number(2)
, match_ID char(8)
, registra_no char(6)
, foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
, foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729'); select * from CEREMONY;
select * from REFEREE;
Bunch of tables are missing, referenced by foreign keys so I'm creating them first:
SQL> -- Missing tables, referenced by foreign keys
SQL> create table match (match_id char(8) primary key);
Table created.
SQL> create table stall (stall_id char(4) primary key);
Table created.
SQL> create table performer (p_id char(8) primary key);
Table created.
SQL>
Your tables:
SQL> create table STADIUM
2 ( stad_location varchar2(20) primary key
3 , stad_name varchar2(10)
4 , stad_capacity number(5)
5 , match_ID char(8)
6 , stall_ID char(4)
7 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
8 , foreign key (stall_ID) references STALL (stall_ID) ON DELETE SET NULL);
Table created.
SQL> create table TRANSPORTATION
2 (registra_no char(6) primary key
3 , transp_type varchar2(10)
4 , capacity number(2)
5 );
Table created.
SQL> create table CEREMONY
2 (cerem_type varchar2(10) primary key
3 , cerem_name varchar2(15)
4 , FIFA_theme_song varchar2(20)
5 , p_ID char(8)
6 , stad_location varchar2(20)
7 , foreign key (p_ID) references PERFORMER(p_ID) ON DELETE SET NULL
8 , foreign key (stad_location) references STADIUM(stad_location)ON DELETE SET NULL
9 );
Table created.
SQL> create table REFEREE( ref_ID char(8) primary key
2 , ref_name varchar2(20)
3 , yo_exp number(2)
4 , match_ID char(8)
5 , registra_no char(6)
6 , foreign key (match_ID) references MATCH (match_ID) ON DELETE SET NULL
7 , foreign Key (registra_no) references TRANSPORTATION(registra_no) ON DELETE SET NULL);
Table created.
SQL>
Inserts into tables referenced by ceremony and referee:
SQL> insert into performer values ('PP561475');
1 row created.
SQL> insert into stadium (stad_location) values ('Al-Waab Street');
1 row created.
SQL> insert into match values ('MM129456');
1 row created.
SQL> insert into transportation (registra_no) values ('QLM729');
1 row created.
SQL>
Your inserts & result of your select statements:
SQL> insert into CEREMONY values('Opening', 'Speech', 'Colors', 'PP561475', 'Al-Waab Street');
1 row created.
SQL> insert into REFEREE values('RF503624','Mike Dean', 25, 'MM129456', 'QLM729');
1 row created.
SQL> select * from CEREMONY;
CEREM_TYPE CEREM_NAME FIFA_THEME_SONG P_ID STAD_LOCATION
---------- --------------- -------------------- -------- --------------------
Opening Speech Colors PP561475 Al-Waab Street
SQL> select * from REFEREE;
REF_ID REF_NAME YO_EXP MATCH_ID REGIST
-------- -------------------- ---------- -------- ------
RF503624 Mike Dean 25 MM129456 QLM729
SQL>
Therefore, if you do everything right, everything is right.

How to delete the primary key from without using constraint name

CREATE TABLE Persons (
ID int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
How to remove the primary key as there is no constraint defined?
"How to remove PK as there is no constraint defined?"
Actually it's every bit as simple as you might hope:
SQL> create table t23 (id number primary key);
Table created.
SQL> select constraint_name, constraint_type
2 from user_constraints
3 where table_name = 'T23'
4 /
CONSTRAINT_NAME C
------------------------------ -
SYS_C0034419 P
SQL> alter table t23 drop primary key;
Table altered.
SQL> select constraint_name, constraint_type
2 from user_constraints
3 where table_name = 'T23'
4 /
no rows selected
SQL>
run this to get constraint name :
SELECT *
FROM user_cons_columns
WHERE table_name = Persons;
then run
ALTER TABLE Persons
DROP CONSTRAINT <pk constraint>;
Don't think you can do it in 1 SQL command,without knowing the constraint name, but you can know the constraint name as in this case it would be defined by system. Something which starts with SYS.....
Alternatively you can use a PLSQL block to achieve the same.
See the example below for your case.
CREATE TABLE Persons (
ID int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Find constraint name
select CONSTRAINT_NAME
From USER_CONSTRAINTS
where table_name='PERSONS'
AND CONSTRAINT_TYPE='P';
OUTPUT:= SYS_C0012152
Drop Constraint
ALTER TABLE PERSONS
DROP CONSTRAINT SYS_C0012152;
Note: The constraint name SYS_C0012152 is not enclosed in single quotes.
PLSQL Block to do the same
declare
sql_stmt varchar2(255);
cons_name varchar2(30);
begin
select CONSTRAINT_NAME
into cons_name
From USER_CONSTRAINTS
where table_name='PERSONS'
AND CONSTRAINT_TYPE='P';
sql_stmt:=' ALTER TABLE PERSONS
DROP CONSTRAINT '||cons_name;
dbms_output.put_line(sql_stmt);
execute immediate sql_stmt;
end;

where oracle foreign key constraint named SYS_C comming from

my database is oracle 10.2 and my create table sql is like this:
create table T_EP_SYS_COMPANY
(
company_id NUMBER not null,
company_name VARCHAR2(30),
company_address VARCHAR2(100),
is_in_use VARCHAR2(1),
is_canceled VARCHAR2(1),
is_headquarter VARCHAR2(1),
account_id NUMBER not null
)
tablespace USERS
pctfree 10
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
alter table T_EP_SYS_COMPANY
add constraint PK_ESHOP_SYS_COMPANY primary key (COMPANY_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
alter table T_EP_SYS_COMPANY
add constraint FK_SYS_COMPANY_PAY_ACCOUNT foreign key (ACCOUNT_ID)
references T_EP_PAY_ACCOUNT (ACCOUNT_ID);
but in my database,I have to 2 FKs on column "account_id":
owner constraint_name table_name column_name position
ESHOPV2 SYS_C009725 T_EP_SYS_COMPANY ACCOUNT_ID null
ESHOPV2 FK_SYS_COMPANY_PAY_ACCOUNT T_EP_SYS_COMPANY ACCOUNT_ID 1
why there are 2 FKs? I did rename the table name,dose the rename ddl has something to do with this?
They aren't both foreign key constraints. The SYS_C is a system-generated name for a constraint you didn't explicitly name; in this case for your not-null check. You can see those immediately after the create:
create table T_EP_SYS_COMPANY
(
company_id NUMBER not null,
company_name VARCHAR2(30),
company_address VARCHAR2(100),
is_in_use VARCHAR2(1),
is_canceled VARCHAR2(1),
is_headquarter VARCHAR2(1),
account_id NUMBER not null
);
select uc.constraint_name, uc.constraint_type, ucc.column_name, ucc.position
from user_constraints uc
join user_cons_columns ucc on ucc.constraint_name = uc.constraint_name
where uc.table_name = 'T_EP_SYS_COMPANY';
CONSTRAINT_NAME CONSTRAINT_TYPE COLUMN_NAME POSITION
------------------------------ --------------- --------------- --------
SYS_C0093988 C COMPANY_ID
SYS_C0093989 C ACCOUNT_ID
The constraint type is C, showing it's a check constraint. You can name those by explicitly adding check constraints rather than specifying them as 'not null' but there is no real benefit - you don't ever need to refer to them by name, e.g. to temporarily disable them.
After you add the primary and foreign key you see those too:
alter table T_EP_SYS_COMPANY
add constraint PK_ESHOP_SYS_COMPANY primary key (COMPANY_ID);
alter table T_EP_SYS_COMPANY
add constraint FK_SYS_COMPANY_PAY_ACCOUNT foreign key (ACCOUNT_ID)
references T_EP_PAY_ACCOUNT (ACCOUNT_ID);
select uc.constraint_name, uc.constraint_type, ucc.column_name, ucc.position
from user_constraints uc
join user_cons_columns ucc on ucc.constraint_name = uc.constraint_name
where uc.table_name = 'T_EP_SYS_COMPANY';
CONSTRAINT_NAME CONSTRAINT_TYPE COLUMN_NAME POSITION
------------------------------ --------------- --------------- --------
SYS_C0093988 C COMPANY_ID
SYS_C0093989 C ACCOUNT_ID
PK_ESHOP_SYS_COMPANY P COMPANY_ID 1
FK_SYS_COMPANY_PAY_ACCOUNT R ACCOUNT_ID 1
These have constraint type P and R, for 'primary key' and 'referential integrity'.
Read more about types of integrity constraints and how they are shown in the data dictionary.
owner constraint_name table_name column_name position
ESHOPV2 SYS_C009725 T_EP_SYS_COMPANY ACCOUNT_ID null
ESHOPV2 FK_SYS_COMPANY_PAY_ACCOUNT T_EP_SYS_COMPANY ACCOUNT_ID 1
why there are 2 FKs?
SYS_C009725 is not a foreign key constraint. It is a CHECK constraint for the NOT NULL condition. If you see the CONSTRAINT_TYPE then you would see it as C. And the name as SYS_C you see is because it is system-generated name.
Too keep it simple, let's see a small example:
SQL> CREATE TABLE t(
2 ID NUMBER NOT NULL
3 );
Table created.
SQL>
SQL> SELECT constraint_name,
2 constraint_type,
3 table_name,
4 search_condition
5 FROM user_constraints
6 WHERE table_name ='T';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME SEARCH_CONDITION
--------------- --------------- ---------- ----------------
SYS_C0010726 C T "ID" IS NOT NULL
SQL>

parent keys not found Oracle 10g

I have two tables :
SQL> desc SEGMENT
Name Null? Type
----------------------------------------- -------- ----------------------------
INDIP NOT NULL VARCHAR2(11)
NOMSEGMENT NOT NULL VARCHAR2(20)
ETAGE
SQL> desc POSTE
Name Null? Type
----------------------------------------- -------- ----------------------------
NPOSTE NOT NULL VARCHAR2(7)
NOMPOSTE NOT NULL VARCHAR2(20)
INDIP VARCHAR2(11)
AD VARCHAR2(3)
TYPEPOSTE VARCHAR2(9)
NSALLE VARCHAR2(7)
I want to add a constraint as the following :
ALTER TABLE "POSTE" ADD CONSTRAINT "FK_POSTE_SEGMENT" FOREIGN KEY ("INDIP") REFERENCES "SEGMENT" ("INDIP") ENABLE;
But I got this error message :
ERROR at line 1: ORA-02298: cannot validate (AIMAD.FK_POSTE_SEGMENT) -
parent keys not found
How can I solve this
You should check what POSTE table does not contain values in INDIP column what don't exist in INDIP column of SEGMENT table.
Like
SQL> create table t (x int primary key);
SQL> insert into t values(1);
SQL> create table t_c (x int);
SQL> insert into t_c values(1);
SQL> insert into t_c values(2);
SQL> commit;
SQL> alter table t_c add constraint t_c_x foreign key(x)
2 references t(x);
alter table t_c add constraint t_c_x foreign key(x)
*
ORA-02298: cannot validate (SCOTT.T_C_X) - parent key not found
SQL> select * from t_c where not exists (select *
2 from t where t.x = t_c.x);
X
-----------------------
2
Also Oracle provides the ability to create constraint in NOVALIDATE status, this prevents Oracle from checking data during contraint creation:
SQL> alter table t_c add constraint t_c_x foreign key(x)
2 references t(x) enable novalidate;
Table altered.
but this can have undesirable side effects because data in both tables are not consistent.

Oracle auto generated check constraint

When I create table with columns not null, Oracle automatically creating check constraints to be not null: like this ( query from user_constraints view)
NAME TYPE SEARCH_CONDITION
------------------------------ ---- ---------------------------------------
SYS_C0036357 C "SUPPLIER_ID" IS NOT NULL
SYS_C0036358 C "SUPPLIER_NAME" IS NOT NULL
So, is there any way to know that constraint created by Oracle(Auto) or it had been created by user.
thank you.
You can look at the GENERATED field in the user_constraints table (or all_constraints).
create table t (a number not null, constraint t_pk primary key(a));
select table_name, constraint_name, generated
from user_constraints
where table_name = 'T';
Gives:
T | CONSTRAINT. | GENERATED
----------------------------
T | SYS_C008425 | GENERATED NAME
T | T_PK | USER NAME

Resources