0% found this document useful (0 votes)
20 views21 pages

Structural Query Language (SQL)

Uploaded by

ziyadhussein631
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views21 pages

Structural Query Language (SQL)

Uploaded by

ziyadhussein631
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

CHAPTER 2

Structural Query Language(SQL)


SQL (Structured Query Language) 2

• Structured query language (SQL) is a programming language for


storing and processing information in a relational database.
• It is used to perform operations on the records stored in the
database such as updating records, deleting records, creating and
modifying tables, views, etc.
• It is designed for managing data in a relational database
management system (RDBMS).
• SQL is a database language, it is used for database creation,
deletion, fetching rows, and modifying rows, etc.
• SQL is based on relational algebra and tuple relational calculus
What are SQL commands? 3

Structured query language (SQL) commands are specific keywords or SQL


statements that developers use to manipulate the data stored in a relational
database.

Data definition language

Data manipulation language

Data query language

Data control language

Transaction control language
Data definition language(DDL) 4

• Data definition language (DDL) refers to SQL commands that design


the database structure.
• Database engineers use DDL to create and modify database objects
based on the business requirements.
• For example, the database engineer uses the CREATE command to
create database objects such as tables, views, and indexes.
Includes data definition language (DDL), statements that specify and
modify database schemas.
(DDL) is a language for defining, or declaring, database objects.
SQL COMMAND IN DDL 5

• DDL statements are used to define server and database objects,


like logins, users, tables, views, indexes, and so forth.
CREATE DATABASE
CRAETE TABLE
ALTER TABLE
DROP TABLE
DDL Command 6

1. CREATE DATABASE:
• The CREATE DATABASE statement is used to create a new SQL
database.
• Syntax:
CREATE DATABASE <databasename>;
2. CRAETE TABLE:
The CREATE TABLE statement is used to create a new table in a database.
Syntax:
CREATE TABLE table_name ( column1 datatype, column2 datatype,
column3 datatype);
DDL Command… 7

3. ALTER TABLE
• The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
• The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
Syntax:
ALTER TABLE table_name
ADD column_name datatype;

ALTER TABLE Customers


ADD Email varchar(255);
DDL Command… 8

4. DROP TABLE
• The DROP DATABASE statement is used to drop(delete) an existing
SQL database.
• DROP DATABASE databasename;
Data Manipulation Language (DML) 9

• Data manipulation language (DML) statements write new


information or modify existing records in a relational database.
• For example, an application uses the INSERT command to store a
new record in the database.
Includes a data manipulation language (DML), statements that
manipulate database content.
(DML) is a language for "manipulating" or processing database objects.
SQL COMMAND IN DML 10

• DML statements are, like the name implies, statements used to


modify data, including statements used to insert new data or
delete data rows.
INSERT INTO
SELECT
UPDATE
DELETE
SQL SYNTAX 11

• SQL is not case sensitive.


• SQL statements are started with any of the SQL commands/keywords
like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP etc. and the
statement ends with a semicolon (;).
CREATE DATABASE:
CREATE DATABASE <database_name>
Example
CREATE DATABASE Mettu
Use Mettu
SYNTAX FOR CREATE TABLE 12

• CREATE TABLE <table_name>(column1 data type constraint, column2 data type


constraint,…);

Example
CREATE TABLE student(ID int NOT NULL PRIMARY KEY, NAME
VARCHAR(10),SEX VARCHAR(4) );

ID NAME SEX
INSERT INTO 13

• Syntax for INSERT INTO


INSERT INTO <table_name>(column1,column2…n)
VALUES(value1,value2…n)
Example
INSERT INTO student(ID,NAME,SEX) VALUES (1, ‘Ababe’, ‘Male’)
NB: String(character) value always enclosed with single quotation.
SELECT 14

• Syntax for SELECT


To display all contents of the table
SELECT * FROM <table_name>
Example
SELECT * FROM student
Display by using Where condition
1. SELECT * FROM student WHERE <column=condition>
Example
SELECT * FROM student WHERE ID=2
SELECT 15

Display only single Value


SELECT <column> FROM <table_name> WHERE column=condition
Example
SELECT NAME FROM student WHERE ID=3
UPDATE 16

Syntax for update


UPDATE <table_name> SET column=value WHERE column=condition
Example
UPDATE student SET name=“Tola” WHERE ID=2
NB: If you didn’t use “WHERE” all column automatically updated
DELETE 17

• Syntax for delete


DELETE FROM <table_name> WHERE column=condition
Example
DELETE FROM student WHERE ID=3
NB: If you didn’t use “WHERE” all rows automatically deleted
ALTRE TABLE

1. Adding a new column to a table


ALTER TABLE <table_name> ADD column_name DataType
Example:
ALTER TABLE student ADD sex varchar(10)
2. Changing column data type
ALTER TABLE <table_name>
ALTER COLUMN column_name new_DataType
Example
ALTER TABLE student ALTER COLUMN sex int
ALTRE TABLE(CON’T…)

3. changing column length in a table


ALTER TABLE <table_name>
ALTER COLUMN column_name New_reduced_DatsType
Example
ALTER TABLE student
ALTER COLUMN name VARCHAR(10)
4. deleting columns from a table
ALTER TABLE table_name DROP COLUMN column_name
Example
ALTER TABLE student DROP COLUMN name
DROP

• Drop command is used to delete Table from database


• Syntax for Drop table is
DROP TABLE <table_name>
Example
DROP TABLE student
21

You might also like