Sei sulla pagina 1di 33

Introduction To SQL

SQL Syntax
l

SQL uses English keywords & user-defined names


CREATE TABLE Staff ( StaffNo INTEGER, Salary FLOAT, Lname CHAR(20) ); INSERT INTO Staff VALUES (32, 25000.0, 'Smith');

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

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

Creating a Database
Syntax
CREATE DATABASE database_name

Example
CREATE DATABASE my_db

Creating a Table
Syntax
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )

Create Table Example

Some SQL Data Types


Data Type Integer decimal float Syntax
int

Explanation Where p is a precision value; s is a scale value Where p is a precision value.


Where x is the number of characters to store. This data type is space padded to fill the number of characters specified.

decimal(p,s) float(p)

Character Character varying date time

char(x) Varchar(x) date time

Where x is the number of characters to be stored Stores year, month, and day values. Stores the hour, minute, and second values.C

SQL Constraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). We will focus on the following constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT

SQL Constraints Primary Key


Primary Key is a set of attributes which identifies uniquely a tuple (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. There cannot be two branches with the same name in the same city

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));

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));

SQL Constraints - UNIQUE


The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

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))

SQL Constraints NOT NULL


The NOT NULL constraint enforces a column to NOT accept NULL values 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));

SQL Constraints - Check


The CHECK constraint is used to limit the value range that can be placed 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 values in certain columns based on values in other columns in the row.

SQL Constraints Check


The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers between 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));

SQL Constraints Foreign Key


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

Foreign Key

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) )

SQL Select
Syntax SELECT column_name(s) FROM table_name Example SELECT LastName,FirstName FROM Persons

SQL Select Example

SELECT DISTINCT

SQL SELECT DISTINCT Syntax


SELECT DISTINCT column_name(s) FROM table_name

SELECT DISTINCT - Example

SQL WHERE Clause

SQL WHERE Syntax


SELECT column_name(s) FROM table_name WHERE column_name operator value

SQL WHERE Example

SQL AND OR Operators

The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true.

AND Operator Example

OR Operator Example

SQL WHERE Example Cont...


View the the names of the customers with respect to each order ? Example Primary Key

Foreign Key

SQL WHERE Example cont...

Answer :
SELECT o.OrderNo, p.FirstName, p.LastName From Orders o, Persons p Where o.P_ID=p.P_ID

SQL Order By Clause

ORDER By example
SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC

SQL Order By Example

SQL Group By Clause

SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name

SQL Group By Clause

Potrebbero piacerti anche