MySQLi Articles - Page 339 of 341

How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?

George John
Updated on 30-Jul-2019 22:30:21

11K+ Views

We can remove FOREIGN KEY constraint from a column of an existing table by using DROP keyword along with ALTER TABLE statement. Syntax ALTER TABLE table_name DROP FOREIGN KEY constraint_name Here constraint name is the name of foreign key constraint which we applied while creating the table. If no constraint name is specified then MySQL will provide constraint name which can be checked by SHOW CREATE TABLE statement. Example The following query will delete the FOREIGN KEY constraint from ‘orders’ table − mysql> Alter table orders DROP FOREIGN KEY orders_ibfk_1; Query OK, 0 rows affected (0.22 sec) ... Read More

What happens if I will delete a row from MySQL parent table?

karthikeya Boyini
Updated on 28-Jan-2020 07:19:14

1K+ Views

While deleting the row from the parent table, if the data of that row is used in the child table then MySQL will throw an error because of the failure of FOREIGN KEY constraint. It can be understood with the example of two tables named ‘customer’ and ‘orders’. Here, ‘customer’ is the parent table and ‘orders’ is the child table. We cannot delete a row, that is used in child table ‘orders’, from the ‘customer’ table. It can be demonstrated by deleting the values from the parent table as follows −mysql> Select * from Customer; +----+--------+ | id | name ... Read More

When are two tables connected with MySQL FOREIGN KEY then how can we say that the integrity of data is maintained in child table?

Swarali Sree
Updated on 28-Jan-2020 07:12:54

221 Views

Actually, foreign keys enforce referential integrity that helps us to maintain the consistency and integrity of the data automatically. It can be understood with the example of two tables named ‘customer’ and ‘orders’. Here, ‘customer’ is the parent table and ‘orders’ is the child table. We cannot create an order for a non-existent customer. It can be demonstrated by inserting the values in both the tables as follows −mysql> Select * from Customer; +----+--------+ | id | name   | +----+--------+ | 1  | Gaurav | | 2  | Raman  | | 3  | Harshit| | 4  | Aarav  | ... Read More

In case of FOREIGN KEY constraint, what kind of relationship is there between MySQL parent and child tables?

Vikyath Ram
Updated on 28-Jan-2020 07:11:41

513 Views

The relationship between parent and child table is One-to-Many relationship. It can be understood with the example of two tables named ‘customer’ and ‘orders’. Here, ‘customer’ is the parent table and ‘orders’ is the child table. The relationship is one-to—many because a customer can have more than one order. It can be demonstrated by inserting the values in both the tables as follows −mysql> Select * from Customer; +----+---------+ | id | name    | +----+---------+ | 1  | Gaurav  | | 2  | Raman   | | 3  | Harshit | | 4  | Aarav   | +----+---------+ ... Read More

How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?

Srinivas Gorla
Updated on 19-Jun-2020 11:58:10

949 Views

MySQL allows us to add a FOREIGN KEY constraint on more than one field in a table. The condition is that each Foreign Key in the child table must refer to the different parent table.ExampleSuppose we have a table ‘customer2’ which have a Primary Key constraint on the field ‘cust_unq_id’ as follows −mysql> describe customer2; +-------------+-------------+------+-----+---------+-------+ | Field       | Type        | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | cust_id     | int(11)     | YES  |     | NULL    |       | | First_name  | ... Read More

How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

2K+ Views

We can add a FOREIGN KEY constraint to a column of an existing MySQL table with the help of ALTER TABLE statement. Syntax ALTER TABLE table_name ADD FOREIGN KEY (colum_name) REFERENCES table having Primary Key(column_name); Example Suppose we want to add a FOREIGN KEY constraint on the table ‘Orders1’ referencing to the table ‘Customer’ which have column ‘Cust_Id’ as the Primary Key. It can be done with the help of the following query − mysql> Alter table orders1 add FOREIGN KEY(Cust_id) REFERENCES Customer(Cust_id); Query OK, 0 rows affected (0.21 sec) Records: 0  Duplicates: 0  Warnings: 0   mysql> ... Read More

How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?

Govinda Sai
Updated on 19-Jun-2020 11:56:51

4K+ Views

We can remove composite PRIMARY KEY constraint from multiple columns of an existing table by using DROP keyword along with ALTER TABLE statement.ExampleSuppose we have a table ‘Room_allotment’ having a composite PRIMARY KEY constraint on columns ‘ID’ and ‘RoomNo’ as follows −mysql> describe room_allotment; +--------+-------------+------+-----+---------+-------+ | Field  | Type        | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id     | int(11)     | NO   | PRI | 0       |       | | Name   | varchar(20) | NO   | PRI |         ... Read More

How can we set PRIMARY KEY on multiple columns of an existing MySQL table?

Syed Javed
Updated on 19-Jun-2020 11:56:21

10K+ Views

We can set PRIMARY KEY constraint on multiple columns of an existing table by using ADD keyword along with ALTER TABLE statement.ExampleSuppose we have a table ‘Room_allotment’ as follows −mysql> Create table Room_allotment(Id Int, Name Varchar(20), RoomNo Int); Query OK, 0 rows affected (0.20 sec) mysql> Describe Room_allotment; +--------+-------------+------+-----+---------+-------+ | Field  | Type        | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | Id     | int(11)     | YES  |     | NULL    |       | | Name   | varchar(20) | YES  |     | NULL ... Read More

What happens if I will try to drop PRIMARY KEY constraint from the AUTO_INCREMENT column?

Arushi
Updated on 19-Jun-2020 11:53:40

308 Views

As we know the AUTO_INCREMENT column must have the PRIMARY KEY constraint on it also hence when we will try to drop PRIMARY KEY constraint from the AUTO_INCREMENT column the MySQL returns an error message regarding the incorrect table definition. The example below will demonstrate it −ExampleSuppose we have ‘Accounts’ table having the following description −mysql> Describe accounts; +--------+-------------+------+-----+---------+----------------+ | Field  | Type        | Null | Key | Default | Extra          | +--------+-------------+------+-----+---------+----------------+ | Sr     | int(10)     | NO   | PRI | NULL    | auto_increment | ... Read More

How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?

Rishi Raj
Updated on 19-Jun-2020 11:53:03

5K+ Views

We can remove PRIMARY KEY constraint from a column of an existing table by using DROP keyword along with ALTER TABLE statement.ExampleSuppose we have a table ‘Player’ having a PRIMARY KEY constraint on column ‘ID’ as follows −mysql> DESCRIBE Player; +-------+-------------+------+-----+---------+-------+ | Field | Type        | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID    |  int(11)    | NO   | PRI | NULL    |       | | Name  | varchar(20) | YES  |     | NULL    |       | +-------+-------------+------+-----+---------+-------+ 2 rows in ... Read More

Advertisements