Default Constraint in SQL Server - Part 4
Default Constraint in SQL Server - Part 4
To drop a constraint
ALTER TABLE { TABLE_NAME }
DROP CONSTRAINT { CONSTRAINT_NAME }
If the BOOLEAN_EXPRESSION returns true, then the CHECK constraint allows the value,
otherwise it doesn't. Since, AGE is a nullable column, it's possible to pass null for this column,
when inserting a row. When you pass NULL for the AGE column, the boolean expression
evaluates to UNKNOWN, and allows the value.
Check Constraint-ul permite NULL-urile daca nu este specificat clar sa nu o faca
To drop the CHECK constraint:
ALTER TABLE tblPerson
DROP CONSTRAINT CK_tblPerson_Age
If a column is marked as an identity column, then the values for this column are automatically
generated, when you insert a new row into the table. The following, create table statement
marks PersonId as an identity column with seed = 1 and Identity Increment = 1. Seed and
Increment values are optional. If you don't specify the identity and seed they both default to 1.
Create Table tblPerson
(
PersonId int Identity(1,1) Primary Key,
Name nvarchar(20)
)
How to get the last generated identity column value in SQL Server - Part
8
SCOPE_IDENTITY() - returns the last identity value that is created in the same session and in
the same scope. à Returneaza ultimul ID din ultima sesiune creat in ultimul scope.
@@IDENTITY - returns the last identity value that is created in the same session and across
any scope. à Returneaza ultimul ID din ultima sesiune din Orice Scope. (Ex: daca asa avea
un trigger care sa insereze un ID cand afac un alt insert, imi va returna pe cel din Inserul
facut de trigger.)
IDENT_CURRENT('TableName') - returns the last identity value that is created for a specific
table across any session and any scope. à Returneaza ultimul ID din Orice sesiune (Querry
page) din Orice Scope. (Ex: daca asa avea un trigger care sa insereze un ID cand fac un
alt insert, imi va returna pe cel din Inserul facut de trigger. Chiar daca scope identity a
fost facut in alta pagina de Querry.)
Unique key constraint - Part 9
Query:
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee E
LEFT JOIN tblDepartment D
ON E.DepartmentId = D.Id
WHERE D.Id IS NULL
How to retrieve only the non matching rows from the right table
Query:
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee E
RIGHT JOIN tblDepartment D
ON E.DepartmentId = D.Id
WHERE E.DepartmentId IS NULL
How to retrieve only the non matching rows from both the left and right table. Matching
rows should be eliminated.
Query:
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee E
FULL JOIN tblDepartment D
ON E.DepartmentId = D.Id
WHERE E.DepartmentId IS NULL
OR D.Id IS NULL
Different ways to replace NULL in sql server - Part 15
Replacing NULL value using ISNULL() function: We are passing 2 parameters to IsNULL()
function. If M.Name returns NULL, then 'No Manager' string is used as the replacement value.
SELECT E.Name as Employee, ISNULL(M.Name,'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
Replacing NULL value using COALESCE() function: COALESCE() function, returns the first
NON NULL value.
SELECT E.Name as Employee, COALESCE(M.Name, 'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
According to the MSDN Books online COALESCE() returns the first Non NULL value. Let's
understand this with an example.
Now, let's write a query that returns the Name of the Employee. If an employee, has all the
columns filled - First, Middle and Last Names, then we only want the first name.
If the FirstName is NULL, and if Middle and Last Names are filled then, we only want
the middle name. For example, Employee row with Id = 1, has the FirstName filled, so we want
to retrieve his FirstName "Sam". Employee row with Id = 2, has Middle and Last names
filled, but the First name is missing. Here, we want to retrieve his middle name "Todd". In short,
The output of the query should be as shown below.
UNION and UNION ALL operators in SQL Server, are used to combine the result-set of two or
more SELECT queries.
If you want to sort, the results of UNION or UNION ALL, the ORDER BY caluse should be
used on the last SELECT statement as shown below.
Select Id, Name, Email from tblIndiaCustomers
UNION ALL
Select Id, Name, Email from tblUKCustomers
UNION ALL
Select Id, Name, Email from tblUSCustomers
Order by Name