Documente Academic
Documente Profesional
Documente Cultură
Note: Put n before any data type to support Unicode Ex: nvarchar (30)
SQL Languages
DDL - Data Definition Language: CREATE, ALTER, DROP, TRUNCATE TRUNCATE: delete all rows in the table and format it, it is faster than DELETE and also when you enter new data in the table after TRUNCATE it'll start ID from 1, not like DELETE which starts the new data after the Last ID deleted. CREATE DATABASE Lab203 CREATE TABLE Instructors(Ins_ID int primary key identity, Ins_Name nvarchar(200), Ins_Phone varchar(14), Ins_DOB date) ALTER TABLE Instructors ADD Ins_Title nvarchar(50) ALTER TABLE Instructors ALTER COLUMN Ins_Mobile nvarchar(50)
DCL - Data Control Language --> GRANT, REVOKE TCL - Transactional Control Language --> COMMIT, ROLLBACK, SAVEPOINT DQL - Documentum/Data Query Language --> SELECT
Operators:
Arithmetic Operators: +-/*% * and / have much priority than + and 5+8-10/2+4*6
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 2 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
<, >, <>, >=, <=, == <> not equal, == if equal Logical Operators: AND, OR, BETWEEN Concatenation Operation: Between Names Ex: ______ + _______
Functions:
Function: it is a set of commands which will be Executed when you call it System Functions Built-in Functions: Aggregate Function SUM, AVG, MIN, MAX, COUNT, COUNT(*) Ex: SELECT MAX(Ins_Sal) FROM Instructors GETDATE() --> for showing the time now (system clock)
Control of flow: BEGIN..END, BREAK, CONTINUE, GOTO, IF...ELSE, RETURN, TRY...CATCH, WAITFOR, WHILE
Variables:
Ex1: DECLARE @X int SET @X=5 DECLARE @Y nvarchar(5) SET @Y='Ahmed' IF @Y='Ahmed' BEGIN SELECT @X+5 END
Ex2:
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 3 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
DECLARE @Degree int SET @Degree=100 IF @Degree>110 BEGIN SELECT'Pass' END ELSE BEGIN SELECT'Fail' END
UPDATE Instructors SET Ins_Sal=Ins_Sal+100 WHERE Ins_ID=1 SELECT*From Instructors WHERE Ins_Sal=1600 SELECT*From Instructors WHERE Ins_Sal IN(1,2,3)
NULL
SELECT * From Instructors WHERE Ins_Phone is NULL NOT NULL SELECT*From Instructors WHERE Ins_Phone is NOT NULL
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 4 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
ISNULL (Ins_Main, N'Not Determine Yet') AS Ins_Main From Instructors OR (for Arabic write N) SELECT Ins_ID, Ins_Name, Ins_Phone, Ins_DOB, Ins_Sal, Ins_Dept, ISNULL (Ins_Main, N' )' AS Ins_Main From Instructors
COALESCE --> Returns the first Not Null Expression among its arguments SELECT COAlESCE(Ins_Phone, Ins_Mobile)From Instructors
DISTINCT --> Returns unrepeated data from table Ex: 1000,1000,1200,1200,1400 will appear like this: 1000,1200,1400 SELECT DISTINCT Ins_Sal From Instructors
GROUP BY --> grouping data SELECT SUM(Ins_Sal)As Salary From Instructors GROUPBY Ins_Main
ROLLUP SELECT Ins_Dept, Ins_Main,MAX(Ins_Sal)AS Salary From Instructors GROUP BY ROLLUP (Ins_Dept, Ins_Main)
CUBE SELECT Ins_Dept, Ins_Main,MAX(Ins_Sal)AS Salary From Instructors GROUP BY CUBE (Ins_Dept, Ins_Main)
COMPUTE COLUMN:
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 5 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
SELECT*From Instructors COMPUTE SUM(Ins_Sal) Important This feature (COMPUTE) will be removed in the next version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. Use ROLLUP instead. For more information (Microsoft)
JOIN:
INNER JOIN (Self Join) --> when returning data from the same table OUTER JOIN (RIGHT & LEFT) --> returning data from 2 tables, depending on right or left CROSS JOIN --> possibilities of conjunctions between 2 tables FULL JOIN --> returning data from 2 tables, depending on the two tables
SELECT Emp_Name, Emp_Sal FROM Salary RIGHT (OR) LEFT OUTERJOIN Employees ON Salary.Emp_ID = Employees.Emp_ID
CROSS JOIN: SELECT Employee.Ins_Mgr_ID AS Ins_ID, Employee.Ins_Name As Instructor , Manager.Ins_Name As Manager From Instructors AS Manager INNERJOIN Instructors AS Employee ON Manager.Ins_Mgr_ID=Employee.Ins_Mgr_ID
INTERSECTION: SELECT Ins_Name, Ins_N_ID From Instructors INTERSECT SELECT Emp_Name, Emp_N_ID From Employees
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 6 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
RANK:
ROW_NUMBER() RANK DENSE_RANK() NTILE
SELECT ROW_NUMBER()OVER (PARTITIONBY Ins_Main ORDERBY Ins_Sal)AS Salary, Ins_Main, Ins_Name, Ins_Sal From Instructors SELECT RANK()OVER (PARTITIONBY Ins_Main ORDERBY Ins_Sal)AS Salary, Ins_Main, Ins_Name, Ins_Sal from Instructors SELECT DENSE_RANK()OVER (PARTITIONBY Ins_Main ORDERBY Ins_Sal)AS Salary, Ins_Main, Ins_Name, Ins_Sal from Instructors SELECT NTILE(4)OVER (PARTITIONBY Ins_Main ORDERBY Ins_Sal)AS Salary, Ins_Main, Ins_Name, Ins_Sal from Instructors
PIVOT:
Ins_Name Mohammed Ahmed Mohammed Mohammed Ahmed Ins_Main SQL SQL ORACLE SQL ORACLE Experience_y 2 2 3 1 3
SELECT*From Experience PIVOT (SUM(Experience_y)FOR Ins_Main In([SQL],[ORACLE])) PVT Result: Ins_Name Mohammed Ahmed SQL 3 2 ORACLE 3 3
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 7 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
Sub Queries
SELECT*From Instructors WHERE Ins_Sal>(SELECT AVG(Ins_Sal)From Instructors) UPDATE Instructors SET Ins_Sal=Ins_Sal+100 WHERE Ins_Sal<(SELECT AVG(Ins_Sal)From Instructors)
Case:
Get data for instructors with salaries which are bigger than the average of every department 1. 2. 3. 4. Get departments averages Get salaries which bigger than averages Choose the smallest number from the averages Use this number
SELECT*From Instructors WHERE Ins_Sal>(SELECT MIN(Stable1.Sal)from (SELECT AVG(Ins_Sal)AS Sal from Instructors GROUPBY Ins_Dept)AS Stable1)
ANY:
Case: Return any of salaries which bigger than average by departments SELECT*From Instructors WHERE Ins_Sal>ANY(SELECT AVG(Ins_Sal)AS SAL From Instructors GROUPBY Ins_Dept)
ALL:
Case: Return the biggest salary by departments SELECT*From Instructors WHERE Ins_Sal>ALL(SELECT AVG(Ins_Sal)AS SAL From Instructors GROUPBY Ins_Dept)
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 8 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
Temporary Table:
It is a normal table which you create it for a little period, and it'll deleted automatically after you disconnect from server Local: only one user can access it, and you can use it for storing data for a little period and it'll deleted after you disconnect from server Global: many users can access it, but it'll be deleted when you disconnected from server
INSERTED and DELETED When you insert a new row, it'll be copied to a Table called INSERTED When you delete a row, it'll be copied to a Table called DELETED There isn't an UPDATED Table because update is process of delete and insert
INSERT
INSERT INTO INSERT SELECT: --> Insert data by selecting it from another table INSERTINTO Employees(Emp_Name, Emp_Phone, Emp_Sal) SELECT Ins_Name, Ins_Phone, Ins_Sal from Instructors
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 9 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
Output Insert: Declare @Table1 Table (ID int, Name nvarchar(60), N_ID nvarchar(50)) INSERT Employees OUTPUT inserted.Emp_ID, inserted.Emp_Name, inserted.N_ID INTO @Table1 Values ('Ahmed','234234234') SELECT*From @Table1
DELETE
DELETE: DELETE Instructors WHERE Ins_Sal>ANY (SELECTAVG(Ins_Sal)From Instructors GROUPBY Ins_DEPT)
UPDATE:
UPDATE Employees SET Ins_Sal=4000
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 10 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
Where Emp_ID=5 UPDATE Instructors Set Ins_Sal=Ins_Sal+100 Where Ins_Sal=(SELECT MAX(Ins_Sal)From Instructors) View(Update): Declare @Table2 Table (New_Salary Decimal(18,2), Las_Salary Decimal(19,3)) UPDATE Instructors SET Ins_Sal=Ins_Sal+100 OUTPUT INSERTED.Ins_Sal, DELETED.Ins_Sal Into @Table2 SELECT*From @Table2
Convert: --> cannot convert big data type into smaller one SELECTCONVERT(nvarchar(50), Ins_ID)+' '+ Ins_Name As'ID&Name'From Instructors
CAST --> convert big data type to smaller one SELECT CAST(Ins_ID ASnvarchar(4))+' '+ Ins_Name As'ID&Name'From Instructors
View
CREATEVIEW New_View1 ASSELECT Ins_Name, Emp_Name, Emp_Sal From Instructors InnerJoin Employees On Employees.Ins_ID=Instructors.Ins_ID SELECT*From New_View1 You cannot use these constraints with VIEW: ORDER BY (only if you use TOP from 1 to 99.9%) COMPUTE GO
CREATEVIEW New_View2 ASSELECTTOP 99.9 PERCENT*From Instructors ORDERBY Ins_Sal SELECT*From New_View2
XML
Extensible Markup Language: helps you to convert data or transfer data Ex: From Microsoft SQL Server 2008 to ORACLE Uses: Between Systems (MS SQL Server to ORACLE) Shared file in Internet Encoding documents Serialized (Serializing Data)
Meta Data:
Meta data is data for data, there are 2 types of metadata: Catalog metadata View metadata
Catalog
SELECT*From sys.sysobjects
View
SELECT*From sys.objects SELECT*From sys.tables SELECT*From sys.views You can select columns from it SELECT name,object_id, create_date Fromsys.objects
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 12 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
To select columns names for a table from information schema: SELECT Column_Name From INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=N'Instructors'
Handle Error: Between TRY and CATCH you can print customized ERROR Message BEGIN TRY Declare @x int set @x=0 Select @x/0 END TRY BEGIN CATCH PRINT 'Error Lab 203 ' END CATCH
While: Function which repeats command DECLARE @X int, @Y int SET @X=10 SET @Y=5 WHILE (@X>5) BEGIN SET @Y=@Y+1 SET @X=@X-1 PRINT @Y END SELECT @X SELECT @Y
Continue:
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 13 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203
Ignores the next command and start from top DECLARE @X int, @Y int SET @X=10 SET @Y=0 WHILE (@X>3) BEGIN SET @Y=@Y+1 SET @X=@X-1 IF (@Y=10) BEGIN CONTINUE END PRINT @Y PRINT @X PRINT '----' END SELECT @X SELECT @Y
Cursor: Enables you to make fetch, to process the next row Declare @Balance int, @TodayRev int, @TodayName nvarchar(50) SET @Balance=0 Declare C1 Cursor For SELECT day_name, Rev FROM CursorSession OPEN C1 FETCH NEXT From C1 Into @TodayName, @TodayRev WHILE @@FETCH_STATUS=0 BEGIN SET @Balance=@Balance+@TodayRev Print (@TodayName+' '+CONVERT (nvarchar(50), @Balance)) FETCH NEXT From C1 Into @TodayName, @TodayRev END CLOSE C1 DEALLOCATE C1
Course 2778 - Writing Queries Using Microsoft SQL Server 2008 Transact-SQL - 14 MCIT Scholarship 2010-11 Database Development and Administration YAT Heliopolis - 203