Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views5 pages

Chapter 7 SQL Practical

This document provides an overview of SQL (Structured Query Language), detailing its components such as Data Manipulation Language (DML) and Data Definition Language (DDL). It includes examples of SQL statements for creating, updating, deleting, and querying databases and tables, along with explanations of various operators and clauses. Key SQL commands such as SELECT, INSERT, UPDATE, DELETE, and their syntax are also discussed.

Uploaded by

felmiketfikadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Chapter 7 SQL Practical

This document provides an overview of SQL (Structured Query Language), detailing its components such as Data Manipulation Language (DML) and Data Definition Language (DDL). It includes examples of SQL statements for creating, updating, deleting, and querying databases and tables, along with explanations of various operators and clauses. Key SQL commands such as SELECT, INSERT, UPDATE, DELETE, and their syntax are also discussed.

Uploaded by

felmiketfikadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Note: SQL is not case sensitive!!!!

Chapter – 7

SQL (Structured Query Language)

Example: - COMPANY DATABASE

SQL Statements:-

SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data
Definition Language (DDL).

 The query and update commands form the DML part of SQL
 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
 The most important DDL statements in SQL are:

 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
 CREATE INDEX - creates an index (search key)

How to create a database using SQL?????

SQL create database syntax

CREATE DATABASE database_name


E.g. create database COMPANY

SQL create table syntax

CREATE TABLE table_name (column_name1 data_type, column_name2data_type,


column_name3 data_type,)

E.g. create table EMPLOYEE (Fname varchar(50),Mname varchar(50),Lname


varchar(50),Ssn int NOT NULL UNIQUE PRIMARY KEY, UNIQUE,Bdate
year,Address varchar (75), Sex varchar(6), salary int, Super_Ssn int NOT NULL
FOREIGN KEY REFERENCES EMPLOYEE(Ssn),Dno int)

 To create a UNIQUE constraint on the "Ssn" column when the table is already
created, use the following SQL:
 ALTER TABLE EMPLOYEE ADD UNIQUE PRIMARY KEY (Ssn)
 To DROP a UNIQUE Constraint

1|Page
Note: SQL is not case sensitive!!!!

 ALTER TABLE EMPLOYEE DROP CONSTRAINT uc_Ssn


 ALTER TABLE EMPLOYEE DROP CONSTRAINT pk_Ssn

To DROP a FOREIGN KEY Constraint


 ALTER TABLE EMPLOYEE DROP CONSTRAINT fk_Super_Ssn
An index can be created in a table to find data more quickly and efficiently.
 SQL CREATE INDEX Syntax
CREATE INDEX index_name ON table_name (column_name)
E.g. CREATE INDEX EmpIndex ON EMPLOYEE (Lname)
 DROP INDEX Syntax for MS SQL Server:
DROP INDEX table_name.index_name
 The DROP TABLE statement is used to delete a table.
DROP TABLE table_name
E.g. DROP TABLE Employee
 The DROP DATABASE Statement
 DROP DATABASE database_name
E.g. DROP DATABASE COMPANY
 The TRUNCATE TABLE Statement (What if we only want to
delete the data inside the table, and not the table itself?)

Syntax: TRUNCATE TABLE table_name


SQL ALTER TABLE Syntax (used to add, delete, or modify
columns in an existing table.)
 To add a column in a table, use the following syntax:
ALTER TABLE table_name ADD column_name datatype
 To delete a column in a table
ALTER TABLE table_name DROP COLUMN column_name
 To change the data type of a column in a table
LTER TABLE table_name LTER COLUMN column_name datatype

The following SQL statement will select all the records in the “Employee” table:

Syntax: SELECT * FROM table_name


SELECT column_name(s) FROM table_name

E.g. SELECT * FROM Employee

 The DISTINCT keyword can be used to return only distinct


(different) values.

SELECT DISTINCT column_name(s) FROM table_name

2|Page
Note: SQL is not case sensitive!!!!

E.g. SELECT DISTINCT salary FROM Employee

The WHERE Clause

 Is used to extract only those records that fulfill a specified criterion.


Syntax:

SELECT column_name(s) FROM table_name WHERE column_name operator value


E.g. SELECT * FROM Employee WHERE salary=1000

For text value

This is correct:
SELECT * FROM Employee WHERE Fname='Abebe'
This is wrong:
SELECT * FROM Employee WHERE Fname =Abebe
For numeric values:

This is correct:
SELECT * FROM Employee WHERE salary =1965
This is wrong:
SELECT * FROM Employee WHERE salary ='1965'
Operators Allowed in the WHERE Clause

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to
return for at least one of the columns
The 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.
E.g. SELECT * FROM Employee WHERE Fname='Abebe' AND Mname='Kebede'
SELECT * FROM Employee WHERE Fname ='Abebe' OR Fname ='Lemma'
Combining AND & OR
E.g. SELECT * FROM Employee WHERE Mname ='Kebede'AND (Fname ='Abebe'
OR Fname ='Lemma')

The ORDER BY Keyword

3|Page
Note: SQL is not case sensitive!!!!

 Is used to sort the result-set by a specified column.


Syntax:

SELECT column_name(s) FROM table_name ORDER BY


column_name(s) ASC|DESC

E.g. SELECT * FROM Employee ORDER BY Fname

The INSERT INTO Statement

 Is used to insert a new row in a table.


Syntax:

INSERT INTO table_name VALUES (value1, value2, value3...)


Or

INSERT INTO table_name (column1, column2, column3...) VALUES


(value1, value2, value3...)

E.g. INSERT INTO Employee VALUES (‘Abebe’,'Kebede', 'Lemma', 1234567,


12/03/1988,’A.A’,’Male’, 6000, 1234567, 6)

Insert Data Only in Specified Columns

E.g. INSERT INTO Employee (Ssn, Fname, Mname) VALUES (534567, 'Helen', 'Asefa')

The UPDATE Statement

 Is used to update existing records in a table.


Syntax:

UPDATE table_name SET column1=value, column2=value2,... WHERE


some_column=some_value

E.g. UPDATE Employee SET Lname=’Tassew’, Salary=3000 WHERE Ssn=534567 AND


Fname='Helen'

SQL UPDATE Warning

 Be careful when updating records. If we had omitted the WHERE clause, all records
will be updated.

The DELETE Statement

 Is used to delete rows in a table.


Syntax:

DELETE FROM table_name WHERE some_column=some_value

4|Page
Note: SQL is not case sensitive!!!!

E.g. DELETE FROM Employee WHERE Lname='Lemma' AND Fname=’Abebe’

Delete All Rows

DELETE FROM table_name or DELETE * FROM table_name


Note: Be very careful when deleting records. You cannot undo this statement!

The LIKE Operator

 Is used to search for a specified pattern in a column.


SQL LIKE Syntax

SELECT column_name(s) FROM table_name WHERE column_name LIKE


pattern
E.g. SELECT * FROM Employee WHERE Fname LIKE 'S%'
SQL Wildcards
 SQL wildcards can substitute for one or more characters when searching for data in a
database.
 SQL wildcards must be used with the SQL LIKE operator.

Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist] or [!charlist] Any single character not in charlist
E.g. SELECT * FROM Employee WHERE Fname LIKE 'A%'
E.g. SELECT * FROM Employee WHERE Fname LIKE '_be' {Now we want to
select the persons with a first name that starts with any character, followed by
"be" from the " Employee " table. }
E.g. SELECT * FROM Employee WHERE Fname LIKE '[bhl]%' { Now we want to
select the persons with a Fname that starts with "b" or "h" or "l" from the
" Employee " table. }

The IN Operator

 The IN operator allows you to specify multiple values in a WHERE clause.


Syntax:

SELECT column_name(s) FROM table_name WHERE column_name IN


(value1, value2,...)
E.g. SELECT * FROM Employee WHERE Fname IN ('Abebe','Helen')

BETWEEN Operator Example

Now we want to select the persons with a last name alphabetically between "Hansen"
an"Pettersen" from the table above.
E.g. SELECT * FROM Employee WHERE Fname BETWEEN 'Abebe' AND 'Helen'

5|Page

You might also like