In SQL developer, when PRIMARY KEY is added like this:
PRIMARY KEY("ID")
In CONSTRAINT is generated with a name like "SYS006321". I need to change this name.
I tried to ADD:
CONSTRAINT TABLE_NAME_PK PRIMARY ("ID")
but I can't because can be only one PRIMARY KEY. This PRIMARY KEY is used in FOREIGN KEYs in others tables. So if i want to drop this PRIMARY KEY and after that add CONSTRAINT. I have to drop with cascade, so i lose references in FOREIGN KEY. What should I do?
If this is Oracle you can rename a constraint like this:
ALTER TABLE yourTable RENAME CONSTRAINT SYS006321 TO yourNewName;
First drop the foreign keys that point to the Primary Key.
Then drop the Primary Key.
Then re-create the primary key with the name you want.
Then re-create the foreign keys.
Related
I am applying a foreign key constraint to a table(datareal.official). Should I always refer to the same columns in the primary key of the other tabel (datareal.officialcarriage). Or could I theoretically also use difference in the reference table?
ALTER TABLE datareal.official
ADD CONSTRAINT FK_carriage FOREIGN KEY
(target,goal)
REFERENCES datareal.officialcarriage (target,goal)
ON DELETE CASCADE
ON UPDATE CASCADE
ALTER TABLE `SWIMMER`
ADD CONSTRAINT `fk5` FOREIGN KEY (`CoachID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`),
ADD CONSTRAINT `fk1` FOREIGN KEY (`MemberID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`);
I'm using this SQL query and unfortunately it runs but the designer mode in phpmyadmin isn't showing up the relation between the TeamMember table and the Swimmer table which is a supertype and subtype relation in this database. Please Let me know if you can do this kind of sql query or you need to make some other connection.
ALTER TABLE `SWIMMER`
ADD CONSTRAINT `fk5` FOREIGN KEY (`CoachID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`),
ADD CONSTRAINT `fk1` FOREIGN KEY (`MemberID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`);
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)
);
can we define a primary key in a table as a foreign key in that table . I mean,
PRIMARY KEY(ssn),
FOREIGN KEY (ssn) REFERENCES Cust(cust_ssn)
And If we have a table that has some parameters that refers to some other table parameters and to some other 3rd table too. Then Do we need to define those parameters as foreign key referencing to both the tables or Just only one.
Yes. Any field or combination of fields can be a foreign key.
it is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table
Yes - you can have a foreign key that references a unique index in another table.
CREATE UNIQUE INDEX UX01_YourTable ON dbo.YourTable(SomeUniqueColumn)
ALTER TABLE dbo.YourChildTable
ADD CONSTRAINT FK_ChildTable_Table
FOREIGN KEY(YourFKColumn) REFERENCES dbo.YourTable(SomeUniqueColumn)
By definition a foreign key must reference a candidate key of some table. It doesn't necessarily have to be the primary key.
As a matter of detail the constraint called a FOREIGN KEY in SQL isn't exactly equivalent to the textbook definition of a foreign key in the relational model. SQL's FOREIGN KEY constraint differs because:
it can reference any set of columns subject to a uniqueness constraint even if they are not candidate keys (superkeys or nullable columns for example).
it may include nulls, in which case the constraint is not enforced
its syntax depends on column order, so a fk constraint on (A,B) referencing (A,B) is different to a constraint on (B,A) referencing (A,B).
Yes , There can be a foreign key which is unique key in other table as Unique key is subset of primary key but not the exact primary key.
So that's possible that foreign key is unique key in aother table.
General standard answer is no. It is only possible if foreign key refers any column uniquely in other table. That means foreign key must be the candidate key in other table and primary key is also a candidate key the table.