SQL Server Lab 2

download SQL Server Lab 2

of 26

Transcript of SQL Server Lab 2

  • 8/8/2019 SQL Server Lab 2

    1/26

    Adavnced Database Programming 1

    MS SQL Server 2005MS SQL Server 2005

    Lab # 2 : SQL: Guide to the Language

    (Components, Data types, Functions) Installing Practicing Example

  • 8/8/2019 SQL Server Lab 2

    2/26

    Adavnced Database Programming 2

    SQL Basic Objects

    1.1. Constants or Literal Values:Constants or Literal Values:

    AlphanumericalAlphanumerical

    Numeric constant:Numeric constant: Integer, Fixed-Point, Float-Point Values

    Hexadecimal:Hexadecimal: Used to represent nonprintable characters

    Each Starts with 0x followed by even number ofcharacters or numbers. EX. 0x53514C0D

  • 8/8/2019 SQL Server Lab 2

    3/26

    Adavnced Database Programming 3

    SQL Basic Objects

    1.1. Constants or Literal Values:Constants or Literal Values: Characters string:Characters string:

    enclosed in single (preferred) OR double quotation

    To print single quotation in a statement add a singlequotation to form Consecutive single quotations .EX. You Cant agree

    Double quotation allows :Double quotation allows : Delimited Identifiers: reserved keywords used as

    identifiers by using double quotation (helps to protect youfrom using names that could be introduced as reservedkeywords in the SQL future), their name can begin with OrContain any character

    Spaces in the names of database objects

  • 8/8/2019 SQL Server Lab 2

    4/26

    Adavnced Database Programming 4

  • 8/8/2019 SQL Server Lab 2

    5/26

    Adavnced Database Programming 5

    Setting Delimited Identifier Usage Option

    SET QUOTED_IDENTIFIER {ON, OFF}

    IF :

    ON: Quotations can be used for delimiting Identifiers

    Quotations cant be used for delimiting Strings

    OFF:

    Quotations cant be used for delimiting Identifiers Quotations can be used for delimiting Strings

  • 8/8/2019 SQL Server Lab 2

    6/26

    Adavnced Database Programming 6

    SQL Basic Objects

    2.2. Delimiters:Delimiters: [ ] & .

    3.3. Comments:Comments:

    /*/*

    Several Comment Liens Here*/*/

    ---- the Reminder of the Current Line is Comment

    4.4. Reserved KeywordsReserved Keywords

    Cant be used as object names

  • 8/8/2019 SQL Server Lab 2

    7/26

    Adavnced Database Programming 7

    SQL Basic Objects

    5.5. Identifiers: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

  • 8/8/2019 SQL Server Lab 2

    8/26

    Transact- SQL

    ReservedKeywords

    Delimiters

    Identifiers

    Constants

    A

    lphanumericalHexadecimalNumericStrings

    Adavnced Database Programming 8

  • 8/8/2019 SQL Server Lab 2

    9/26

    Adavnced Database Programming 9

    Data Types

    NumericNumeric Data Types Numbers

    StringString Data Types set of characters

    Date and/or TimeDate and/or Time Data Types Derived DataDerived Data Types Derived from simple

    data types

    MiscellaneousMiscellaneous Data Types Data Types thatdoesnt belong to other Data Types

    UserUser--DefinedDefined Data Types

  • 8/8/2019 SQL Server Lab 2

    10/26

    Adavnced Database Programming 10

    Numeric Data Types (10 Types)Data TypeData Type ExplanationExplanation

    INTINT integer(4 Bytes)

    SMALLINTSMALLINT integer(2 Bytes)

    TINYINTTINYINT Non-Negative Integer (0255)(1 Byte)

    BIGINTBIGINT integer(8 Bytes)DECIMAL(p,[s])DECIMAL(p,[s]) P=precison(Total no of digits with S)

    S=Scale(No. of Decimal Point Digits)

    REALREAL Floating-Point Values

    MONEYMONEY--

    SMALLMONEYSMALLMONEY

    Decimal(8 Bytes), Rounded to 4 digitsafter the decimal point - (4 Bytes)

    Float[(p)]Float[(p)] P=25(8 Bytes)

  • 8/8/2019 SQL Server Lab 2

    11/26

    Adavnced Database Programming 11

    String Data Types (6 Types)Data TypeData Type ExplanationExplanation

    CHAR[(n)]CHAR[(n)] ORCHARACTER [(n)]CHARACTER [(n)]

    String of n Characters, n Maximumvalue=800, if n is omitted n=1

    VARCHAR [(n)]VARCHAR [(n)] String of (0

  • 8/8/2019 SQL Server Lab 2

    12/26

    Adavnced Database Programming 12

    Binary Data Types (4 Types)Data TypeData Type ExplanationExplanation

    BINARY [(n)]BINARY [(n)] A bit String of fixed length with exactlyn bytes (0

  • 8/8/2019 SQL Server Lab 2

    13/26

    Adavnced Database Programming 13

    Maximizing Data Types with MAX

    Where MAX=Where MAX=223030

    VARCHAR(MAX)

    NVA

    RCHA

    R(MA

    X) VARBINARY(MAX)

  • 8/8/2019 SQL Server Lab 2

    14/26

    Adavnced Database Programming 14

    Date & Time Data Types (2 Types)

    Data TypeData Type ExplanationExplanationDATETIMEDATETIME Date & Time Stored in (4 Bytes)

    SMALLDATETIMESMALLDATETIME Date & Time Stored in (2 Bytes)

    Date ( MM DD YYYY )EX. January 1 2007 OR January 1 2007

    1959 MAY 28

    Using SET DATEFORMAT dmyTime ( hh:mm AM or PM)

    EX. 8:45 AM, 4 pm

  • 8/8/2019 SQL Server Lab 2

    15/26

    Adavnced Database Programming 15

    Derived Data Types (2 Types)

    Data TypeData Type ExplanationExplanationTIMESTAMPTIMESTAMP Maintains a current value for each

    DB which increments whenever anyrow with a timestamp column is

    inserted or updatedUsed to determine the relative timewhen rows where last changed

    SYSNAMESYSNAME Name of DB objects in the system

    catalog

  • 8/8/2019 SQL Server Lab 2

    16/26

    Adavnced Database Programming 16

    Miscellaneous Data Types

    Cursor:Cursor: Used in stored procedure

    UNIQUEIDENTIFIER:UNIQUEIDENTIFIER: for Data Replication

    SQL_VARIANT:SQL_VARIANT: Store Values of Different DataTypes TABLE:TABLE:

    Store Rows with several & different values

    SQL Server Restrict its usage in: Local Variable &User-Defined Functions

    XML:XML: Store XML Document in a DB

  • 8/8/2019 SQL Server Lab 2

    17/26

    Adavnced Database Programming 17

    Predicates

    A logical condition being applied to rows in atable with Values of : True, False, unknownor Not applicable All Relational Operators BETWEENBETWEEN Operator ININ Operator LIKELIKE Operator NULLNULL Operator ALLALL and ANYANY Operator EXISTSEXISTS function

  • 8/8/2019 SQL Server Lab 2

    18/26

    Adavnced Database Programming 18

    Aggregate Functions

    Applied to a group of data values fromdifferent rows, and returns a SINGLE value AVG:AVG: Applied on numeric values Only MAX,MIN:MAX,MIN: Applied on numeric, String, Date/Time

    values SUM:SUM: Applied on numeric values COUNTCOUNT: Counts number of non-null data values

    in a column (only Function not applied to aColumn, its applied to rows) COUNT_BIG:COUNT_BIG: Returns same as COUNT but in

    BIGINT data type

  • 8/8/2019 SQL Server Lab 2

    19/26

    Adavnced Database Programming 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

  • 8/8/2019 SQL Server Lab 2

    20/26

    Adavnced Database Programming 20

    Scalar Functions: Numeric Functions

    FunctionFunction ExplanationExplanation FunctionFunction ExplanationExplanation

    LOG (n)LOG (n) Logarithm of n EXP(n)EXP(n) =en

    ACOS(n)ACOS(n) Arccosine,Float

    COS(n)COS(n) Cosine,Float

    ASIN(n)ASIN(n) Arc sine,Float SIN(n)SIN(n) sine,Float

    ATAN(n)ATAN(n) Arctangent,Float

    Tan(n)Tan(n) tangent,Float

    CEILING(n)CEILING(n) Smallestinteger valuegreater orequal to n

    Floor(n)Floor(n) Largest integervalue less orequal to n

    DEGREES(n)DEGREES(n) Converts Radians(Radians( Converts

  • 8/8/2019 SQL Server Lab 2

    21/26

    Adavnced Database Programming 21

    Scalar Functions: Numeric Functions

    FunctionFunction ExplanationExplanation FunctionFunction ExplanationExplanation

    Power (x,n)Power (x,n) =x n Sign(n)Sign(n) Return -1 fornegative & +1for positive

    PI()PI() Return (3.14) ABS(n)ABS(n) Absolute Value

    RANDRAND Returnsrandom

    between 0 - 1(float)

    SQUARESQUARE

    (n)(n)

    SQRT(n)SQRT(n)

    =n2

    =n

  • 8/8/2019 SQL Server Lab 2

    22/26

    Adavnced Database Programming 22

    Scalar Functions: Date FunctionsFunctionFunction ExplanationExplanation

    GETDATE()GETDATE() Returns system Date & Time

    DATEPART(iDATEPART(i

    tem,date)tem,date)

    Returns specified item of a dateEx. Select datepart(weekday,01.01.2005)=1

    DATENAME(DATENAME(item,date)item,date) Returns specified item of a date as acharacter string, Ex. Select

    datename(weekday,01.01.2005)=Saturday

    DATEDIFFDATEDIFF

    (item,dat1,(item,dat1,dat2)dat2)

    Returns the difference between dat1 & dat2 in

    integer Num of itemEx. Select datediff(year,birthday,getdate())

    DATEADD(itDATEADD(it

    em,number,em,number,

    date)date)

    Adds the number of units itemto date

    Ex. Select dateadd(day,3,birthdate) AS Agefrom Employee

  • 8/8/2019 SQL Server Lab 2

    23/26

    Adavnced Database Programming 23

    Scalar Functions: String FunctionsFunctionFunction ExplanationExplanation

    ASCII (ch)ASCII (ch) Equivalent ASCII code of ch

    CHAR(integer)CHAR(integer) Equivalent Character ofASCII integer

    LOWER(z)LOWER(z) Convert all uppercase to lowercase

    Ex. Select lower(BIG)=bigUPPER(Z)UPPER(Z) Convert all lowercase to uppercase

    Ex. Select upper(big)=BIG

    RIGHT(z,length

    )RIGHT(z,length

    ) Returns length characters from zEx. Select right(notebook,4)=book

  • 8/8/2019 SQL Server Lab 2

    24/26

    Adavnced Database Programming 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

  • 8/8/2019 SQL Server Lab 2

    25/26

    Adavnced Database Programming 25

    Scalar Operators

    ~ NOT

    & AND

    |

    OR ^ XOR

  • 8/8/2019 SQL Server Lab 2

    26/26

    Adavnced Database Programming 26

    Installing SQL Server 2005 . . .

    Installing Default Server

    Installing Multi other Instances(NamedInstances)