0% found this document useful (0 votes)
4 views

Lab 04

Uploaded by

melaibaabbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 04

Uploaded by

melaibaabbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Agenda

1
1 SQL
SQL Introduction
Introduction

2
2 What
What is
is aa Table?
Table?

3
3 Select

4
Select Statement

4 DDL
DDL
Statement

SQL
5
5 DML
DML
SQL Introduction
SQL stands for Structured Query Language.

It is used to interact with the Database.

Itis the standard language for Relational


Database Management System.

Using SQL statements we can perform tasks


such as retrieving data from the data base,
updating the database etc.
What is a Table?
In a Relational database system, data is stored
in the form of one or more objects called
Tables.

A table in a database is a place where the data


or the information is stored.

These tables are uniquely identified by their


names.

The data in the tables is stored in the form of


rows and columns.
What is a Table? - Contd
 The following is a sample table Weather
called Weather. Here City, State,
High and Low are columns and
city state high low
the rows contain the data for
this table.
Phoenix Arizona 105 90

Tucson Arizona 101 92

Flagstaff Arizona 88 69

San Diego California 77 60

Albuquerque Mexico 80 72
Basic Select Statement
Select Statement

The SELECT statement is the only way of


querying and retrieving data from the
database.

The data will be selected according to


condition specified in the WHERE clause of
the statement.
Basic Select Statement - Contd

Syntax 1: Selecting all the columns from the table

SELECT *
FROM <Table Name>
WHERE <Condition>

 Here, * indicates all the columns in the given table.


 SELECT, FROM and WHERE are the keywords.
 In the place of Table Name, the name of the table
should be specified from which the data has to be
retrieved.
 In the WHERE clause, the condition will be
specified.
Basic Select Statement - Contd
Example:
SELECT *
FROM emp
WHERE DEPTNO = 20;

It will retrieve all the columns in the “emp”


table for the column “DEPTNO=20”.
Basic Select Statement - Contd

Syntax 2: Selecting only needed columns from a


table

SELECT Column 1, Column 2, … Column n


FROM <Table Name>
WHERE <Condition>

Here, the columns which need to be retrieved


will be specified after Select keyword.
Basic Select Statement - Contd
Example:
SELECT ename, job
FROM emp
WHERE deptno = 30;

It will retrieve the columns ename and job


from the table weather for the “deptno=30”.
The SQL Select Statement Basic Syntax

SELECT column-list
FROM table-names
WHERE condition(s)
A Basic SQL Select Statement
Performs 2 Types of Operations

Projection

SELECT column-list
FROM tables-names

WHERE condition(s)

Selection
Performing Projection

SELECT Course_Title, C_Hrs FROM course


COURSE Result Table
Course No. Course_Title C_Hrs. Dept. C Course_Title C_Hrs.
CIS 120 Intro to CIS 4 Cis Intro to CIS 4
MKT 333 Intro to Mkting 3 MKT
Intro to Mkting 3
ECO 473 Labor Econ. 3 ECO
Labor Econ. 3
BA201 Intro to Stat. 5 ECO
Intro to Stat. 5
CIS 345 Intro to Dbase 4 CIS
Intro to Dbase 4
Performing a Selection Operation

SELECT * FROM course WHERE C_Hrs = 4

Course No. Course Title C. Hrs. Dept. C


CIS 120 Intro to CIS 4 Cis
M KT 333 Intro to M kting 3 M KT COURSE
ECO 473 Labor Econ. 3 ECO
BA201 Intro to Stat. 5 ECO
CIS 345 Intro to Dbase 4 CIS

Course No. Course Title C. Hrs. Dept. C


CIS 120
CIS 345
Intro to CIS
Intro to Dbase
4 Cis
4 CIS Result Table
Performing both Projection and
Selection

SELECT Course_Title, C_Hrs FROM course


WHERE Dept_C = ‘CIS’

COURSE Result Table


Course_No Course_Title C_ Hrs. Dept_C Course_Title C_ Hrs.
CIS 120 Intro to CIS 4 CIS Intro to CIS 4
MKT 333 Intro to Mkting 3 MKT Intro to Dbase 4
ECO 473 Labor Econ. 3 ECO
BA201 Intro to Stat. 5 ECO
CIS 345 Intro to Dbase 4 CIS
Various SQL Statements
The following are the various SQL
Statements.

DDL
Data Definition Language
DML
Data Manipulation Language
DCL
Data Contol Language
DDL – Data Definition Language

DDL statements are used to create and modify the


structure of the tables.
It enables us to perform the following tasks.
Creating a Table (Using Create Statement)
Altering a Table (Using Alter Statement)
Dropping a Table (Using Drop Statement)
DDL – Data Definition Language - Contd

Create Statement:
Using Create statement, we can create a new
table in the database. The syntax of the Create
statement is as follows.
Syntax:
CREATE TABLE <Table Name>
(Column 1 data type,
Column 2 data type,
Column 3 data type …
Column n data type);
DDL – Data Definition Language - Contd
Here, CREATE and TABLE are keywords.

The table and column names must start with


a letter and can be followed by letters,
numbers, or underscores.

SQL reserved keywords should not be used


as names for tables and columns

Data type specifies what type of data that


particular column contains.
DDL – Data Definition Language - Contd
Here are the most common Data types:
char(size) Fixed-length character string. Size is specified in
parenthesis. Max 255 bytes.

varchar(size) Variable-length character string. Max size is specified


in parenthesis.

number(size) Number value with a max number of column digits


specified in parenthesis.

date Date value

number(size,d) Number value with a maximum number of digits of


"size" total, with a maximum number of "d" digits to
the right of the decimal
DDL – Data Definition Language - Contd
Example:

CREATE TABLE employee


(first varchar(20),
last varchar(20),
address varchar(30),
city varchar(20),
state varchar(20));

The above statement will create a table “employee”


with “first”, “last”, “address”, “city”, “state” as its
columns with the specified data type and the size.
DDL – Data Definition Language - Contd
Alter Statement:

Using alter statement we can do the following


alterations in our existing table.

Adding a new column

Renaming a column
DDL – Data Definition Language - Contd

Adding a new column:


The syntax for adding a new column into the table is
as follows.

Syntax:
ALTER TABLE <Table name>
ADD(column 1 data type, column 2 data type, …
column n data type);

 Here, ALTER, TABLE and ADD are keywords.


 The table name and the column name(s) along with their
data types to be added into that table should be specified.
DDL – Data Definition Language - Contd
Example:

ALTER TABLE employee


ADD(age number(3), dept number(5));

The columns “age” and “dept” with their


specified data types are added into the
“employee” table.
DDL – Data Definition Language - Contd
Example:

ALTER TABLE employee


DROP COLUMN age;
DDL – Data Definition Language - Contd

Renaming a column:
The syntax for renaming a column is as follows.

Syntax:
ALTER TABLE <Table name> RENAME COLUMN
<old name> TO <new name>;

 Here, ALTER, TABLE, RENAME, COLUMN and


TO are keywords.
 The name of the table in which the column needs to
be modified has to be specified. Also the existing
column name and the new name should be specified.
DDL – Data Definition Language - Contd
Example:

ALTER TABLE employee RENAME


COLUMN dept to deptno;

The column “dept” is renamed as “deptno”


in the “employee” table.
DDL – Data Definition Language - Contd

Drop Statement:

The drop statement is used to delete a table and


all the rows in the table.

Syntax:

DROP TABLE <Table name>


 Here, DROP and TABLE are keywords.
Table name which needs to be deleted has to be
specified
DDL – Data Definition Language - Contd

Example:

DROP TABLE weather;

The table definition as well as all the rows of


the table weather will be deleted
DML – Data Manipulation Language

The DML commands are used to insert and


modify the data in the database. It enables us to
perform the following tasks.

Insertinga row or the values of particular


columns into the table (Using Insert statement)

Updating values of columns in a table (Using


Update statement)

Deleting rows in a table (Using Delete


statement)
DML – Data Manipulation Language- Contd

Insert Statement:

The insert statement is used to insert or add


a row of data into the table. There are two
syntaxes available depending on the need of
the data to be inserted.
DML – Data Manipulation Language- Contd
Syntax 1: (To insert values for the specified
columns in a table)

INSERT INTO <Table Name> (column 1,


column 2, … column n)
VALUES (value1, value2, … value n);

Here,INSERT, INTO and VALUES are


keywords.

The values for the specified columns will be


inserted into the specified table name.
DML – Data Manipulation Language- Contd
Example:

INSERT INTO employee (first, last, deptno)


Values (Aarthy, Menon, 20);

A row will be inserted into the “employee”


table only with the specified columns.
DML – Data Manipulation Language- Contd
Syntax 2: (If all the column values are provided)

INSERT INTO <Table Name>


Values (value 1, value 2, … value n)

Here, no need of specifying the column names,


as values are inserted in all the columns.

The order of the given values should match the


order of the columns in the table.
DML – Data Manipulation Language- Contd

Example:

INSERT INTO employee


Values (Arul, Kumar, No.25 1st main,
Bangalore, Karnataka, 30, 40);

No column names are specified, as values


for all the columns in “employee” table are
specified.
DML – Data Manipulation Language- Contd

Update Statement:
The update statement is used to update or change
records that match specified criteria. This is
accomplished by using a WHERE clause.

Syntax:
UPDATE <Table Name>
SET column 1 = value 1, column 2 = value 2, …
column 3 = value 3
WHERE <condition>;
DML – Data Manipulation Language- Contd

Here,UPDATE, SET and WHERE are


keywords.

WHERE clause is to specify condition.


Based on the given condition, the rows will
be affected, if any.
DML – Data Manipulation Language- Contd

Example:

UPDATE employee
SET age = age + 1
WHERE last = kumar;

The age of the employees whose last name


is Kumar will be incremented by one.
DML – Data Manipulation Language- Contd
Delete Statement:
The delete statement is used to delete the records or
rows from the table.

Syntax:
DELETE FROM <Table Name>
WHERE <Condition>

 Here, DELETE, FROM and WHERE are keywords.


 The rows in the table will be deleted according to the
condition specified in the WHERE clause.
 If no conditions are specified, all the rows in the
table will be deleted.
DML – Data Manipulation Language- Contd

Example:

DELETE FROM employee


WHERE deptno = 10;

All the employee records, who work in


deptno 10 will be deleted from the employee
table.

You might also like