# 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
create table Jobs(
job_id number not null,
job_title varchar(30),
min_salary number,
max_salary number
);
create table job_history(
employee_id number not null,
start_date date,
end_date date,
job_id number not null,
department_id number
);
alter table jobs add constraint pk_jobs primary key(job_id);
alter table job_history add constraint fk_job foreign key(job_id) references jobs(job_id);
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
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id int REFERENCES Customers(id)
);
FOREIGN KEY (fk_col_name)
REFERENCES target_table_name (pk_col_name);
A foreign key is an attribute of a relation (subordinate/dependent)
that points to the primary key of another relation (main/master)
FK is used to create a many-to-one (one-to-many) relationship.
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
ALTER TABLE Table_Name
ADD column_name Data_Type,
CONSTRAINT FK_Name FOREIGN KEY(column_name)
REFERENCES PK_Table_Name(PK_Column_Name);
Code Example |
---|
Sql :: delete sql |
Sql :: sql cross apply vs join |
Sql :: get last inserted primary key |
Sql :: remove duplicates mysql |
Sql :: sql developer connect to sql server |
Sql :: create user in mysql |
Sql :: join multiple tables in sql |
Sql :: test database for sql |
Sql :: inspecting a column unique/distinct values in SQL |
Sql :: mysql foreign key |
Sql :: stored function in sql |
Sql :: mysql write into table |
Sql :: not in in mongodb |
Sql :: Insert Multiple Rows at Once in SQL |
Sql :: mysql ddl |
Sql :: mysql replace empty string with null |
Sql :: join sql |
Sql :: sql selet |
Sql :: cardinality example sql |
Sql :: sql from |
Sql :: sql to linq converter |
Sql :: restore backupfile discourse |
Sql :: online compiler for sql plus |
Sql :: ORACLE multiset union distinct |
Sql :: trncate table with relationships |
Sql :: sqldf change user |
Sql :: Un mask mysql |
Sql :: prestashop alter table if not exists |
Sql :: oracle dbms scheduler repeat interval every 5 minutes |
Sql :: update top 100 order by sql server |