filmov
tv
Foreign key in sql with example (create table with foreign key)

Показать описание
desc customers -- table structure
create table loan_request
(
loan_requset_id int not null primary key,
customer_id references customers(customer_id),
loan_amount int,
loan_type_id int
);
insert into loan_request values(1,3,100,1);
select * from loan_request
----------------------------------------------
SQL - ORACLE
-----------
CUSTOMERS table is created
with customer_id as primary key
and customer_name columns
Now to create a
loan_request table with
customer_id column referencing
CUSTOMERS table customer_id,
customer_id int references customers(customer_id)
-- now lets see data in customers table
-- insert values into loan_request_table
-- 1 and 2 customer_id is present in the
customers table
-- lets try to insert the values not present
in the customers table
It is thrwoing error as 3 customer_id is not
present in the customers table
as the customer_id in the loan_request table
is referring the customer_id in the customers
table.
create table loan_request
(
loan_requset_id int not null primary key,
customer_id references customers(customer_id),
loan_amount int,
loan_type_id int
);
insert into loan_request values(1,3,100,1);
select * from loan_request
----------------------------------------------
SQL - ORACLE
-----------
CUSTOMERS table is created
with customer_id as primary key
and customer_name columns
Now to create a
loan_request table with
customer_id column referencing
CUSTOMERS table customer_id,
customer_id int references customers(customer_id)
-- now lets see data in customers table
-- insert values into loan_request_table
-- 1 and 2 customer_id is present in the
customers table
-- lets try to insert the values not present
in the customers table
It is thrwoing error as 3 customer_id is not
present in the customers table
as the customer_id in the loan_request table
is referring the customer_id in the customers
table.