Answers for "mysql set foreign key"

SQL
6

alter table add foreign key mysql

ALTER TABLE orders
ADD 
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
Posted by: Guest on May-29-2020
1

mysql modify foreign key

-- 2 Step process
ALTER TABLE tbl_name_1 DROP FOREIGN KEY fk_name;
ALTER TABLE tbl_name_1 ADD FOREIGN KEY fk_name(fk_col)
            REFERENCES tbl_name_2(pk_name) ON DELETE RESTRICT;


-- Use to find the name of the FOREIGN KEY 
SHOW CREATE TABLE tbl_name;
Posted by: Guest on July-20-2021
25

mysql add foreign key

-- On Create
CREATE TABLE tableName (
    ID INT,
    SomeEntityID INT,
    PRIMARY KEY (ID),
    FOREIGN KEY (SomeEntityID)
        REFERENCES SomeEntityTable(ID)
        ON DELETE CASCADE
);

-- On Alter, if the column already exists but has no FK
ALTER TABLE
  tableName
ADD
  FOREIGN KEY (SomeEntityID) REFERENCES SomeEntityTable(ID) ON DELETE CASCADE;
  
 -- Add FK with a specific name
 -- On Alter, if the column already exists but has no FK
ALTER TABLE
  tableName
ADD CONSTRAINT fk_name
  FOREIGN KEY (SomeEntityID) REFERENCES SomeEntityTable(ID) ON DELETE CASCADE;
Posted by: Guest on April-27-2020
11

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;
Posted by: Guest on March-02-2020
1

mysql alter add foreign key

ALTER TABLE ordenes ADD ticket VARCHAR(50) NOT NULL;
ALTER TABLE ordenes ADD CONSTRAINT fk_ticket FOREIGN KEY (ticket) REFERENCES tickets(ticket);
Posted by: Guest on October-19-2020
1

update foreign key value in mysql

SET foreign_key_checks = 0;
UPDATE languages SET id='xyz' WHERE id='abc';
UPDATE categories_languages SET language_id='xyz' WHERE language_id='abc';
SET foreign_key_checks = 1;
Posted by: Guest on July-27-2020

Code answers related to "SQL"

Browse Popular Code Answers by Language