Primary Key Constraints, Joins,
Sample Tables
Cross join 1
Self join
Except Output
Set Operators
3
3
CT *
SELE Table 1 Table 2
SELECT *
FROM employees
FROM employees e1
A primary key is a unique identifier for ROSS JOIN departments; INNER JOIN employees e2
a record in a database table.
C
ON e1.department_id = Get the rows from Employee_Details1
e2.department_id;
except the rows that are present in
Syntax for creating primary key: Employee_Details2 table.
Create table student123
:
Except Ret rnu s distinct rows from the result SELECT * FROM Employee_Details1
(
of the first SELECT query that are not present EX EPT
epartment
C
student_id int unique,
in the result of the second SELECT query. SELECT * FROM Employee_Details2;
student_name varchar(30),
D
student_age int check (student_age>0)
Adding primary key constraints using Alter D fe ault
U se to give a default value for a column Set Operators
Alter table student1
Syntax
if the user not giving any value
Add constraint pk_student_idprimary key (student_id)
Employee_Details1 Employee_Details2
Create table student_course
if not null is not active on the column on (
which you are trying to make the primary student_id int foreign key references student(student_id),
Inner join
key using the alter command first you have student_course varchar(100),
to make that column not null. rofessor archar(100) Default to be decided
P V ' '
SELECT *
) FROM employees
INNER JOIN departments
Alter table student1
ON
Set Operators
Alter column student_id int not null Adding default constraints using Alter employees.department_id =
departments.department_id;
Alter table student_course
Foreign key
Union Union All Intersect Except
Add default to be decided for professor
' '
A foreign key is a column or a set of Set operators are used to combine two result set together SQL
columns in a database table that
heck onstraint Left join
nion Output
C C
refers to the primary key of another
table, establishing a link between the U
SELECT *
two tables. se to give a default value for a column
U
FROM employees
if the user not giving any value LEFT JOIN departments
ON
Get all distinct rows between
Create table student_course
employees.department_id = Employee_Details1 and
(
Create table student
departments.department_id; Employee_Details2 table.
student_id int foreign keyreferences student(student_id),
(
student_course varchar(100),
student_id int primary key,
professor varchar(100)
student_name varchar(30),
SELECT * FROM Employee_Details1
) student_age int check (student_age>0)
UNION
UNION : Combines the result sets of two
) SELECT * FROM Employee_Details2; SELECT queries, removing duplicate rows.
Adding foreign key constraints
using Alter Adding c eck constraints using Alter
nion All Output
h
Right join
U
Alter table student_course
Alter table student
CT *
SELE
Add constraint fk_student_Id foreign key references Add constraint ck_age check(student_age>0) FROM employees
Get all rows with duplicates between
student(student_id) RIGHT JOIN departments
Employee_Details1 and
ON
Employee_Details2 table.
SQL
employees.department_id =
Joins
departments.department_id;
U nique Key
Inner Join
SELECT * FROM Employee_Details1
A unique constraint ensures that all UNION ALL
ALL: Combines the result sets of
Cheat Sheet
UNION
values in a specified column or SELECT * FROM Employee_Details2; two SELECT queries, including all rows,
combination of columns are distinct
including duplicates.
within a database table.
Full join
Create table student123
SELECT *
Intersect Output
(
FROM employees
student_id int unique,
Left Join Joins Right Join FULL JOIN departments
student_name varchar(30),
ON
student_age int check (student_age>0)
employees.department_id =
) departments.department_id; Get common rows between Employee_Details1
and Employee_Details2 table
Adding unique constraints using Alter
: u s common rows
INTERSE T Ret rn
C SELECT * FROM Employee_Details1
Full Outer
Join
between the result sets of two SELECT INTERSE T
queries, removing duplicates SELECT * FROM Employee_Details2;
ALTER TABLE student
ADD CONSTRAINT uk_studentid UNIQUE (student_id)