Not able to update the constraints of my table column - sql

I am writing below command to make my column pname of table programs a foreign key, but the it gives me an error saying
ORA-00905: missing keyword
SQL> alter table programs alter column pname varchar2(20) foreign key references programmer(pname);

Test case:
SQL> create table programmer (pname varchar2(20) primary key);
Table created.
SQL> create table programs (pname varchar2(10));
Table created.
Altering the table:
SQL> alter table programs modify pname varchar2(20);
Table altered.
SQL> alter table programs add constraint fk_prog_ammer
2 foreign key (pname) references programmer (pname);
Table altered.
SQL>

You want to use ADD CONSTRAINT:
ALTER TABLE programs -- table name
ADD CONSTRAINT programs__pname__fk -- constraint name
FOREIGN KEY (pname) -- column to apply the constraint to
REFERENCES programmer(pname) -- table and column to reference
db<>fiddle here

Related

Even with complete and readable statements, an error occurs: SQL Error [900] [42000]: ORA-00900: invalid SQL statement

I have two tables created in my Oracle database: the table 'responsible' (which has the id and description of the responsible) and the table 'ident' (which has id's of three types of expenses).
Below, I am trying to create a script in which:
Add values to the columns of the 'responsible';
create a 'type_responsible' table, and insert values into it;
create a relationship table called 'responsible_by_type', which, using the data from the table 'responsible', 'ident' and 'responsible_type' establishes one relationship between them.
These changes in data type to primary key or foreign key are attempts to get the bank to recognize the fields as relatable.
I get the error: 'SQL Error [900] [42000]: ORA-00900: invalid SQL statement'. Even if I execute one instruction at a time, which makes no sense.
I really don't know where my mistake is. Since I can't run the script, I can't know if the script allows me to reach my goal. Could someone help me?
<--Table RESPONSIBLE-->
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (1, 'payer1');
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (2, 'payer2');
ALTER TABLE RESPONSIBLE ADD CONSTRAINT PK_RESPONSIBLE_ID PRIMARY KEY (RESPONSIBLE_ID);
ALTER TABLE RESPONSIBLE ADD CONSTRAINT UNIQUE_RESPONSIBLE_DESC UNIQUE (RESPONSIBLE_DESC);
<--Table IDENT-->
<--OPTION ONE-->
ALTER TABLE IDENT ADD CONSTRAINT pk_RESPONSIBLES_TYPE_IDS primary key(RESPONSIBLE_DEBTS_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_EXPENSES_ID);
<--OPTION TWO-->
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_DEBTS_ID FOREIGN KEY (RESPONSIBLE_DEBTS_ID);
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_ASSETS_ID FOREIGN KEY (RESPONSIBLE_ASSETS_ID);
ALTER TABLE IDENT ADD CONSTRAINT FK_RESPONSIBLE_EXPENSES_ID FOREIGN KEY (RESPONSIBLE_EXPENSES_ID);
<--Table RESPONSIBLE_TYPE-->
CREATE TABLE RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID NUMBER(10), RESPONSIBLE_TYPE_DESC VARCHAR(100));
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (1, 'Assets');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (2, 'Expenses');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (3, 'Debts');
ALTER TABLE RESPONSIBLE_TYPE ADD CONSTRAINT PK_RESPONSIBLE_TYPE_ID PRIMARY KEY (RESPONSIBLE_TYPE_ID);
<--Table RESPONSIBLE_BY_TYPE-->
CREATE TABLE RESPONSIBLE_BY_TYPE(RESPONSIBLE_ID NUMBER(10) NOT NULL, RESPONSIBLE_EXPENSES_ID NUMBER(10) NOT NULL,
RESPONSIBLE_ASSETS_ID NUMBER(10) NOT NULL, RESPONSIBLE_DEBTS_ID NUMBER(10) NOT NULL, RESPONSIBLE_DESC VARCHAR(100), RESPONSIBLE_TYPE VARCHAR(100),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_ID) REFERENCES RESPONSIBLE(RESPONSIBLE_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_EXPENSES_ID) REFERENCES IDENT(RESPONSIBLE_EXPENSES_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_ASSETS_ID) REFERENCES IDENT(RESPONSIBLE_ASSETS_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_DEBTS_ID) REFERENCES IDENT(RESPONSIBLE_DEBTS_ID),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_TYPE) REFERENCES RESPONSIBLE_TYPE(RESPONSIBLE_TYPE),
CONSTRAINT FK_RESPONSIBLE_BY_TYPE FOREIGN KEY(RESPONSIBLE_DESC) REFERENCES RESPONSIBLE(RESPONSIBLE_DESC);
In the first steps it was running. From the moment I created the table 'responsible_by_type' and started changing the data types of the other tables, I couldn't do anything anymore.
Now, even when I execute a simple insert instruction, I get the error I mentioned above.
This works from Oracle SQL Developer
-- ******************* Existing Table RESPONSIBLE - you don't need to create it ***************************
--CREATE TABLE RESPONSIBLE
-- (
-- RESPONSIBLE_ID NUMBER(10),
-- RESPONSIBLE_DESC VARCHAR2(100)
-- );
-- -------------- Alter table --------------------
ALTER TABLE RESPONSIBLE ADD CONSTRAINT pk_responsible_id PRIMARY KEY (RESPONSIBLE_ID);
ALTER TABLE RESPONSIBLE ADD CONSTRAINT unique_responsible_desc UNIQUE (RESPONSIBLE_DESC);
-- ---------- I n s e r t s ----------------
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (1, 'payer1');
INSERT INTO RESPONSIBLE(RESPONSIBLE_ID, RESPONSIBLE_DESC) VALUES (2, 'payer2');
-- ******************* Existing Table IDENT - you don't need to create it ***************************
-- CREATE TABLE IDENT
-- (
-- RESPONSIBLE_DEBTS_ID NUMBER,
-- RESPONSIBLE_ASSETS_ID NUMBER,
-- RESPONSIBLE_EXPENSES_ID NUMBER
-- );
-- -------------- Alter table --------------------
ALTER TABLE IDENT ADD CONSTRAINT pk_responsibles_type_ids
PRIMARY KEY(RESPONSIBLE_DEBTS_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_EXPENSES_ID);
-- *************** New Table RESPONSIBLE_TYPE ***************************
CREATE TABLE RESPONSIBLE_TYPE
(
RESPONSIBLE_TYPE_ID NUMBER(10),
RESPONSIBLE_TYPE_DESC VARCHAR(100),
CONSTRAINT pk_responsible_type_id PRIMARY KEY (RESPONSIBLE_TYPE_ID)
-- option 2 -- PK(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC)
);
-- ---------- I n s e r t s ----------------
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (1, 'Assets');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (2, 'Expenses');
INSERT INTO RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) VALUES (3, 'Debts');
-- *************** New Table RESPONSIBLE_BY_TYPE ***************************
CREATE TABLE RESPONSIBLE_BY_TYPE
(
RESPONSIBLE_ID NUMBER(10) NOT NULL,
RESPONSIBLE_EXPENSES_ID NUMBER(10) NOT NULL,
RESPONSIBLE_ASSETS_ID NUMBER(10) NOT NULL,
RESPONSIBLE_DEBTS_ID NUMBER(10) NOT NULL,
RESPONSIBLE_DESC VARCHAR(100),
RESPONSIBLE_TYPE_ID NUMBER(10),
-- NOTE *** FK Constraints References PKs in referenced tables *** ----------
CONSTRAINT fk_resp_by_type_resp
FOREIGN KEY(RESPONSIBLE_ID) REFERENCES RESPONSIBLE(RESPONSIBLE_ID),
CONSTRAINT fk_resp_by_type_idnt
FOREIGN KEY(RESPONSIBLE_EXPENSES_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_DEBTS_ID)
REFERENCES IDENT(RESPONSIBLE_EXPENSES_ID, RESPONSIBLE_ASSETS_ID, RESPONSIBLE_DEBTS_ID),
CONSTRAINT fk_resp_by_type_resptype
FOREIGN KEY(RESPONSIBLE_TYPE_ID) REFERENCES RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID)
-- option 2 -- FK(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC) references RESPONSIBLE_TYPE(RESPONSIBLE_TYPE_ID, RESPONSIBLE_TYPE_DESC)
);
-- R e s u l t
table RESPONSIBLE created.
table RESPONSIBLE altered.
table RESPONSIBLE altered.
1 rows inserted.
1 rows inserted.
table IDENT created.
table IDENT altered.
table RESPONSIBLE_TYPE created.
1 rows inserted.
1 rows inserted.
1 rows inserted.
table RESPONSIBLE_BY_TYPE created

Add multiple foreign keys to existing table in Oracle

I want to add multiple foreign keys to existing table in Oracle database. Following sql query gives me an error. One by one I can add foreign key constraint. But I want to do this within one statement like below.
ALTER TABLE address
ADD CONSTRAINT fk_customer_id FOREIGN KEY (customer_id)
REFERENCES customer (id) ON DELETE CASCADE ,
ADD CONSTRAINT fk_city_id FOREIGN KEY (city_id)
REFERENCES city (id) ON DELETE CASCADE;
Any idea how to do this?
That's not entirely true (what #Thorsten has said). You can add two constraints at a time.
SQL> create table test (empno number, deptno number);
Table created.
SQL>
SQL> alter table test add
2 ( constraint fk_test_emp foreign key (empno) references emp (empno),
3 constraint fk_test_dept foreign key (deptno) references dept (deptno)
4 );
Table altered.
SQL>

Convert foreign key constraint from SQL Server to Oracle

I want to convert the following script(SQL SERVER) to Oracle:
ALTER TABLE [dbo].[TDistribucion] WITH CHECK ADD CONSTRAINT [FK_TDistribucion_TDocumentos] FOREIGN KEY([Id_Documento])
REFERENCES [dbo].[TDocumentos] ([Id])
GO
ALTER TABLE [dbo].[TDistribucion] CHECK CONSTRAINT [FK_TDistribucion_TDocumentos]
GO
I have tried to run the same script in Oracle:
ALTER TABLE TDISTRIBUCION WITH CHECK ADD CONSTRAINT FK_TDistribucion_TDocumentos FOREIGN KEY(ID_DOCUMENTO)
REFERENCES TDOCUMENTOS (ID);
ALTER TABLE TDISTRIBUCION CHECK CONSTRAINT FK_TDistribucion_TDocumentos;
and I got this error:
Error starting at line : 3 in command -
ALTER TABLE TDISTRIBUCION WITH CHECK ADD CONSTRAINT FK_TDistribucion_TDocumentos FOREIGN KEY(ID_DOCUMENTO)
REFERENCES TDOCUMENTOS (ID)
Error report -
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
*Cause:
*Action:
I believe that the "WITH CHECK ADD" is the problem!
See if this helps.
A master table - its primary key column will be referenced from the tdistribucion table.
SQL> create table tdocumentos (id number primary key);
Table created.
A detail - tdistribucion - table (with only one column, just to demonstrate the issue):
SQL> create table tdistribucion (id number);
Table created.
A foreign key constraint, using proper syntax. It will succeed if no records violate the constraint and fail if opposite. Oracle does it at once, you don't have to run another "check". As both tables are empty, it'll succeed:
SQL> alter table tdistribucion add constraint
2 fk_dis_doc foreign key (id)
3 references tdocumentos (id);
Table altered.
However, if there were some rows that violate the constraint, you wouldn't be able to create it:
SQL> alter table tdistribucion drop constraint fk_dis_doc;
Table altered.
SQL> insert all
2 into tdocumentos (id) values (1) --> 1 can't be referenced ...
3 into tdistribucion (id) values (2) --> ... by 2
4 select * from dual;
2 rows created.
SQL> alter table tdistribucion add constraint
2 fk_dis_doc foreign key (id)
3 references tdocumentos (id);
fk_dis_doc foreign key (id)
*
ERROR at line 2:
ORA-02298: cannot validate (SCOTT.FK_DIS_DOC) - parent keys not found
But, if you create it as enable novalidate, it'll "discard" rows that violate the constraint, but will check any subsequent inserts:
SQL> alter table tdistribucion add constraint
2 fk_dis_doc foreign key (id)
3 references tdocumentos (id)
4 enable novalidate;
Table altered.
Just to show that rows that violate the constraint really exist:
SQL> select * from tdocumentos;
ID
----------
1
SQL> select * from tdistribucion;
ID
----------
2
This is OK:
SQL> insert all
2 into tdocumentos (id) values (100)
3 into tdistribucion (id) values (100)
4 select * from dual;
2 rows created.
This isn't OK:
SQL> insert all
2 into tdocumentos (id) values (300)
3 into tdistribucion (id) values (400)
4 select * from dual;
insert all
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.FK_DIS_DOC) violated - parent key not
found
SQL>

SQL "Alter Table" for foreign key

So I'm learning some SQL as a bit of a side project since my SQL sucks. I have the following table I created:
CREATE TABLE deliveries (
pid INTEGER,
FOREIGN KEY (pid) REFERENCES person_lives_at,
);
I want to alter it to be a table like this one:
CREATE TABLE deliveries (
pid INTEGER,
FOREIGN KEY (pid) REFERENCES employee,
);
How can I achieve that? I'm using Oracle SQL Developer
Since you created an FK constraint without a name, oracle assigned a system-generated name to the constraint like SYS_xxxxxx. To find the constraint name:
select constraint_name from all_Constraints
where table_name = 'DELIVERIES'
In my test case, it returned "SYS_C0016779". I then drop the constraint:
alter table deliveries drop constraint SYS_C0016779
Then add the new constraint:
ALTER TABLE deliveries
ADD CONSTRAINT PID_EMP_FK -- or whatever you want to call it.
FOREIGN KEY (pid)
REFERENCES employee(pid); -- or whatever the name of the column is

Firebird foreign key and references from another table

So I have a problem with adding foreign key to my table "Tekmovalec". I would like to add new key to that table by using references from another table called "Kraj". Somehow I always get error when I try to add foreign key, so I made new database again and copied all commands I have done so far, here.
So Does anyone have any idea why this doesn't work?
making new database:
SQL> create database "c:\Baze\Tekmovanje.fdb" user 'sysdba' password 'masterkey';
SQL> create domain Rezultat as integer default 18 not null
CON> check (value between 1 and 32);
SQL>
SQL> create table Tekmovalec(
CON> TekmovalecID integer not null,
CON> ime char(10) not null,
CON> priimek char(20) not null,
CON> vzdevek char(10),
CON> rojen date,
CON> tocke rezultat,
CON> primary key (tekmovalecID));
SQL>
SQL> create table Kraj(
CON> krajID int not null,
CON> imekraja char(20) not null,
CON> primary key(krajID));
SQL>
SQL> alter table Tekmovalec add foreign key (krajID) references kraj (krajID)
CON> on delete no action on update cascade;
SQL>
and then I get this error when I hit enter after alter table Tekmovalec add foreign key ....
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Unknown columns in index RDB$FOREIGN3
SQL>
Before adding foreign key relation, it is necessary to have joining column present in both the tables.
krajID column is present in Kraj table but it is missing in Tekmovalec table.
There are 2 thing that 2 can do:
Drop table Tekmovalec and recreate it by adding krajID column. Like:
create table Tekmovalec(
....
krajID int,
primary key (tekmovalecID));
Alter the existing table and add new column. Like:
alter table Tekmovalec add krajID int
Add new column to table