SQL_Server_2008_ephrem
SQL_Server_2008_ephrem
Contents
1. DDL (DATA DEFINITION LANGUAGE) _________________________________________________ 3
4. SPECIAL ______________________________________________________________________________ 9
4.1. HOW TO ASSIGN OUR OWN NAME FOR THE COLUMNS IN THE DATABASE ________________________ 9
4.2. TRANSACT-SQL: LIKE ________________________________________________________________ 9
4.3. HOW TO USE GROUP BY ______________________________________________________________ 11
4.4. GETDATE () ________________________________________________________________________ 11
4.5. LEFT JOIN AND RIGHT JOIN ___________________________________________________________ 12
4.6. HOW TO CREATE VIEW _______________________________________________________________ 13
4.7. AVERAGE, MAXIMUM AND MINIMUM ___________________________________________________ 13
4.8. DISTINCT ________________________________________________________________________ 13
4.9. SELECT INTO _______________________________________________________________________ 14
4.10. TOP ______________________________________________________________________________ 14
4.11. HOW TO BACK UP A DATABASE ________________________________________________________ 14
If you want the name of the database to be in different words, include them in square brackets.
Here is an example:
Example :
use RegistrarDB
sp_help Resulttbl;
1.8. To truncate
The keyword truncate removes all values from the table once and cannot be rolled back.
The formula:
truncate table TableName;
example:
use RegistrarDB
truncate table StudStatustbl;
Or
use RegistrarDB
INSERT INTO [Coursetbl]([Ccode],[CTitle],[CCredit])
VALUES(‘c005’,’internet programming’,’3’);
Example :
use RegistrarDB
begin transaction
save transaction s1
delete from Coursetbl
where Ccode='c002';
2.5. Rollback / Restoring
ROLLBACK TRANSACTION without a savepoint_name or transaction_name rolls back to the
beginning of the transaction.
When nesting transactions, this same statement rolls back all inner transactions to the outermost
BEGIN TRANSACTION statement.
In both cases, ROLLBACK TRANSACTION decrements the @@TRANCOUNT system function to
0. ROLLBACK TRANSACTION savepoint_name does not decrement @@TRANCOUNT.
A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed,
except when the COMMIT TRANSACTION is associated with a nested transaction that is
contained within the transaction being rolled back.
In this instance, the nested transaction will also be rolled back, even if you have issued a
COMMIT TRANSACTION for it.
Within a transaction, duplicate savepoint names are allowed, but a ROLLBACK TRANSACTION
using the duplicate savepoint name rolls back only to the most recent SAVE TRANSACTION
using that savepoint name.
Example:
use RegistrarDB
begin transaction s1
rollback transaction
Use RegistrarDB
select * from Statussampleview;
select * from Resulttbl;
select * from CourseRegistrationtbl;
select *from Student_Backup;
select *from Coursetbl;
select *from StudStatustbl;
select Studenttbl.Studname,Studenttbl.StudDepartment,Resulttbl.LetterGrade from
Studenttbl,Resulttbl
where Resulttbl.Sid=Studenttbl.Sid;
4. Special
4.1. How to assign our own name for the columns in the database
Example:
use RegistrarDB
select Studname "Full Name",StudCollege "College",StudDepartment "Department" from
Studenttbl;
The Expression factor is the expression that will be evaluated. This must be a clear and valid
expression.
The pattern factor can be a value to be found in Expression. For example, it can be the same type of
value used in a WHERE statement. In this case, the equality operator would be the same as LIKE.
If you want to match any character, in any combination, for any length, use the %wildcard. If you
precede it with a letter, as in S%, the condition would consist of finding any string that starts with
S.
Imagine that you want to get a list of students whose last names start with S. You would type the
condition as LIKE 'S%'.
If you set the letter in between (%letter%) with a letter, as in %S%, the condition would consist of
finding any string that contains S in the middle.
Example:
select StudName,StudAge from Studenttbl where Studname like 'm%'; // display sstudent name from
student table where student name starts with ‘m’
select StudName,StudAge from Studenttbl where Studname like '%m'; // display sstudent name from
student table where student name end with ‘m’
select StudName,StudAge from Studenttbl where Studname like '%m%'; // display sstudent name from
student table where student name has ‘m’ letter in between.
select StudName,StudAge from Studenttbl where Studname like '[G or g]%'; // displays student name
from student table where name strats with upper case or small case g.
select StudName,StudAge from Studenttbl where Studname like '_t%'; // displays student name from
student table where second letter name is t.
select StudName,StudAge from Studenttbl where Studname not like '_t%';
Example:
use RegistrarDB
select Studenttbl.Studname,
Studenttbl.StudCollege,
StudStatustbl.CGPA,
StudStatustbl.StudStatus
from Studenttbl,StudStatustbl where Studenttbl.Sid=StudStatustbl.Sid;
example:
use RegistrarDB
select studenttbl.sid from Studenttbl
where Studenttbl.StudCollege like '[T or t]%'
intersect
(select StudStatustbl.Sid from StudStatustbl)
Example:
Select studCollege, count(*) as NumOfStudInEachCollege
From Studenttbl
Group by StudCollege;
use RegistrarDB
select studenttbl.sid from Studenttbl
where Studenttbl.StudCollege like '[T or t]%'
union
(select StudStatustbl.Sid from StudStatustbl)
Example:
Select * from Studenttbl order by studAge desc;
The right join returns all the rows from the second table (student Status table). Even if there are no
matches in the first table (student table)
Example
use RegistrarDB
select Studenttbl.Studname,StudStatustbl.StudStatus
from Studenttbl left join StudStatustbl ON
Studenttbl.Sid=StudStatustbl.Sid
Example:
use RegistrarDB
select Studenttbl.Studname,StudStatustbl.StudStatus
4.8. DISTINCT
Example
Select COUNT(DISTINCT studenttbl.studname) as ‘NO OF Students’
From studenttbl
Where studGender like ‘[m or M]%’;
4.10. Top
Example ;
Select top 5 studname
From studenttbl;