Friday, August 5, 2016

SQL Foreign key Constraint



A Foreign key in one table points to a primary key in another table.
Firstly we create two tables:-
employee” table: -

E_Id
Name
Address
1
Sachin
Kanpur
2
Amit
Aligarh
3
Sumt
Etah

Emp” table
Emp_id
dept
E_Id
1
IT
2
2
sales
1

The SQL Foreign key constraint on create table
Create table Emp
(
Emp_id int NOT NULL PRIMARY KEY,
dept varchar (250) NOT NULL,
E_Id int Foreign Key references employee(E_Id)
)

SQL foreign Key constraint on ALTER Table
ALTER Table Emp
Add foreign key (E_Id)
References Employee (E_Id)

Drop a Foreign Key Constraint –
Alter table Emp
Drop contraint E_Id

                                                Author : - Sachin Pathak







Thursday, August 4, 2016

SQL Primary Key constraints



The primary key is used to uniquely identify each record in the table. Primary key contain unique value .The primary key cannot contain NULL value, each table contain only one primary key.

Primary key on create table –
Create table employee
Id int NOT NULL,
First name varchar(250) NOT NULL,
Last name varchar(250),
City varchar(250),
Salary int,
CONSTRAINT emp_id PRIMARY KEY (ID, First Name)

Primary key on alter table-
ALTER table employee
Add primary key(Id)

Drop a Primary Key –
ALTER table employee
Drop CONSTRAINT emp_id

                                                             Author - Sachin Pathak



Monday, August 1, 2016

The SQL UNION Operator



The UNION operator is used to combine the result of two or more SELECT Statement. The UNION operator work when each select statement have same number of column, the column must have same data type and also the order of the columns in the each SELECT statement be the same.

Example -
Firstly we create two table names emp and employee


select id from emp
union
select id from employee

 Output - 


                                                            Author - Sachin Pathak