Lecture 06
Lecture 06
Sapna Kumari
DATABASE SYSTEMS- LECTURE 06 Lecturer, CS-SZABIST
Sapna.kumari@hyd.szabist.edu.pk
OUTLINE
• Introduction to Structured Query Language (SQL)
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Query Language (DQL)
• Data Control Language (DCL)
• Transaction Control Language (TCL)
INTRODUCTION TO SQL
• SQL was initially developed by IBM in the 1970s and became the standard language for relational
database management systems (RDBMS) in the 1980s. SQL is used by major database systems like
MySQL, PostgreSQL, Oracle, Microsoft SQL Server, and SQLite.
• SQL (Structured Query Language) is a standardized programming language used to manage and
manipulate relational databases. It is the most widely used language for interacting with relational
databases and allows users to perform a variety of tasks, such as querying, inserting, updating, and
deleting data.
INTRODUCTION TO SQL
Key Concepts in SQL
1.Database: A collection of data organized in a way that allows for efficient retrieval and manipulation. In
SQL, a database consists of tables, which are the core structures that store the data.
2.Table: A table is a collection of rows and columns. Each row represents a record, and each column
represents a field in the record. For example, an employees table might have columns like id, name, age, and
department.
3.Record: A row in a table. It contains all the information for a particular entity in that table. For example, a
record in the employees table might represent a particular employee's information.
4.Field/Column: A specific data category in a table. Columns have names (like id, name, age) and data types
(like INTEGER, VARCHAR, DATE).
5.Primary Key: A unique identifier for each record in a table. A primary key ensures that no two rows have
the same value for the key column(s).
6.Foreign Key: A column or set of columns in one table that refers to the primary key in another table.
Foreign keys are used to establish relationships between tables.
INTRODUCTION TO SQL
Rules:
•SQL is not case sensitive. Generally, keywords of SQL are written in uppercase.
•Statements of SQL are dependent on text lines.
•Using the SQL statements, you can perform most of the actions in a database.
•SQL depends on tuple relational calculus and relational algebra.
DOMAIN TYPES IN SQL
•char(n). Fixed length character string, with user-specified length n.
•varchar(n). Variable length character strings, with user-specified maximum length n.
•int. Integer (a finite subset of the integers that is machine-dependent).
•smallint. Small integer (a machine-dependent subset of the integer domain type).
•numeric(p,d). Fixed point number, with user-specified precision of p digits, with d digits to the right
of decimal point. (ex., numeric(3,1), allows 44.5 to be stores exactly, but not 444.5 or 0.32)
•float(n). Floating point number, with user-specified precision of at least n digits.
DDL COMMANDS
DATA DEFINITION LANGUAGE (DDL)
DDL is used to define or modify the structure of a database, including creating, altering, or
deleting tables and other database objects. It deals with the schema of the database.
Some common DDL commands are:
CREATE: Used to create a new database object (e.g., tables, views, etc.).
ALTER: Used to modify an existing database object (e.g., adding a column to a table).
DROP: Used to delete an existing database object.
TRUNCATE: Used to remove all records from a table (without deleting the table itself).
RENAME: Used to rename an existing database object.
CREATE COMMAND (DDL)
SQL CREATE TABLE statement is used to create table in a database.
If you want to create a table, you should name the table and define its column and each column's data
type.
Let's see the simple syntax to create the table.
CREATE TABLE instructor (
ID char(5) Primary Key,
name varchar(20) NOT NULL,
dept_name varchar(20) NOT NULL,
salary numeric(8,2) NOT NULL,
)
ALTER COMMAND (DDL)
ALTER Command:
ALTER is a DDL command which changes or modifies the existing structure of the database, and it also
changes the schema of database objects
Example:
RENAME TABLE Student TO Student_Details ;
DROP COMMAND (DDL)
DROP Command:
DROP is a DDL command used to delete/remove the database objects from the SQL database. We can
easily remove the entire table, view, or index from the database using this DDL command.
DROP DATABASE Database_Name;
DROP TABLE Table_Name
Example:
DROP DATABASE Books;
DROP TABLE Employee;
DIFFERENCE BETWEEN TRUNCATE AND DELETE
DML COMMANDS
DATA MANIPULATION LANGUAGE (DML)
DML is used for managing and manipulating the data stored within database objects
(like tables). DML commands are responsible for inserting, updating, deleting, and
querying data.
Some common DML commands are:
INSERT: Used to insert new data into a table.
UPDATE: Used to modify existing data in a table.
DELETE: Used to remove data from a table.
DATA MANIPULATION LANGUAGE (DML)
INSERT COMMAND:
- INSERT is used to add a single tuple/row to a table.
- Specify the table name and a list of values for the row.
- Values should be listed in the same order in which the corresponding attributes
were specified in the CREATE TABLE command.
-Also possible to insert into a relation multiple tuples separated by commas in a
single INSERT command.
Syntax:
INSERT INTO tablename VALUES ( ‘Value1’, ‘Value2’ , ‘Value3’, ‘Value4’,
‘Value5’);
DATA MANIPULATION LANGUAGE (DML)
INSERT COMMAND:
Example:1
DATA MANIPULATION LANGUAGE (DML)
DELETE COMMAND:
• The DELETE command removes tuples from a table .
• Includes a WHERE clause, similar to that used in an SQL query, to select the
rows to be deleted.
• Rows are explicitly deleted from only one table at a time.
• Several Rows can be deleted by a single DELETE command.
Syntax:
DELETE FROM Tablename ;
DELETE FROM EMPLOYEE WHERE Lname=‘Brown’;
DATA MANIPULATION LANGUAGE (DML)
Examples:
1) DELETE FROM Employee;
2) DELETE FROM Employee WHERE name= ‘Clark’ ;
3) DELETE FROM Employee WHERE name= ‘Dave’ AND Dept =
‘Accounting’ ;
DATA MANIPULATION LANGUAGE (DML)
Update Command:
The update command is part of DML (Data Manipulation Language), which
is used to modify the data inside tables.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DATA MANIPULATION LANGUAGE (DML)
Update Command:
UPDATE users
SET email = 'newemail@example.com'
WHERE user_id = 1;