0
MS SQL Server 2005  Lab # 2 :    SQL: Guide to the Language   (Components, Data types, Functions)    Installing    Prac...
SQL Basic Objects1.       Constants or Literal Values:            Alphanumerical            Numeric constant: Integer, F...
SQL Basic Objects1.       Constants or Literal Values:            Characters string:             enclosed in single (pre...
Adavnced Database Programming   4
Setting Delimited Identifier Usage Option   SET QUOTED_IDENTIFIER {ON, OFF}   IF :       ON:           Quotations can ...
SQL Basic Objects2.       Delimiters: ’ “ [ ] & ….3.       Comments:         /* Several Comment Liens Here */         --...
SQL Basic Objects5.       Identifiers:            Used to identify DB Objects such as: tables &             Indices     ...
Adavnced Database Programming   8
Data Types   Numeric Data Types  Numbers   String Data Types set of characters   Date and/or Time Data Types   Deriv...
Numeric Data Types (10 Types)   Data Type                   ExplanationINT              integer(4 Bytes)SMALLINT         i...
String Data Types (6 Types)   Data Type                 ExplanationCHAR[(n)] OR    String of n Characters, n MaximumCHARAC...
Binary Data Types (4 Types)   Data Type                     ExplanationBINARY [(n)]       A bit String of fixed length wit...
Maximizing Data Types with MAX   Where MAX=230   VARCHAR(MAX)   NVARCHAR(MAX)   VARBINARY(MAX)                    Adav...
Date & Time Data Types (2 Types)   Data Type             ExplanationDATETIME      Date & Time Stored in (4 Bytes)SMALLDATE...
Derived Data Types (2 Types)    Data Type                ExplanationTIMESTAMP       Maintains a current value for each    ...
Miscellaneous Data Types   Cursor: Used in stored procedure   UNIQUEIDENTIFIER: for Data Replication   SQL_VARIANT: Sto...
Predicates   A logical condition being applied to rows in a    table with Values of : True, False, unknown    or Not appl...
Aggregate Functions   Applied to a group of data values from    different rows, and returns a SINGLE value       AVG: Ap...
Scalar Functions   Used to construct Scalar expressions    (operates on a single or list of values)       Numeric Functi...
Scalar Functions: Numeric Functions  Function     Explanation     Function  ExplanationLOG (n)       Logarithm of n   EXP(...
Scalar Functions: Numeric Functions Function    Explanation              Function      ExplanationPower (x,n) =x n        ...
Scalar Functions: Date Functions  Function                 ExplanationGETDATE() Returns system Date & TimeDATEPART( Return...
Scalar Functions: String Functions   Function                   ExplanationASCII (ch)       Equivalent ASCII code of chCHA...
Scalar Functions: Text/Image Functions   PATINDEX (%pattern%,expr)       Returns an integer value specifying the positio...
Scalar Operators   ~      NOT   &      AND   |      OR   ^      XOR                  Adavnced Database Programming...
Installing SQL Server 2005 . . .   Installing Default Server   Installing Multi other Instances(Named    Instances)     ...
Upcoming SlideShare
Loading in...5
×

Sql server lab_2

144

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
144
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Sql server lab_2"

  1. 1. MS SQL Server 2005 Lab # 2 :  SQL: Guide to the Language (Components, Data types, Functions)  Installing  Practicing Example Adavnced Database Programming 1
  2. 2. SQL Basic Objects1. Constants or Literal Values:  Alphanumerical  Numeric constant: Integer, Fixed-Point, Float- Point Values  Hexadecimal:  Used to represent nonprintable characters  Each Starts with “0x” followed by even number of characters or numbers. EX. 0x53514C0D Adavnced Database Programming 2
  3. 3. SQL Basic Objects1. Constants or Literal Values:  Characters string:  enclosed in single (preferred) OR double quotation  To print single quotation in a statement add a single quotation to form Consecutive single quotations . EX. ‘You Can’’t agree’  Double quotation allows :  Delimited Identifiers: reserved keywords used as identifiers by using double quotation (helps to protect you from using names that could be introduced as reserved keywords in the SQL future), their name can begin with Or Contain any character  Spaces in the names of database objects Adavnced Database Programming 3
  4. 4. Adavnced Database Programming 4
  5. 5. Setting Delimited Identifier Usage Option SET QUOTED_IDENTIFIER {ON, OFF} IF :  ON:  Quotations can be used for delimiting Identifiers  Quotations can’t be used for delimiting Strings  OFF:  Quotations can’t be used for delimiting Identifiers  Quotations can be used for delimiting Strings Adavnced Database Programming 5
  6. 6. SQL Basic Objects2. Delimiters: ’ “ [ ] & ….3. Comments:  /* Several Comment Liens Here */  -- the Reminder of the Current Line is Comment2. Reserved Keywords  Can’t be used as object names Adavnced Database Programming 6
  7. 7. SQL Basic Objects5. Identifiers:  Used to identify DB Objects such as: tables & Indices  Names can be created with:  up to 128 characters: containing letters, numerals, _, @, #, $  Each name must start with a letter or one of the following characters (_, @, #, $)  # at the beginning of table or stored procedure name  denotes a temporary object  @ at the beginning of name  denotes a Variable Adavnced Database Programming 7
  8. 8. Adavnced Database Programming 8
  9. 9. Data Types Numeric Data Types  Numbers String Data Types set of characters Date and/or Time Data Types Derived Data Types Derived from simple data types Miscellaneous Data Types Data Types that doesn’t belong to other Data Types User-Defined Data Types  Adavnced Database Programming 9
  10. 10. Numeric Data Types (10 Types) Data Type ExplanationINT integer(4 Bytes)SMALLINT integer(2 Bytes)TINYINT Non-Negative Integer (0255)(1 Byte)BIGINT integer(8 Bytes)DECIMAL(p,[s]) P=precison(Total no of digits with S) S=Scale(No. of Decimal Point Digits)REAL Floating-Point ValuesMONEY- Decimal(8 Bytes), Rounded to 4 digitsSMALLMONEY after the decimal point - (4 Bytes)Float[(p)] P<25(4 Bytes), P>=25(8 Bytes) Adavnced Database Programming 10
  11. 11. String Data Types (6 Types) Data Type ExplanationCHAR[(n)] OR String of n Characters, n MaximumCHARACTER [(n)] value=800, if n is omitted n=1VARCHAR [(n)] String of (0<n<=8000) CharactersNCHAR [(n)] Fixed-length Unicode Char data(each char stored in 2 Byte), MAX=4000NVARCHAR [(n)] Varying-length Unicode Char data (each char stored in 2 Byte), MAX=4000TEXT [(n)] Fixed Length String up to 2GB(each char stored in 1 Byte)NTEXT [(n)] Large character data, MAX Bytes=230 (eachDatabase Programming in 2 Byte) Adavnced char stored 11
  12. 12. Binary Data Types (4 Types) Data Type ExplanationBINARY [(n)] A bit String of fixed length with exactly n bytes (0<n<=8000)VARBINARY [(n)] A bit String of Variable length with exactly n bytes (0<n<=8000)IMAGE [(n)] A bit String of fixed length with Unlimted values (231 Bytes) Can contain(audio/video, modulesBit Boolean Data type(True, False, NULL)TEXT/Image Contain any image type With Text Adavnced Database Programming 12
  13. 13. Maximizing Data Types with MAX Where MAX=230 VARCHAR(MAX) NVARCHAR(MAX) VARBINARY(MAX) Adavnced Database Programming 13
  14. 14. Date & Time Data Types (2 Types) Data Type ExplanationDATETIME Date & Time Stored in (4 Bytes)SMALLDATETIME Date & Time Stored in (2 Bytes) Date ( MM DD YYYY ) EX. “January 1 2007” OR ‘January 1 2007’ ‘1959 MAY 28’ Using SET DATEFORMAT dmy Time ( hh:mm AM or PM) EX. 8:45 AM, 4 pm Adavnced Database Programming 14
  15. 15. Derived Data Types (2 Types) Data Type ExplanationTIMESTAMP Maintains a current value for each DB which increments whenever any row with a timestamp column is inserted or updated Used to determine the relative time when rows where last changedSYSNAME Name of DB objects in the system catalog Adavnced Database Programming 15
  16. 16. Miscellaneous Data Types Cursor: Used in stored procedure UNIQUEIDENTIFIER: for Data Replication SQL_VARIANT: Store Values of Different Data Types TABLE:  Store Rows with several & different values  SQL Server Restrict its usage in: Local Variable & User-Defined Functions XML: Store XML Document in a DB Adavnced Database Programming 16
  17. 17. Predicates A logical condition being applied to rows in a table with Values of : True, False, unknown or Not applicable  All Relational Operators  BETWEEN Operator  IN Operator  LIKE Operator  NULL Operator  ALL and ANY Operator  EXISTS function Adavnced Database Programming 17
  18. 18. Aggregate Functions Applied to a group of data values from different rows, and returns a SINGLE value  AVG: Applied on numeric values Only  MAX,MIN: Applied on numeric, String, Date/Time values  SUM: Applied on numeric values  COUNT: Counts number of non-null data values COUNT in a column (only Function not applied to a Column, its applied to rows)  COUNT_BIG: Returns same as COUNT but in BIGINT data type Adavnced Database Programming 18
  19. 19. Scalar Functions Used to construct Scalar expressions (operates on a single or list of values)  Numeric Functions  Date Functions  String Functions  Text/Image Functions  System Functions Adavnced Database Programming 19
  20. 20. Scalar Functions: Numeric Functions Function Explanation Function ExplanationLOG (n) Logarithm of n EXP(n) =enACOS(n) Arc COS(n) Cosine,Float cosine,FloatASIN(n) Arc sine,Float SIN(n) sine,FloatATAN(n) Arc Tan(n) tangent,Float tangent,FloatCEILING(n) Smallest Floor(n) Largest integer integer value value less or greater or equal to n equal to nDEGREES(n) Converts Database Programming Adavnced Radians( Converts 20
  21. 21. Scalar Functions: Numeric Functions Function Explanation Function ExplanationPower (x,n) =x n Sign(n) Return -1 for negative & +1 for positivePI() Return (3.14) ABS(n) Absolute ValueRAND Returns SQUARE =n2 random (n) between 0 - 1 SQRT(n) =√n (float) Adavnced Database Programming 21
  22. 22. Scalar Functions: Date Functions Function ExplanationGETDATE() Returns system Date & TimeDATEPART( Returns specified item of a dateitem,date) Ex. Select datepart(weekday,’01.01.2005’)=1DATENAME Returns specified item of a date as a(item,date) character string, Ex. Select datename(weekday,’01.01.2005’)=SaturdayDATEDIFF Returns the difference between dat1 & dat2 in(item,dat1, integer Num of itemdat2) Ex. Select datediff(year,birthday,getdate())DATEADD(it Adds the number of units itemto dateem,number, Ex. Select dateadd(day,3,birthdate) AS Agedate) from Employee Programming Adavnced Database 22
  23. 23. Scalar Functions: String Functions Function ExplanationASCII (ch) Equivalent ASCII code of chCHAR(integer) Equivalent Character of ASCII integerLOWER(z) Convert all uppercase to lowercase Ex. Select lower(“BIG”)=bigUPPER(Z) Convert all lowercase to uppercase Ex. Select upper(“big”)=BIGRIGHT(z,length) Returns length characters from z Ex. Select right(“notebook”,4)=book Adavnced Database Programming 23
  24. 24. Scalar Functions: Text/Image Functions PATINDEX (%pattern%,expr)  Returns an integer value specifying the position of the string pattern in expr  Ex. Select patindex(%gs%,’longstring’)=4 Adavnced Database Programming 24
  25. 25. Scalar Operators ~  NOT &  AND |  OR ^  XOR Adavnced Database Programming 25
  26. 26. Installing SQL Server 2005 . . . Installing Default Server Installing Multi other Instances(Named Instances) Adavnced Database Programming 26
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×