Académique Documents
Professionnel Documents
Culture Documents
Syntax:
IDENTITY [ (seed , increment ) ] seed: Is the value that is used for the very first row loaded into the table. increment: Is the incremental value that is added to the identity value of the previous row that was
loaded. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1). Example: Case1: Seed = 2; Increment = 2
CREATE TABLE [dbo].[Biodata]( [FirstName] [nvarchar](50) NOT NULL, [LastName] [nvarchar](50) NOT NULL, [Id] [int] NOT NULL IDENTITY(2,2), [Age] [int] NULL, [Address] [nvarchar](50) NULL, CONSTRAINT [PK_Biodata] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Case2: Default
CREATE TABLE [dbo].[Biodata]( [FirstName] [nvarchar](50) NOT NULL, [LastName] [nvarchar](50) NOT NULL, [Id] [int] NOT NULL IDENTITY, [Age] [int] NULL, [Address] [nvarchar](50) NULL, CONSTRAINT [PK_Biodata] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
NOTE: A Identity column can be set as the primary key of the table. In the above example the primary key is set on the table [dbo].[Biodata]on the column [Id] which is also an Identity Column.
--See the data present in tables Employee and Department select * from dbo.Employee select * from dbo.Department
Results: Employee Table:
Empid 1 2 3
Managerid 2 1 1
Departmentid 2 1 2
A004
Vivek
Kumar
vivek@abc.com
NULL
Deparment Table:
Departmenttid 1 2 3 4 DepartmentName Accounts Admin HR Technology
Now if the question is to fetch only the common rows from both the tables OR To fetch all the employees which belong to a particular department then use Inner join as below:
SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp INNER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid
Results: Empid 1 2 3 EmpFirstName Samir Amit Neha EmpLastName Singh Kumar Sharma DepartmentName Admin Accounts Admin
For example:
Now if the question is to fetch the Manager Name using the table Employee then you will have to use the Self Join because Manager Name is nothing but the Employee Name which is obtained from the Employee table by using a Self Join as below:
SELECT Emp1.Empid, Emp1.EmpFirstName+' '+Emp1.EmpLastName as EmployeeName, Emp2.EmpFirstName+' '+Emp2.EmpLastName as ManagerName FROM Employee Emp1 INNER JOIN Employee Emp2 ON Emp1.Managerid=Emp2.Empid
Results: Empid 1 2 3 4 EmployeeName Samir Singh Amit Kumar Neha Sharma Vivek Kumar ManagerName Amit Kumar Samir Singh Samir Singh Samir Singh
This type of join is needed when we need to select all the rows from the table on the left (or right or both) regardless of whether the other table has common values or not and it usually enter null values for the data which is missing. The Outer join can be of three types 1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
7. Explain Left Outer Join and Right Outer Join and Full Outer Join:
Left Outer Join: If we want to get employee id, employee first name, employes last name and their department name for all the employees regardless of whether they belong to any department or not,then we can use the left outer join. In this case we keep the Employee table on the left side of the join clause. It will insert NULL values for the data which is missing in the right table. For example:
--See the data present in tables Employee and Department select * from dbo.Employee select * from dbo.Department
Results: Employee Table: Empid 1 2 3 4 EmpNumber A001 A002 A003 A004 EmpFirstName Samir Amit Neha Vivek EmpLastName Singh Kumar Sharma Kumar EmpEmail samir@abc.com amit@abc.com neha@abc.com vivek@abc.com Managerid 2 1 1 1 Departmentid 2 1 2 NULL
Deparment Table:
Departmenttid 1 2 3 4
SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp LEFT OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid
Result: Empid 1 2 3 4
Right Outer Join: If we want to get all the departments name and employee id, employee first name, and employees last name of all the employees belonging to the department regardless of whether a department have employees or not, then we can use the right outer join. In this case we keep the Department table on the right side of the join clause. It will insert NULL values for the data which is missing in the left table (Employee).
SELECT Dept.DepartmentName, Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName FROM Employee Emp RIGHT OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmentid
Result:
Empid 2 1 3 NULL NULL EmpFirstName Amit Samir Neha NULL NULL EmpLastName Kumar Singh Sharma NULL NULL DepartmentName Accounts Admin Admin HR Technology
Full Outer Join: If we want to get all the departments name and the employee id, employee first name, employes last name of all the employees regardless of whether a department have employees or not, or whether a employee belong to a department or not, then we can use the full outer join. It will insert null values for the data which is missing in both the tables.
SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp FULL OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid
SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp CROSS JOIN Department dept
Result: Empid 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 EmpFirstName Samir Amit Neha Vivek Samir Amit Neha Vivek Samir Amit Neha Vivek Samir Amit Neha Vivek EmpLastName Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar DepartmentName Accounts Accounts Accounts Accounts Admin Admin Admin Admin HR HR HR HR Technology Technology Technology Technology
9. Explain Normalization:
Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: i. eliminating redundant data (for example, storing the same data in more than one table) ii. ensuring data dependencies make sense (only storing related data in a table).
Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored.