Difference between add constraint syntax in MySQL? - mysql

So, I am applying foreign key constraint on a column in MySQL table.
What I noticed is that I am able to do that in two ways -
ALTER TABLE book ADD CONSTRAINT fk_code_id FOREIGN KEY(book_type) REFERENCES code(id);
and
ALTER TABLE book ADD FOREIGN KEY(book_type) REFERENCES code(id);
Why do we have two ways in place to achieve same thing?

Alter table with Constraint option for adding check constraints to MySQL database tables.
The add constraint function allows the user to add a constraint name and a constraint condition.
https://razorsql.com/features/mysql_add_constraint.html
And by this ALTER TABLE book ADD FOREIGN KEY(book_type) REFERENCES code(id) you just make fk between two tables.

create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

Related

MariaDB - Foreign key constraint is incorrectly formed Error

I am using MariaDB and the DB schema looks like this:
And I've created those tables:
However, I didn't defined the foreign key when I created the table. But I am trying to use alter manipulation command to make a reference between two tables like this:
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow(uid);
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow (following_uid);
ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES feed (uid);
The first one was successful. But the other ones, I get this error:
Can't create table `SimpleSNS`.`#sql-6b94_5f` (errno: 150 "Foreign key constraint is incorrectly formed")
I checked InnoDB and the columns data type and its prototypes such as default or not null, etc. However, It didn't work.
What's the problem of this???
Create an index on "follow (following_uid)" before executing -> ALTER TABLE member ADD FOREIGN KEY (uid) REFERENCES follow (following_uid);

Does Heidi sql allow you to add more than one foreign key?

I have successfully added my first FK in heidisql using the foreign key tab and adding all the appropriate sections.
I have tried to do the same to my second related column both by using the FK tab and by running a query but I keep getting an error.
SQL Error (1005): Can't create table sprout.#sql-430_3 (errno: 150 "Foreign key constraint is incorrectly formed")
sprout is my db name so I have no idea why it is saying cant create table sprout (because I'm not referencing it in my query).
sql query for my first FK(generated via heidisql):
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_id` FOREIGN KEY (`bus_id`) REFERENCES `business` (`bus_id`);
sql query for my second FK(generated via heidisql)
ALTER TABLE `purchase_history`
ADD CONSTRAINT `bus_name_fk` FOREIGN KEY (`bus_name`) REFERENCES `business` (`bus_name`);
sql query I wrote to try and add second FK
Alter table purchase_history
Add constraint bus_name_fk
Foreign key (bus_name)
references business(bus_name);
Can someone help explain to me how my constraint is incorrectly formed? To my understanding I was able to add another constraint to the the table.
This is too long for a comment.
Huh? Why are you adding two foreign constraints to the same table . . . but using different columns? That doesn't really make sense. In general, the foreign key should be referencing the primary key of the other table, which I presume is bus_id.
Then, if you want the business name, you can use a join to get the name.

How to add Foreign keys?

So I made all of my tables already and just found out about Foreign keys and now I know I need them.
How would I add a relation between 2 columns from 2 tables? I see multiple variations of how Foreign key is used, how would I use mine in my case?
Use an ALTER TABLE query to modify the table to add the foreign key constraint.
ALTER TABLE yourTable
ADD CONSTRAINT FOREIGN KEY (col_in_yourTable) REFERENCES otherTable (col_in_otherTable);
The syntax after ADD CONSTRAINT is essentially the same as the CONSTRAINT clause of CREATE TABLE.

Adding foreign key constraint in table vs Alter table adding foreign key constraint

I have a newbie question here in terms of database design and I've noticed 2 options.
1) include the foreign key constrain in the create table block
2) create table then Alter table by ADD CONSTRAINT FOREIGN KEY
Appreciate your experienced view on this
1. include the foreign key constrain in the create table block
self explanatory, You're creating foreign key constraint in create table there are not records.
2) create table then Alter table by ADD CONSTRAINT FOREIGN KEY
If you don't have any records in table then this will behave as 1. If you've any records in tables then you need to take care whether the available records doesn't violates the foreign key constraints.

SQL Add foreign key to existing column

If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint:
ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)
UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error:
Foreign key 'UserID' references invalid column 'UserID' in referencing
table 'Employees'.
Error indicates that there is no UserID column in your Employees table. Try adding the column first and then re-run the statement.
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
Maybe you got your columns backwards??
ALTER TABLE Employees
ADD FOREIGN KEY (UserID) <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id) <-- this needs to be a column of the ActiveDirectories table
Could it be that the column is called ID in the Employees table, and UserID in the ActiveDirectories table?
Then your command should be:
ALTER TABLE Employees
ADD FOREIGN KEY (ID) <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID) <-- column in table "ActiveDirectories"
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
In the future.
ALTER TABLE Employees
ADD UserID int;
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyStudent
FOREIGN KEY FacId
REFERENCES Student StuId
way of foreign key creation correct for ActiveDirectories(id), i think the main mistake is you didn't mentioned primary key for id in ActiveDirectories table
If the table has already been created:
First do:
ALTER TABLE `table1_name` ADD UNIQUE( `column_name`);
Then:
ALTER TABLE `table1_name` ADD FOREIGN KEY (`column_name`) REFERENCES `table2_name`(`column_name`);