Have tried looking through the code up-and-down and cannot get it to work. Was mostly trying out the Create table function. Worked fine until trying to create a table with composite primary key. Absolutely stumped. Any help is appreciated.
Tried removing the names for the keys, but then was redirected to a different sort of error: "invalid datatype"
Create Table Cust_Artist_EOI (ArtistID Number(38) Not Null,
CustomerID Number(38) Not Null,
Constraint Cust_Artist_EOI_PK Primary Key(ArtistID, CustomerID),
Constraint Cust_Artist_EOI_ArtistFK Foreign Key(ArtistID)
References MyArtist(ArtistID)
On Update No Action
On Delete Cascade,
Constraint Cust_Artist_EOI_CustFK Foreign Key(CustomerID)
References MyCustomer(CustomerID)
On Update No Action
On Delete Cascade);
The problem is the on update. Remove that:
Create Table Cust_Artist_EOI (
ArtistID Number(38) Not Null,
CustomerID Number(38) Not Null,
Constraint Cust_Artist_EOI_PK Primary Key(ArtistID, CustomerID),
Constraint Cust_Artist_EOI_ArtistFK Foreign Key(ArtistID)
References MyArtist(ArtistID) On Delete Cascade,
Constraint Cust_Artist_EOI_CustFK Foreign Key(CustomerID)
References MyCustomer(CustomerID) On Delete Cascade
);
Here is a db<>fiddle.
If you look in the syntax diagram for foreign key constraint in Oracle, you will see that neither on update nor no action is supported.
I'm not sure why no action is not supported, because that is the default behavior (and I think it is a good idea to be able to express default behavior).
Not supporting on update is a better idea. It discourages changes to primary keys. And changing primary keys is generally a bad idea.
Related
I have multiple tables,
-Student(StudentID[pk],StudentName)
-Qualified(FID[pk],CourseID[pk],dateQ)
-Faculty(FID[pk],Fname)
-Course(CourseID[pk],CourseName)
And i need to create 2 more, which are Section, and Registration.
-Section(SectionNo[pk],Semester[pk],CourseID[pk])
-Registration(StudentID[pk],SectionNo[pk],Semester[pk])
I first create section without any issues:
create table section(
SectionNo number(28) not null,
Semester varchar(25) not null,
CourseID varchar(25) not null,
constraint sec_pk primary key(SectionNo,Semester,CourseID),
constraint sec_fk foreign key(CourseID) references Course(CourseID)
on delete cascade);
Then I try to make a table called registration, but it gives me the error in the title.
create table registration(
StudentID number(28) not null,
SectionNo number(28) not null,
Semester varchar(25) not null,
constraint reg_pk primary key(SectionNo,StudentID,Semester),
constraint reg_fk foreign key(StudentID) references Student(StudentID)
on delete cascade,
constraint reg_fk2 foreign key(SectionNo,Semester) references
Section(SectionNo,Semester) on delete cascade);
Can someone please help me figure out what the issue is?
The ORA-2270 error is quite simple: it happens when the columns we reference in the foreign key do not match a primary key or unique constraint on the parent table.
Here, in your case, the primary key of section table is (SectionNo,Semester,CourseID) while you are referring only to Section(SectionNo,Semester)
To get rid of this please add "CourseID in your secondary key as well
A good read : Oracle (ORA-02270) : no matching unique or primary key for this column-list error
I am trying to add foreign key to my existing column using below query
ALTER TABLE Sub_Category_Master
ADD FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
but I'm getting an error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Sub_Categ__Categ__5812160E". The conflict occurred in database "shaadikarbefikar_new", table "shaadikarbefikar.Category_Master", column 'Category_ID'.
Well, the error clearly tells you that Category_ID in your Sub_Category_Master table contains some values that are not present in Category_Master (column Category_ID). But that's exactly the point of having a foreign key constraint - making sure your child table (Sub_Category_Master) only uses defined values from its parent table.
Therefore, you must fix those "voodoo" values first, before you're able to establish this foreign key relationship. I would also strongly recommend to explicitly name that constraint yourself, to avoid those system-generated, but not really very useful constraint names like FK__Sub_Categ__Categ__5812160E:
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FK_SubCategoryMaster_CategoryMaster
FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FKSub_Category_Master_Category_ID FOREIGN KEY (Category_ID)
REFERENCES Category_Master(Category_ID);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.
I am trying to create a new table with "on delete" and "on update" referential triggered actions, but it keeps saying that there is a syntax error. When I tried to run the script, the error message is "missing right parentheses". I don't understand why this is not right.
CREATE TABLE PERSON
(SSN VARCHAR(9) NOT NULL,
Name VARCHAR(20) NOT NULL,
Phone VARCHAR(10),
Address VARCHAR(40),
Employer VARCHAR(20),
Insurer VARCHAR(20),
PCP INT,
CONSTRAINT PERPPK
PRIMARY KEY(SSN),
CONSTRAINT PEREMPFK
FOREIGN KEY(Employer) REFERENCES EMPLOYER(Name)
ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT PERINSFK
FOREIGN KEY(Insurer) REFERENCES INSURER(Name)
ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT PERPCP
FOREIGN KEY(PCP) REFERENCES PHYSICIAN(PID)
ON DELETE SET NULL ON UPDATE CASCADE);
ON UPDATE CASCADE is not valid syntax. There is no direct way to do this and for good reason. You should not be creating a model where you have to update values in Primary keys. Primary keys should only be created and dropped, never updated. If it is being updated, then it is probably not a primary key. But that is a design consideration that you would have to make.
If you want to implement something like cascading updates, you could maybe do it with a stored procedure and deferrable constraints. Defer the constraint and update both the parent and foreign keys before committing.
Here is the asktom discussion on this. Another.
I have the following T-SQL in VS2013:
CREATE TABLE [dbo].[Jugo]
(
[JugoID] INT IDENTITY (1, 1) NOT NULL,
[Jugo] NVARCHAR (50) NOT NULL,
[ColorID] INT NOT NULL,
[IngreID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([JugoID] ASC),
FOREIGN KEY (ColorID) REFERENCES Color (ColorID)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (IngreID) REFERENCES Ingrediente (IngreID)
ON UPDATE CASCADE ON DELETE CASCADE
);
And I get the following message before updating the database:
Highlights
None
User actions
Create
Foreign Key: unnamed constraint on [dbo].[Jugo] (Foreign Key)
Foreign Key: unnamed constraint on [dbo].[Jugo] (Foreign Key)
Supporting actions
None
Can I proceed with these unnamed constraints? What would I need the constraints for?
For reference this is what I'm trying to do:
3 tables:
Jugo, Color, Ingrediente: so to create a jugo (juice) you specify name, color and ingredients, color and ingredients are on their own tables, hence the foreign keys I want to define.
Thanks.
As long as the ColorID and IngreID are both primary key in their own tables, you won't have any trouble.
But here's the catch, you didn't constrain your primary keys. It is a good practice to use constraint for every PRIMARY KEY you are using, in that way, when you foreign key it to another table. It can read that it is from another table. I don't know about SQL in VS2013 But here's my sample for SQL Server:
CREATE TABLE JUGO
(
JUGOID numeric identity (1,1) //For auto increment
CONSTRAINT PK_JUGOID PRIMARY KEY (JUGOID),
JUGO nvarchar(50) not null,
.
.
.
)
The PK_JUGOID in CONSTRAINT PK_JUGOID PRIMARY KEY (JUGOID), is user-defined, but it is a good practice to just copy the format of the primary key so you won't be confused when using foreign keys. :)
Foreign keys are for data consistency. You create the foreign keys on columns, so as not to allow invalid entries.
FOREIGN KEY (ColorID) REFERENCES Color (ColorID)
ON UPDATE CASCADE ON DELETE CASCADE,
This constraint makes sure that your jugo can only have a color that exists in the color table. Moreover you have two cascade commands:
ON UPDATE CASCADE means that if color red is ID 5, but you want to change it to 500, the change will be performed automatically for all jugo records. However, an ID should never change, so this is a clause seldom used.
ON DELETE CASCADE means that if you delete red, then you delete all red jugo records with it. I don't know if this is an appropriate case for you. It is used for instance on a users table. Remove a user, so you remove its complete data. But remove a color?
At last you should give your constraints names, so it's easier for you to deal with them.
CONSTRAINT pk_jugo PRIMARY KEY CLUSTERED ([JugoID] ASC),
CONSTRAINT fk_jugo_color FOREIGN KEY (ColorID) REFERENCES Color (ColorID),
CONSTRAINT fk_jugo_ingre FOREIGN KEY (IngreID) REFERENCES Ingrediente (IngreID)