INTRODUCTION TO SQL
1
SQL is Structured Query Language, it is a language
that enables us to create and operate on relational
databases, which are sets of related information
stored in tables. SQL is a standard language for
storing, manipulating and retrieving data in
databases.
Chapter 6 Copyright © 2014 Pearson Education, Inc.
SQL OVERVIEW
 Structured Query Language
 1970–E. F. Codd develops relational
database concept
 The standard for relational database
management systems (RDBMS)
 RDBMS: A database management system
that manages data as a collection of tables in
which all relationships are represented by
common values in related tables
2
3
SQL commands are mainly categorized
into four categories as:
4
DDL(Data Definition Language)
DDL or Data Definition Language actually consists of the SQL commands that
can be used to define the database schema. It simply deals with
descriptions of the database schema and is used to create and modify the
structure of database objects in database.
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
5
SQL DATA TYPES
6
SQL DATA TYPES
7
The SQL CREATE TABLE Statement
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
8
SQL Constraints:
9
SQL NOT NULL Constraint
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values
The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values when the "Persons" table is created:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25) NOT NULL,
Age int
);
10
SQL UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are
different.
The following SQL creates a UNIQUE constraint on the "ID" column
when the "Persons" table is created:
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Age int
);
11
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can
consist of single or multiple columns
CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Age int
);
12
SQL FOREIGN KEY Constraint
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
13
SQL CHECK Constraint
The CHECK constraint is used to limit the value range that can be
placed in a column.
The following SQL creates a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint ensures that you can not have
any person below 18 years:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Age int CHECK (Age>=18)
);
14
SQL DEFAULT Constraint
The DEFAULT constraint is used to provide a default value for a column.
The default value will be added to all new records IF no other value is
specified.
The following SQL sets a DEFAULT value for the "City" column when
the "Persons" table is created:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Age int,
City varchar(255) DEFAULT ‘Delhi'
);

introdution concept on _ _ sql_basic.ppt

  • 1.
    INTRODUCTION TO SQL 1 SQLis Structured Query Language, it is a language that enables us to create and operate on relational databases, which are sets of related information stored in tables. SQL is a standard language for storing, manipulating and retrieving data in databases.
  • 2.
    Chapter 6 Copyright© 2014 Pearson Education, Inc. SQL OVERVIEW  Structured Query Language  1970–E. F. Codd develops relational database concept  The standard for relational database management systems (RDBMS)  RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables 2
  • 3.
    3 SQL commands aremainly categorized into four categories as:
  • 4.
    4 DDL(Data Definition Language) DDLor Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in database. CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table
  • 5.
  • 6.
  • 7.
    7 The SQL CREATETABLE Statement Syntax CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
  • 8.
  • 9.
    9 SQL NOT NULLConstraint By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values when the "Persons" table is created: CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(25) NOT NULL, FirstName varchar(25) NOT NULL, Age int );
  • 10.
    10 SQL UNIQUE Constraint TheUNIQUE constraint ensures that all values in a column are different. The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created: CREATE TABLE Persons ( ID int NOT NULL UNIQUE, LastName varchar(25) NOT NULL, FirstName varchar(25), Age int );
  • 11.
    11 SQL PRIMARY KEYConstraint The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, LastName varchar(25) NOT NULL, FirstName varchar(25), Age int );
  • 12.
    12 SQL FOREIGN KEYConstraint A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. CREATE TABLE Orders ( OrderID int NOT NULL PRIMARY KEY, OrderNumber int NOT NULL, PersonID int FOREIGN KEY REFERENCES Persons(PersonID) );
  • 13.
    13 SQL CHECK Constraint TheCHECK constraint is used to limit the value range that can be placed in a column. The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that you can not have any person below 18 years: CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(25) NOT NULL, FirstName varchar(25), Age int CHECK (Age>=18) );
  • 14.
    14 SQL DEFAULT Constraint TheDEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified. The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created: CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(25) NOT NULL, FirstName varchar(25), Age int, City varchar(255) DEFAULT ‘Delhi' );