Search
 
SCRIPT & CODE EXAMPLE
 

SQL

how to add foreign key constraint in sql

ALTER TABLE 'table_name'
ADD CONSTRAINT 'constraint_name' FOREIGN KEY 'foreign_key' REFERENCES 'column_name'('primary_key');
Comment

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
Comment

sql foreign key

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);
Comment

foreign key in sql

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
Comment

SQL FOREIGN KEY Constraint

CREATE TABLE Orders (
  order_id INT PRIMARY KEY,
  customer_id int REFERENCES Customers(id)
);
Comment

PREVIOUS NEXT
Code Example
Sql :: oracle grants 
Sql :: Uncaught Error: Cannot use object of type mysqli_result as array 
Sql :: postgresql restore from dump 
Sql :: sql server reseed identity column 
Sql :: mysql persistence.xml 
Sql :: oracle create as select 
Sql :: wsl centos 8 mysql 
Sql :: sql server cannot create database diagram 
Sql :: add auto increment column mysql 
Sql :: how to change column type psql 
Sql :: alter table name 
Sql :: stop and start mysql 
Sql :: mysql incrementation 
Sql :: make a field auto_increment mysql 
Sql :: postgresql substring 
Sql :: mssql how to insert more than 1000 rows 
Sql :: zsh: command not found: mysql mamp 
Sql :: t sql get foreign key 
Sql :: oracle tables with column name 
Sql :: insert query in ci 
Sql :: drop unique key constraint in sql server 
Sql :: pandas to sql index 
Sql :: Get Minimum from multiple columns sql 
Sql :: show tables in cassandra cql 
Sql :: duplicate table sql 
Sql :: change date format in oracle query 
Sql :: postgresql create user 
Sql :: SQL COUNT() with DISTINCT 
Sql :: add column if not exists mysql 
Sql :: DB: in eloquent using sql 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =