Search
 
SCRIPT & CODE EXAMPLE
 

SQL

alter table add foreign key mysql

ALTER TABLE orders
ADD 
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; 
Comment

foreign key mySql

CREATE TABLE parent (
    id INT NOT NULL,
    PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE child (
    id INT,
    parent_id INT,
    INDEX par_ind (parent_id),
    FOREIGN KEY (parent_id)
        REFERENCES parent(id)
        ON DELETE CASCADE
) ENGINE=INNODB;
Comment

Mysql Create table with foreign keys.

CREATE TABLE products(
    productId INT AUTO_INCREMENT PRIMARY KEY,
    productName varchar(100) not null,
    categoryId INT NOT NULL,
    CONSTRAINT fk_category
    FOREIGN KEY (categoryId) 
    REFERENCES categories(categoryId)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=INNODB;
Comment

how to connect database foreign key in mysql


        
            
        
     CREATE TABLE products(
    productId INT AUTO_INCREMENT PRIMARY KEY,
    productName varchar(100) not null,
    categoryId INT NOT NULL,
    CONSTRAINT fk_category
    FOREIGN KEY (categoryId) 
    REFERENCES categories(categoryId)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=INNODB;
Comment

mysql foreign key constraint

ALTER TABLE `tabl`
  ADD CONSTRAINT `constraint_name` FOREIGN KEY (`column`) REFERENCES `table2` (`id`) ON DELETE CASCADE;
Comment

MySQL Foreign Key

Here is the basic syntax of defining a foreign key constraint in the CREATE TABLE or ALTER TABLE statement:

[CONSTRAINT constraint_name]
FOREIGN KEY [foreign_key_name] (column_name, ...)
REFERENCES parent_table(colunm_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
In this syntax:

First, specify the name of foreign key constraint that you want to create after the CONSTRAINT keyword. If you omit the constraint name, MySQL automatically generates a name for the foreign key constraint.

Second, specify a list of comma-separated foreign key columns after the FOREIGN KEY keywords. The foreign key name is also optional and is generated automatically if you skip it.

Third, specify the parent table followed by a list of comma-separated columns to which the foreign key columns reference.

Finally, specify how foreign key maintains the referential integrity between the child and parent tables by using the ON DELETE and ON UPDATE clauses.  The reference_option determines action which MySQL will take when values in the parent key columns are deleted (ON DELETE) or updated (ON UPDATE).

MySQL has five reference options: CASCADE, SET NULL, NO ACTION, RESTRICT, and SET DEFAULT.

CASCADE: if a row from the parent table is deleted or updated, the values of the matching rows in the child table automatically deleted or updated.
SET NULL:  if a row from the parent table is deleted or updated, the values of the foreign key column (or columns) in the child table are set to NULL.
RESTRICT:  if a row from the parent table has a matching row in the child table, MySQL rejects deleting or updating rows in the parent table.
NO ACTION: is the same as RESTRICT.
SET DEFAULT: is recognized by the MySQL parser. However, this action is rejected by both InnoDB and NDB tables.
In fact, MySQL fully supports three actions: RESTRICT, CASCADE and SET NULL.

If you don’t specify the ON DELETE and ON UPDATE clause, the default action is RESTRICT.

MySQL FOREIGN KEY examples
Let’s create a new database called fkdemo for the demonstration.

CREATE DATABASE fkdemo;

USE fkdemo;
RESTRICT & NO ACTION actions
Inside the fkdemo database, create two tables categories and products:

CREATE TABLE categories(
    categoryId INT AUTO_INCREMENT PRIMARY KEY,
    categoryName VARCHAR(100) NOT NULL
) ENGINE=INNODB;

CREATE TABLE products(
    productId INT AUTO_INCREMENT PRIMARY KEY,
    productName varchar(100) not null,
    categoryId INT,
    CONSTRAINT fk_category
    FOREIGN KEY (categoryId) 
        REFERENCES categories(categoryId)
) ENGINE=INNODB;
The categoryId in the products table is the foreign key column that refers to the categoryId column in the  categories table.

Because we don’t specify any ON UPDATE and ON DELETE clauses, the default action is RESTRICT for both update and delete operation.
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql select update same table 
Sql :: mysql show all table from database 
Sql :: mysql query single row 
Sql :: generate a random otp in sql server 
Sql :: show the colums of table sql 
Sql :: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client vs code 
Sql :: how to get the date from datetime in mysql 
Sql :: sqlite show table indexes 
Sql :: how to drop all tables in sql 
Sql :: search mysql database for column 
Sql :: how to check table exists or not in postgresql 
Sql :: sql date format 
Sql :: events mysql 
Sql :: how to retrive the today date sql 
Sql :: add primary key with auto increment to existing table column mysql 
Sql :: current timestamp in milliseconds mysql 
Sql :: oracle leftmost characters 
Sql :: CONVERT time string into 12 hours in sql 
Sql :: how to insert multiple rows in sql 
Sql :: sql count null as 0 
Sql :: oracle temporary table 
Sql :: sql email Regex 
Sql :: snowflake drop column 
Sql :: MySQL INSERT IGNORE Statement 
Sql :: mysql delete entire row on condition 
Sql :: how to create external table in hive 
Sql :: sql server create constraint 
Sql :: how to add month in update sql 
Sql :: 11:04:35 PM [mysql] Error: MySQL shutdown unexpectedly. 11:04:35 PM [mysql] This may be due to a blocked port, missing dependencies, 
Sql :: q operator in sql 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =