Tatiana
0
Q:

alter table add column forigen key mysql

-- 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;
13
# A foreign key is essentially a reference to a primary
# key in another table.

# A Simple table of Users, 
CREATE TABLE users(
	userId INT NOT NULL,
  	username VARCHAR(64) NOT NULL,
  	passwd VARCHAR(32) NOT NULL,
  	PRIMARY KEY(userId);
);
# Lets add a LEGIT user!
INSERT INTO users VALUES(1000,"Terry","Teabagface$2");

# We will create an order table that holds a reference
# to an order made by our Terry
CREATE TABLE orders(
	orderId INT NOT NULL,
  	orderDescription VARCHAR(255),
  	ordererId INT NOT NULL,
  	PRIMARY KEY(orderId),
  	FOREIGN KEY (ordererId) REFERENCES users(userId)
);
# Now we can add an order from Terry
INSERT INTO orders VALUES(0001,"Goat p0rn Weekly",1000);

# Want to know more about the plight of Goats?
# See the link below
24
ALTER TABLE tryholpz_demo07.core_modules 
ADD COLUMN belongs_to_role INT, 
ADD FOREIGN KEY core_modules(belongs_to_role) REFERENCES role_specific_modules_info(id) ON DELETE CASCADE
4

ALTER TABLE Orders

ADD FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID); 
1
ALTER TABLE database.table
  ADD COLUMN columnname INT DEFAULT(1),
  ADD FOREIGN KEY fk_name(fk_column) REFERENCES reftable(refcolumn) ON DELETE CASCADE;
2
ALTER TABLE table_name
DROP CONSTRAINT fk_name;
1

New to Communities?

Join the community