Sie sind auf Seite 1von 11

Structured Query

Language
(SQL)

Introduction
SQL is an international standard for defining and manipulating

data sets from a


database. SQL provides a convenient method to create and manipulate databases.

SQL is simply a medium of communicating to the DBMS what the user wants it to do.
SQL is sometimes referred to as a non-procedural database language. It implies that the
SQL statements describe what data is to be retrieved, rather than specifying how to find
the data.

Introduction (Contd.)
Characteristics of SQL
Standard
independent
language

Cost effective

Speed

Cross-platform
Abilities

Characteristics
of SQL

Universality

Easy to learn and use

Less programming

Scaling

Getting Started With SQL


Data Types

DDL Commands
Data Definition Language or DDL consists of those commands in SQL that directly
create or delete database objects such as tables and indexes, specify links between
tables, and impose constraints between database tables.

CREATE TABLE Command: This command is used to define the structure of the table.

Syntax: CREATE TABLE table-name (


<column1> <data-type>,
<column2> <data-type>,
... ... ... ... ...
<columnN> <data-type>);
Example: CREATE TABLE EMPLOYEE(
Empid INTEGER,
Dept CHAR(10),
Empname CHAR(15),
Address CHAR(25),
Salary NUMBER);

DDL Commands (Contd.)


ALTER TABLE Command: This command allows a user to change the structure of an
existing table. New columns can be added with the ADD clause. Existing columns can be
modified with the MODIFY clause. Columns can be removed from a table by using the
DROP clause.
Syntax:

ALTER TABLE table-name


<ADD | MODIFY | DROP column(s)>;

Example: ALTER TABLE EMPLOYEE ADD Email CHAR(25);


ALTER TABLE EMPLOYEE MODIFY Empname CHAR(25);
ALTER TABLE EMPLOYEE DROP Dept;
DROP TABLE Command: This command removes the table definition (with all records).

Syntax:

DROP TABLE table-name;

Example: DROP TABLE EMPLOYEE;

DML Commands
Data Manipulation Language or DML consists of those commands, which
operate on the data in the database. These include statements that add data to
the table as well as those statements that are used to query the database.

INSERT Command: This command is used to add records in a table.


Syntax: INSERT INTO table-name (
column1, column2, ..., columnN)
VALUES (value1, value2, ..., valueN);
Example: INSERT INTO EMPLOYEE (

Empid, Dept, Empname, Address, Salary)


VALUES (101, RD01, Prince, Park Way, 15000);

DML Commands (Contd.)


UPDATE Command: This command is used to make changes to the data in the database.
Syntax:

UPDATE table-name
SET column1 = value1
[, column2 = value2]
... ... ... ... ...
[, columnN = valueN]

[WHERE condition];
Example 1:
Example 2:

Example 3:

UPDATE EMPLOYEE
SET Salary = Salary + 1000;
UPDATE EMPLOYEE
SET Salary = Salary + 1000
WHERE Dept = RD01;
UPDATE EMPLOYEE
SET Salary = Salary + 1000, Dept = RD01
WHERE Empname =Price;
8

DML Commands (Contd.)


DELETE Command : This command is used to delete all or selected records
from the specified table.
Syntax:

DELETE FROM table-name


[WHERE condition];

Example:

DELETE FROM EMPLOYEE

WHERE Salary > 8000;


The above statement deletes all the records from the EMPLOYEE table,
which satisfy the WHERE condition. That is, records for all the employees
whose Salary is more than 8000 will be deleted. Note that, if the WHERE
condition is not used then all the records from the specified table will be
deleted.

DML Commands (Contd.)


SELECT Command: This command allows users to retrieve data from one or more
tables with various conditions. The SELECT statement allows the user to specify the
desired data to be retrieved, the order to arrange the data, calculations to be performed on
the data set, and many such operations. The SELECT statement has a well-structured set
of clauses.
Syntax: SELECT column(s)
FROM table-name
[WHERE condition]
[ORDER BY column(s) [ASC|DESC]]:
Example: SELECT Empid, Dept, Empname, Address, Salary
FROM EMPLOYEE ORDER BY Empname;

10

DML Commands (Contd.)


Using Aggregate Functions
Aggregate functions are functions that take a collection of values as input and return a
single value as the output. These functions summarize the result of a query instead of
reporting each detail record.

Example 1: SELECT COUNT(*) FROM EMPLOYEE;


Example 2: SELECT AVG(Salary) AS Average_Salary FROM
EMPLOYEE;
11

Das könnte Ihnen auch gefallen