3-Data Manipulation Using SQL-Part1

download 3-Data Manipulation Using SQL-Part1

of 33

Transcript of 3-Data Manipulation Using SQL-Part1

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    1/33

    Introduction To SQL

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    2/33

    lSQL uses English keywords & user-defined names

    By convention, keywords are upper-caseText data is enclosed using single quotes ( ' )Round brackets (() are used to group related itemsCommas (,) separate items in a list Statements areterminated with a semicolon (;)

    CREATE TABLE Staff (

    StaffNo INTEGER,

    Salary FLOAT,

    Lname CHAR(20)

    );

    INSERT INTO Staff VALUES (32, 25000.0, 'Smith');

    SQL Syntax

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    3/33

    Some SQL Key words

    SELECT - extracts data from a database

    UPDATE - updates data in a database

    DELETE - deletes data from a database

    INSERT INTO - inserts new data into a database

    CREATE DATABASE - creates a new database

    ALTER DATABASE - modifies a database

    CREATE TABLE - creates a new table

    ALTER TABLE - modifies a table

    DROP TABLE - deletes a table

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    4/33

    Creating a Database

    SyntaxCREATE DATABASE database_name

    ExampleCREATE DATABASE my_db

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    5/33

    Creating a Table

    Syntax

    CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,column_name3 data_type,....)

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    6/33

    Create Table Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    7/33

    Some SQL Data Types

    Data Type Syntax ExplanationInteger int

    decimal decimal(p,s)Where p is a precisionvalue; s is a scale value

    float float(p)

    Where p is a precision

    value.

    Character char(x)

    Where x is the number ofcharacters to store. This datatype is space padded to fillthe number of charactersspecified.

    Character varying Varchar(x)Where x is the number ofcharacters to be stored

    date dateStores year, month, andday values.

    time timeStores the hour, minute,and second values.C

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    8/33

    SQL Constraints

    Constraints are used to limit the type of data that can go into atable.

    Constraints can be specified when a table is created (with the

    CREATE TABLE statement) or after the table is created (with theALTER TABLE statement).

    We will focus on the following constraints:NOT NULL

    UNIQUEPRIMARY KEYFOREIGN KEYCHECKDEFAULT

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    9/33

    SQL Constraints Primary Key

    Primary Keyis a set of attributes which identifies uniquely atuple (i.e., a row in a table) E.g., your NRIC or your email address The combination (name, city) in the branch table

    You cannot have two tuples with the same Primary Key in a table E.g., there cannot be two persons with the same NRIC. Therecannot be two branches with the same name in the same city

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    10/33

    SQL Constraints Primary Key etc

    Eg 1:CREATE TABLE branch( name varchar(10),

    city varchar(20),

    director varchar(20),assets number,PRIMARY KEY (name, city));

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    11/33

    SQL Constraints Primary Key etc

    Eg 2:CREATE TABLE Persons(P_Id int PRIMARY KEY,LastName varchar(255) NOT NULL,

    FirstName varchar(255),Address varchar(255),City varchar(255));

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    12/33

    SQL Constraints - UNIQUE

    The UNIQUE constraint uniquely identifies each record in a databasetable.

    The UNIQUE and PRIMARY KEY constraints both provide a guaranteefor uniqueness for a column or set of columns.

    A PRIMARY KEY constraint automatically has a UNIQUE constraintdefined on it.

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    13/33

    SQL Constraints UNIQUE etc

    The following SQL creates a UNIQUE constraint on the "P_Id"column when the "Persons" table is created:

    CREATE TABLE Persons(

    P_Id int PRIMARY KEY,NIC int,LastName varchar(255),FirstName varchar(255),Address varchar(255),

    City varchar(255),UNIQUE (NIC))

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    14/33

    SQL Constraints NOT NULL

    The NOT NULL constraint enforces a column to NOT accept NULLvalues

    The following SQL enforces the "P_Id" column and the "LastName"

    column to not accept NULL values:

    CREATE TABLE Persons(P_Id int PRIMARY KEY,LastName varchar(255) NOT NULL,

    FirstName varchar(255),Address varchar(255),City varchar(255));

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    15/33

    SQL Constraints - Check

    The CHECK constraint is used to limit the value range that can beplaced in a column.

    If you define a CHECK constraint on a single column it allows only

    certain values for this column.

    If you define a CHECK constraint on a table it can limit the valuesin certain columns based on values in other columns in the row.

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    16/33

    SQL Constraints Check

    The following SQL creates a CHECK constraint on the "P_Id"column when the "Persons" table is created. The CHECK constraintspecifies that the column "P_Id" must only include integersbetween 0 and 1000.

    CREATE TABLE Persons(P_Id int PRIMARY KEY,LastName varchar(255) NOT NULL,FirstName varchar(255),

    Address varchar(255),City varchar(255),CHECK (P_Id BETWEEN 0 AND 1000));

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    17/33

    SQL Constraints Foreign Key

    A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

    Example

    Foreign Key

    Primary Key

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    18/33

    SQL Constraints Foreign Key

    A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

    Example :

    CREATE TABLE Orders(O_Id int,OrderNo int NOT NULL,P_Id int,PRIMARY KEY (O_Id),

    FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    19/33

    SQL Select

    SyntaxSELECT column_name(s)FROM table_name

    ExampleSELECT LastName,FirstName FROM Persons

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    20/33

    SQL Select Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    21/33

    SELECT DISTINCT

    SQL SELECT DISTINCT Syntax

    SELECT DISTINCT column_name(s)

    FROM table_name

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    22/33

    SELECT DISTINCT - Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    23/33

    SQL WHERE Clause

    SQL WHERE Syntax

    SELECT column_name(s)

    FROM table_name

    WHERE column_name operator value

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    24/33

    SQL WHERE Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    25/33

    SQL AND OR Operators

    The AND operator displays a record if boththe first condition and the second conditionis true.

    The OR operator displays a record if eitherthe first condition or the second condition

    is true.

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    26/33

    AND Operator Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    27/33

    OR Operator Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    28/33

    SQL WHERE Example Cont...

    View the the names of the customers with respect to each order ?

    Example

    Foreign Key

    Primary Key

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    29/33

    SQL WHERE Example cont...

    Answer :

    SELECT o.OrderNo, p.FirstName, p.LastName

    From Orders o, Persons p

    Where o.P_ID=p.P_ID

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    30/33

    SQL Order By Clause

    ORDER By example

    SELECT column_name(s)

    FROM table_name

    ORDER BY column_name(s) ASC|DESC

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    31/33

    SQL Order By Example

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    32/33

    SQL Group By Clause

    SELECT column_name,aggregate_function(column_name)

    FROM table_name

    WHERE column_name operator value

    GROUP BY column_name

  • 8/2/2019 3-Data Manipulation Using SQL-Part1

    33/33

    SQL Group By Clause