Answers for "what is foreign key"

24

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
4

primary key vs foreign key

Primary Key :
It is unique column in every table in a database
It can ONLY accept;
    - nonduplicate values
    - cannot be NULL
Foreign Key: 
It is a column that comes from a different table and
using Foreign key tables are related each other
It is the primary key of another table
It can be duplicate or null for another table

Unique Key:
Only unique value and also can contain NULL
Posted by: Guest on December-05-2020
41

sql foreign key

# 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
Posted by: Guest on February-24-2020
19

foreign key in sql

A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

Example:
# creating table users
CREATE TABLE users(
	user_id INT NOT NULL,
  	user_name VARCHAR(64) NOT NULL,
  	user_pass VARCHAR(32) NOT NULL,
  	PRIMARY KEY(user_id);
);
# adding user data
INSERT INTO users VALUES(1,"Raj","raj@123");

# creating table orders
CREATE TABLE orders(
	order_id INT NOT NULL,
  	order_description VARCHAR(255),
  	orderer_id INT NOT NULL,
  	PRIMARY KEY(order_id),
  	FOREIGN KEY (orderer_id) REFERENCES users(user_id)
);
# adding order data
INSERT INTO orders VALUES(1,"Daily groceries",1);
Posted by: Guest on April-25-2020
3

what is foreign key

Foreign Key: 
It is a column that comes from a different table and
using Foreign key tables are related each other
It is the primary key of another table
It can be duplicate or null for another table


Primary Key :
It is unique column in every table in a database
It can ONLY accept;
    - nonduplicate values
    - cannot be NULL

Unique Key:
Only unique value and also can contain NULL
Posted by: Guest on January-27-2021
1

what is foreign key

Foreign Key: 
It is a column that comes from a different table and
using Foreign key tables are related each other
It is the primary key of another table
It can be duplicate or null for another table
Posted by: Guest on January-07-2021

Browse Popular Code Answers by Language