Documente Academic
Documente Profesional
Documente Cultură
2. Table:.................................................................................................................. 2
a) Create Table................................................................................................... 2
b)
c) Drop table....................................................................................................... 2
d)
3. Column............................................................................................................... 3
a) Add Column.................................................................................................... 3
b)
c) Drop Column................................................................................................... 3
d)
4. Database............................................................................................................ 3
a) Create Database............................................................................................ 3
b)
Rename Database...................................................................................... 3
c) Drop Database............................................................................................... 3
d)
CHECK........................................................................................................... 4
c) UNIQUE KEY.................................................................................................... 4
d)
PRIMARY KEY............................................................................................... 4
e) Foreign Key..................................................................................................... 4
f)
Candidate Key................................................................................................ 4
g) Alternate Key................................................................................................. 5
h)
Composite Key............................................................................................ 5
i)
Surrogate Key................................................................................................ 5
j)
Supper Key..................................................................................................... 5
6. View.................................................................................................................... 5
7. Triggers.............................................................................................................. 5
8. Joins.................................................................................................................... 8
a) Type of Joins:.................................................................................................. 8
b)
Inner Join:.................................................................................................... 9
c) Outer Join:....................................................................................................... 9
d)
g) Cross Join:..................................................................................................... 11
h)
Self-Join...................................................................................................... 12
Points of Interest...................................................................................... 12
9. Unions............................................................................................................... 13
10.
Index.............................................................................................................. 14
c)
Transaction Control (TCL) statements are used to manage the changes made by
DML statements. It allows statements to be grouped together into logical
transactions.
2. Table:
A table is a collection of related data held in a structured format within a database. It
consists of fields (columns), and rows
a) Create Table
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
c) Drop table
3. Column
a) Add Column
c) Drop Column
4. Database
A database is a collection of information that is organized so that it can easily be
accessed, managed, and updated.
a) Create Database
b) Rename Database
USE master
GO
ALTER DATABASE TestDB1
Modify Name = TestDB
GO
c) Drop Database
5. Constraints
SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is
aborted by the constraint.
Constraints can be specified when the table is created (inside the CREATE TABLE
statement) or after the table is created (inside the ALTER TABLE statement).
a) NOT NULL
Indicates that a column cannot store NULL value
b) CHECK
Ensures that the value in a column meets a specific condition
c) UNIQUE KEY
Ensures that each row for a column must have a unique value, and unique key
does allow a NULL value.
d) PRIMARY KEY
A Primary Key is a column or a combination of columns that uniquely identify a
record. Only one Candidate Key can be Primary Key. Primary Key does not allow
the NULL values.
A table can contain only one primary key constraint.
A primary key cannot exceed 16 columns and a total key length of 900
bytes.
The index generated by a primary key constraint cannot cause the number
of indexes on the table to exceed 999 non-clustered indexes and 1
clustered index.
If clustered or non-clustered is not specified for a primary key constraint,
clustered is used if there no clustered index on the table.
All columns defined within a primary key constraint must be defined as not
null. If null ability is not specified, all columns participating in a primary
key constraint have their null ability set to not null.
If a primary key is defined on a CLR user-defined type column, the
implementation of the type must support binary ordering.
e) Foreign Key
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
f) Candidate Key
A Candidate Key can be any column or a combination of columns that can qualify
as unique key in database. There can be multiple Candidate Keys in one table.
Each Candidate Key can qualify as Primary Key
g) Alternate Key
Any candidate key which is not selected as a primary key is called Alternate Key.
h) Composite Key
A key that is composed of more than one column is called Composite Key.
i) Surrogate Key
A surrogate key is a key which contains the numeric values, an Identity Key in SQL
Server is a surrogate key, it can be a primary key, it is very fast for comparison
purpose because it takes less time for comparison.
6. View
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table.
Example: 1
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
We can query the view above as follows:
SELECT * FROM [Current Product List]
Example: 2
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName
We can query the view above as follows:
SELECT * FROM [Current Product List]
7. Triggers
Triggers are a special kind of stored procedure that fires automatically; they are
executed when an event occurs in the database server. We can create Data
Manipulation Language (DML) triggers and Data Definition Language (DDL) triggers in
SQL Server 2012.
When the user wants to modify data using a DML event then the DML trigger is
executed. In other words, a DML trigger is used for INSERT, DELETE and UPDATE
statements of a table or view.
When the user attempts to perform an operation using DDL then the DDL trigger is
executed. In other words, a DDL trigger is executed for CREATE, ALTER and DROP
statements of a table or view.
There are three types of triggers in SQL Server 2012:
AFTER Trigger
INSTEAD OF Trigger
FOR Trigger
vendors_info
vendors_info
vendors_info
vendors_info
vendors_info
values
values
values
values
values
(20,'vipendra','noida','up')
(21,'deepak','lucknow','up')
(22,'rahul','kanpur','up')
(23,'malay','delhi','delhi')
(24,'mayank','noida','up')
8. Joins
Joins are used to get data from two or more tables based on relationship between
some of the columns in tables
a) Type of Joins:
Inner Join
Outer Join
o Left Outer Join
o Right Outer Join
o Full Outer Join
Self-Join
Cross Join
Employee Table:
Department Table:
b) Inner Join:
The join that displays only the rows that have a match in both the joined tables is
known as inner join.
Select
e1.Username,
e1.FirstName,
e1.LastName,
e2.DepartmentName
From Employee e1 Inner JoinDepartments e2 on e1.DepartID=e2.id
Output:
c) Outer Join:
Outer join returns all the rows of both tables whether it has matched or not.
Output:
Output:
Output:
g) Cross Join:
Cross Join that produces Cartesian product of the tables that are involved in the
join. The size of a Cartesian product is the number of the rows in the first table
multiplied by the number of rows in the second table like this.
SELECT
*
FROM Employee
Cross Join Departments e2
h) Self-Join
Joining the table itself called self-join. Self-join is used to retrieve the records
having some relation or similarity with other records in the same table.
Here, we need to use aliases for the same table to set a self-join between single
table and retrieve records satisfying the condition in where clause.
SELECT
e1.Username,
e1.FirstName,
e1.LastName fromEmployee e1
Inner Join Employee e2 on e1.id=e2.DepartID
Output:
Interesting Point:
Here, I have taken one example of self-join in this scenario where manager name
can be retrieved by manager-id with reference of employee id from one table.
Here, I have created one table employees like that:
Select
E1.empName as ManagerName,
E2.empName as EmpName
from employees e1
inner join employees e2
on e1.id=e2.managerid
Result:
9. Unions
The SQL UNION operator combines the result of two or more SELECT statements.
Union & Union ALL:
Notice that each SELECT statement within the UNION must have the same number
of columns. The columns must also have similar data types. Also, the columns in
each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate
values, use the ALL keyword with UNION.
10.
Index
Indexes allow the database application to find data fast; without reading the
whole table.
So, an index can be created in a table to find data more quickly and
efficiently.
The users cannot see the indexes; they are just used to speed up
searches/queries.
Note: Updating a table with indexes takes more time than updating a table
without (because the indexes also need an update). So you should only
create indexes on columns (and tables) that will be frequently searched
against.
table. The leaf level of a clustered index is the actual data and
the data is resorted in case of clustered index. Whereas in nonclustered index the leaf level is actually a pointer to the data
in rows so we can have as many non-clustered indexes as we
can on the
Clustered index is super-fast for searches, and slows down deletes
and update to data. If you have a primary key on your table, it is a
clustered index by default, so if you want to have another clustered
index, your primary key needs to be marked NOT clustered.