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

DBMS

Uploaded by

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

DBMS

Uploaded by

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

What is DBMS?

A DBMS is a software that allows creation, definition and manipulation of database, allowing users to store,
process and analyse data easily. DBMS provides us with an interface or a tool, to perform various operations
like creating database, storing data in it, updating data, creating tables in the database and a lot more.

DBMS also provides protection and security to the databases. It also maintains data consistency in case of
multiple users.

Here are some examples of popular DBMS used these days:

 MySql
 Oracle
 SQL Server
 IBM DB2
 PostgreSQL
 Amazon SimpleDB (cloud based) etc.

Characteristics of Database Management System
A database management system has following characteristics:

1. Data stored into Tables: Data is never directly stored into the database. Data is stored into tables,
created inside the database. DBMS also allows to have relationships between tables which makes the
data more meaningful and connected. You can easily understand what type of data is stored where by
looking at all the tables created in a database.

2. Reduced Redundancy: In the modern world hard drives are very cheap, but earlier when hard drives
were too expensive, unnecessary repetition of data in database was a big problem. But DBMS
follows Normalisation which divides the data in such a way that repetition is minimum.

3. Data Consistency: On Live data, i.e. data that is being continuosly updated and added, maintaining the
consistency of data can become a challenge. But DBMS handles it all by itself.

4. Support Multiple user and Concurrent Access: DBMS allows multiple users to work on it(update,
insert, delete data) at the same time and still manages to maintain the data consistency.

5. Query Language: DBMS provides users with a simple Query language, using which data can be easily
fetched, inserted, deleted and updated in a database.

6. Security: The DBMS also takes care of the security of data, protecting the data from un-authorised
access. In a typical DBMS, we can create user accounts with different access permissions, using which
we can easily secure our data by restricting user access.

7. DBMS supports transactions, which allows us to better handle and manage data integrity in real world
applications where multi-threading is extensively used.
Advantages of DBMS

 Segregation of applicaion program.


 Minimal data duplicacy or data redundancy.
 Easy retrieval of data using the Query Language.
 Reduced development time and maintainance need.
 With Cloud Datacenters, we now have Database Management Systems capable of storing almost infinite
data.
 Seamless integration into the application programming languages which makes it very easier to add a
database to almost any application or website.

Disadvantages of DBMS

 It's Complexity
 Except MySQL, which is open source, licensed DBMSs are generally costly.
 They are large in size.
Components of DBMS
The database management system can be divided into five major components, they are:

1. Hardware
2. Software
3. Data
4. Procedures
5. Database Access Language
Let's have a simple diagram to see how they all fit together to form a database management system.
DBMS Components: Hardware

When we say Hardware, we mean computer, hard disks, I/O channels for data, and any other physical
component involved before any data is successfully stored into the memory.

When we run Oracle or MySQL on our personal computer, then our computer's Hard Disk, our Keyboard using
which we type in all the commands, our computer's RAM, ROM all become a part of the DBMS hardware.

DBMS Components: Software

This is the main component, as this is the program which controls everything. The DBMS software is more like
a wrapper around the physical database, which provides us with an easy-to-use interface to store, access and
update data.

The DBMS software is capable of understanding the Database Access Language and intrepret it into actual
database commands to execute them on the DB.

DBMS Components: Data

Data is that resource, for which DBMS was designed. The motive behind the creation of DBMS was to store
and utilise data.

In a typical Database, the user saved Data is present and meta data is stored.
Metadata is data about the data. This is information stored by the DBMS to better understand the data stored in
it.

For example: When I store my Name in a database, the DBMS will store when the name was stored in the
database, what is the size of the name, is it stored as related data to some other data, or is it independent, all this
information is metadata.

DBMS Components: Procedures

Procedures refer to general instructions to use a database management system. This includes procedures to setup
and install a DBMS, To login and logout of DBMS software, to manage databases, to take backups, generating
reports etc.

DBMS Components: Database Access Language

Database Access Language is a simple language designed to write commands to access, insert, update and
delete data stored in any database.

A user can write commands in the Database Access Language and submit it to the DBMS for execution, which
is then translated and executed by the DBMS.

User can create new databases, tables, insert data, fetch stored data, update data and delete the data using the
access language.

Users
 Database Administrators: Database Administrator or DBA is the one who manages the complete
database management system. DBA takes care of the security of the DBMS, it's availability, managing
the license keys, managing user accounts and access etc.

 Application Programmer or Software Developer: This user group is involved in developing and
desiging the parts of DBMS.

 End User: These days all the modern applications, web or mobile, store user data. How do you think
they do it? Yes, applications are programmed in such a way that they collect user data and store the data
on DBMS systems running on their server. End users are the one who store, retrieve, update and delete
data.

Types of Database Model


A Database model defines the logical design and structure of a database. It defines how data will be stored,
accessed, and updated in a database management system.

 The database model sets the rule, relationships, constraints, etc. to define how data is stored in the
database.
 It's like creating a blueprint of your Database.
 There are different types of Database models and each one has its own set of features.
 You can define how you want to structure the application data using a database model.

In this tutorial you will learn about the 7 database model that are popularly used.
Type of Database models

There are several different Database model types, some of them are old, while some of them are new, to cater to
the new age requirements. Here is a list of the 7 popular Database models:

1. Hierarchical Model
2. Network Model
3. Entity-relationship Model
4. Relational Model
5. Object-oriented Model

1. Hierarchical Model

 The hierarchical database model organizes data into a tree-like structure,, with a single root, to which
all the other data is linked.
 The hierarchy starts from the Root data, and expands like a tree, adding child nodes to
the parent nodes.
 In this model, a child node will only have a single parent node.
 This model efficiently describes many real
real-world relationships like the index of a book,
book etc.
 IBM's Information Management System (IMS) is based on this model.
 Data is organized into a tree-like
like structure with a one-to-many relationship between two different types
of data, for example, one department can have many courses, many teachers,, and of course
many students(like
(like shown in the diagram below).

Advantages/Disadvantages of the Hierarchical Model

Here are a few points to mark the advantages


vantages and disadvantages of the Hierarchical database model:

1. Because it has one-to-many


many relationships between different types of data so it is easier and fast to fetch
the data.
2. But the Hierarchical model is less flexible.
3. And it doesn't support many-to-many
many relationships.

2. Network Model

 The Network Model is an extension of the Hierarchical model.


 In this model, data is organized more like a graph, and allowed to have more than one parent node.
node
 In the network database model, data is more related as more relationships are established in this
database model.
 Also, as the data is more related, hence accessing the data is also easier and fast.
fast
 This database model uses many-to to-many data relationships.
 Integrated Data Store (IDS) is based on this database mode
model.
 This was the most widely used database model before Relational Model was introduced.
 The implementation of the Network model is complex, and it's very difficult to maintain it.
 The Network model is difficult to modify also.
 You may want to explore this if you are developing some social networking applications, although the
Graph Database model is new and is far better than the Network Database model.

Advantages of the Network Model

1. It supports complex relationships


2. It allows more flexibility

3. Entity-relationship Model

 In this database model, relationships are created by dividing objects of interest into entities and their
characteristics into attributes.
 Different entities are related using relationships.
 ER Models are defined to represent the relationships in pictorial form to make it easier for different
stakeholders to understand.
 This model is good to design a database, which can then be turned into tables in a relational model
(explained below).
 Let's take an example,, If we have to design a School Database, then the Student will be
an entity with attributes name, ageage, address, etc. As an Address is generally complex, it can be
another entity with attributes street
street, pincode, city, etc, and there will be a relationship between them.
 Relationships can also be of different types. You can learn about ER Diagrams in detail if you want to
learn about entities and relationships.
Advantages of the ER Model

1. It is easy
sy to understand and design.
2. Using the ER model we can represent data structures easily.
3. As the ER model cannot be directly implemented into a database model, it is just a step toward
designing the relational database model.

4. Relational Model

 In this model, data is organized in two


two-dimensional tables and the relationship is maintained by storing
a common field.
 This model was introduced by E.F Codd in 1970, and since then it has been the most widely used
database model.
 The basic structure of data in the
he relational model is tables.. All the information related to a particular
type is stored in rows of that table.
 Hence, tables are also known as relations in the relational model.
 You can design tables, normalize them to reduce data redundancy, and use Structured Query language or
SQL to access data from the tables.
 Some of the most popular databases are based on this database model. For example, Oracle, MySQL,
etc.

Advantages of the Relational Model

1. It's simple and easy to implement.


2. Poplar database software is available for this database model.

3. It supports SQL using which you can easily query the data.

A Relational Database management System


System(RDBMS)
(RDBMS) is a database management system based on the
relational model introduced by E.F Codd. In relational model, data is stored in relations(tables)
relations and is
represented in form of tuples(rows).

RDBMS is used to manage Relational database. Relational database is a collection of organized set of tables
related to each other, and from which data can be accessed easily. Relational Database is the most commonly
used database these days.
RDBMS: What is Table ?

In Relational database model, a table is a collection of data elements organised in terms of rows and columns.
A table is also considered as a convenient representation of relations. But a table can have duplicate row of data
while a true relation cannot have duplicate data. Table is the most simplest form of data storage. Below is an
example of an Employee table.

ID Name Age Salary

1 Adam 34 13000

2 Alex 28 15000

3 Stuart 20 18000

4 Ross 42 19020

RDBMS: What is a Tuple?

A single entry in a table is called a Tuple or Record or Row. A tuple in a table represents a set of related data.
For example, the above Employee table has 4 tuples/records/rows.

1 Adam 34 13000

Following is an example of single record or tuple.

RDBMS: What is an Attribute?

A table consists of several records(row), each record can be broken down into several smaller parts of data
known as Attributes. The above Employee table consist of four attributes, ID, Name, Age and Salary.

When an attribute is defined in a relation(table), it is defined to hold only a certain type of values, which is
known as Attribute Domain.
Hence, the attribute Name will hold the name of employee for every tuple. If we save employee's address there,
it will be violation of the Relational database model.

Name

Adam

Alex

Stuart - 9/401, OC Street, Amsterdam

Ross

What is a Relation Schema?

A relation schema describes the structure of the relation, with the name of the relation(name of table), its
attributes and their names and type.

What is a Relation Key?

A relation key is an attribute which can uniquely identify a particular tuple(row) in a relation(table).

Relational Integrity Constraints

Every relation in a relational database model should abide by or follow a few constraints to be a valid relation,
these constraints are called as Relational Integrity Constraints.

The three main Integrity Constraints are:

1. Key Constraints

2. Domain Constraints

3. Referential integrity Constraints


Key Constraints
We store data in tables, to later access it whenever required. In every table one or more than one attributes
together are used to fetch data from tables. The Key Constraint specifies that there should be such an
attribute(column) in a relation(table), which can be used to fetch data for any tuple(row).

The Key attribute should never be NULL or same for two different row of data.

For example, in the Employee table we can use the attribute ID to fetch data for each of the employee. No value
of ID is null and it is unique for every row, hence it can be our Key attribute.

Domain Constraint
Domain constraints refers to the rules defined for the values that can be stored for a certain attribute.

Like we explained above, we cannot store Address of employee in the column for Name.

Similarly, a mobile number cannot exceed 10 digits.

Referential Integrity Constraint


We will study about this in detail later. For now remember this example, if I say Supriya is my girlfriend, then
a girl with name Supriya should also exist for that relationship to be present.

If a table reference to some data from another table, then that table and that data should be present for
referential integrity constraint to hold true.

Introduction to Database Keys


Keys are very important part of Relational database model. They are used to establish and identify relationships
between tables and also to uniquely identify any record or row of data inside a table.

A Key can be a single attribute or a group of attributes, where the combination may act as a key.

Why we need a Key?

In real world applications, number of tables required for storing the data is huge, and the different tables are
related to each other as well.

Also, tables store a lot of data in them. Tables generally extends to thousands of records stored in them,
unsorted and unorganised.

Now to fetch any particular record from such dataset, you will have to apply some conditions, but what if there
is duplicate data present and every time you try to fetch some data by applying certain condition, you get the
wrong data. How many trials before you get the right data?

To avoid all this, Keys are defined to easily identify any row of data in a table.

Let's try to understand about all the keys using a simple example.
student_id name phone age

1 Akon 9876723452 17

2 Akon 9991165674 19

3 Bkon 7898756543 18

4 Ckon 8987867898 19

5 Dkon 9990080080 17

Super Key

Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table.
Super Key is a superset of Candidate key.

In the table defined above super key would include student_id, (student_id, name), phone etc.

Confused? The first one is pretty simple as student_id is unique for every row of data, hence it can be used to
identity each row uniquely.

Next comes, (student_id, name), now name of two students can be same, but their student_id can't be same
hence this combination can also be a key.

Similarly, phone number for every student will be unique, hence again, phone can also be a key.

So they all are super keys.

Candidate Key

Candidate keys are defined as the minimal set of fields which can uniquely identify each record in a table. It is
an attribute or a set of attributes that can act as a Primary Key for a table to uniquely identify each record in that
table. There can be more than one candidate key.

In our example, student_id and phone both are candidate keys for table Student.
 A candiate key can never be NULL or empty. And its value should be unique.

 There can be more than one candidate keys for a table.

 A candidate key can be a combination of more than one columns(attributes).

Primary Key

Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key that can
uniquely identify each record in a table.

For the table Student we can make the student_id column as the primary key.

Composite Key

Key that consists of two or more attributes that uniquely identify any record in a table is called Composite key.
But the attributes which together form the Composite key are not a key independentely or individually.

In the above picture we have a Score table which stores the marks scored by a student in a particular subject.

In this table student_id and subject_id together will form the primary key, hence it is a composite key.

Secondary or Alternative key

The candidate key which are not selected as primary key are known as secondary keys or alternative keys.

Non-key Attributes

Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields in a table.

Non-prime Attributes

Non-prime Attributes are attributes other than Primary Key attribute(s).

Introduction to SQL
Structure Query Language(SQL) is a database query language used for storing and managing data in Relational
DBMS. SQL was the first commercial language introduced for E.F Codd's Relational model of database.
Today almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the standard database
query language. SQL is used to perform all types of data operations in RDBMS.

SQL Command

SQL defines following ways to manipulate data stored in an RDBMS.

DDL: Data Definition Language


This includes changes to the structure of the table like creation of table, altering table, deleting a table etc.
Command Description

create to create new table or database

alter for alteration

truncate delete data from table

drop to drop a table

rename to rename a table

All DDL commands are auto-committed. That means it saves all the changes permanently in the database.

DML: Data Manipulation Language

DML commands are used for manipulating the data stored in the table and not the table itself.

DML commands are not auto-committed. It means changes are not permanent to database, they can be rolled
back.

Command Description

insert to insert a new row

update to update existing row

delete to delete a row

merge merging two rows or two tables


TCL: Transaction Control Language

These commands are to keep a check on other commands and their affect on the database. These commands can
annul changes made by other commands by rolling the data back to its original state. It can also make any
temporary change permanent.

Command Description

commit to permanently save

rollback to undo change

savepoint to save temporarily

DCL: Data Control Language

Data control language are the commands to grant and take back authority from any database user.

Command Description

grant grant permission of right

revoke take back permission.

DQL: Data Query Language

Data query language is used to fetch data from tables based on conditions that we can easily apply.

Command Description

select retrieve records from one or more table


SQL: create command
create is a DDL SQL command used to create a table or a database in relational database management system.

Creating a Database

To create a database in RDBMS, create command is used. Following is the syntax,

CREATE DATABASE <DB_NAME>;

Example for creating Database

CREATE DATABASE Test;

The above command will create a database named Test, which will be an empty schema without any table.

To create tables in this newly created database, we can again use the create command.

Creating a Table

create command can also be used to create tables. Now when we create a table, we have to specify the details of
the columns of the tables too. We can specify the names and datatypes of various columns in
the create command itself.

Following is the syntax,

CREATE TABLE <TABLE_NAME>


(
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4

);

create table command will tell the database system to create a new table with the given table name and column
information.

Example for creating Table

CREATE TABLE Student(


student_id INT,
name VARCHAR(100),
age INT);
The above command will create a new table with name Student in the current database with 3 columns,
namely student_id, name and age. Where the column student_id will only store integer, name will hold upto 100
characters and age will again store only integer value.

If you are currently not logged into your database in which you want to create the table then you can also add
the database name along with table name, using a dot operator .

For example, if we have a database with name Test and we want to create a table Student in it, then we can do
so using the following query:

CREATE TABLE Test.Student(


student_id INT,
name VARCHAR(100),
age INT);

Most commonly used datatypes for Table columns

Here we have listed some of the most commonly used datatypes used for columns in tables.

Datatype Use

INT used for columns which will store integer values.

FLOAT used for columns which will store float values.

DOUBLE used for columns which will store float values.

VARCHAR used for columns which will be used to store characters and integers, basically a string.

CHAR used for columns which will store char values(single character).

DATE used for columns which will store date values.


used for columns which will store text which is generally long in length. For example, if you create a
TEXT table for storing profile information of a social networking website, then for about me section you can
have a column of type TEXT.

SQL: ALTER command


alter command is used for altering the table structure, such as,

 to add a column to existing table

 to rename any existing column

 to change datatype of any column or to modify its size.

 to drop a column from the table.

ALTER Command: Add a new Column

Using ALTER command we can add a column to any existing table. Following is the syntax,

ALTER TABLE table_name ADD(

column_name datatype);

Copy

Here is an Example for this,

ALTER TABLE student ADD(

address VARCHAR(200)

);

The above command will add a new column address to the table student, which will hold data of
type varchar which is nothing but string, of length 200.

ALTER Command: Add multiple new Columns

Using ALTER command we can even add multiple new columns to any existing table. Following is the syntax,

ALTER TABLE table_name ADD(


column_name1 datatype1,
column-name2 datatype2,
column-name3 datatype3);
Here is an Example for this,

ALTER TABLE student ADD(


father_name VARCHAR(60),
mother_name VARCHAR(60),
dob DATE);

The above command will add three new columns to the student table

ALTER Command: Add Column with default value

ALTER command can add a new column to an existing table with a default value too. The default value is used
when no value is inserted in the column. Following is the syntax,

ALTER TABLE table_name ADD(

column-name1 datatype1 DEFAULT some_value

);

Here is an Example for this,

ALTER TABLE student ADD(

dob DATE DEFAULT '01-Jan-99'

);

The above command will add a new column with a preset default value to the table student.

ALTER Command: Modify an existing Column

ALTER command can also be used to modify data type of any existing column. Following is the syntax,

ALTER TABLE table_name modify(

column_name datatype

);

Copy

Here is an Example for this,

ALTER TABLE student MODIFY(


address varchar(300));

Remember we added a new column address in the beginning? The above command will modify
the address column of the student table, to now hold upto 300 characters.

ALTER Command: Rename a Column

Using ALTER command you can rename an existing column. Following is the syntax,

ALTER TABLE table_name RENAME

old_column_name TO new_column_name;

Copy

Here is an example for this,

ALTER TABLE student RENAME

address TO location;

The above command will rename address column to location.

ALTER Command: Drop a Column

ALTER command can also be used to drop or remove columns. Following is the syntax,

ALTER TABLE table_name DROP(

column_name);

Here is an example for this,

ALTER TABLE student DROP(

address);

The above command will drop the address column from the table student.

SQL Truncate, Drop or Rename a Table


In this tutorial we will learn about the various DDL commands which are used to re-define the tables.
TRUNCATE command

TRUNCATE command removes all the records from a table. But this command will not destroy the table's
structure. When we use TRUNCATE command on a table its (auto-increment) primary key is also initialized.
Following is its syntax,

TRUNCATE TABLE table_name

Here is an example explaining it,

TRUNCATE TABLE student;

The above query will delete all the records from the table student.

In DML commands, we will study about the DELETE command which is also more or less same as
the TRUNCATE command. We will also learn about the difference between the two in that tutorial.

DROP command

DROP command completely removes a table from the database. This command will also destroy the table
structure and the data stored in it. Following is its syntax,

DROP TABLE table_name

Here is an example explaining it,

DROP TABLE student;

Copy

The above query will delete the Student table completely. It can also be used on Databases, to delete the
complete database. For example, to drop a database,

DROP DATABASE Test;

The above query will drop the database with name Test from the system.

RENAME query

RENAME command is used to set a new name for any existing table. Following is the syntax,
RENAME TABLE old_table_name to new_table_name

Here is an example explaining it.

RENAME TABLE student to students_info;

The above query will rename the table student to students_info.

Using INSERT SQL command


Data Manipulation Language (DML) statements are used for managing data in database. DML commands are
not auto-committed. It means changes made by DML command are not permanent to database, it can be rolled
back.

Talking about the Insert command, whenever we post a Tweet on Twitter, the text is stored in some table, and
as we post a new tweet, a new record gets inserted in that table.

INSERT command

Insert command is used to insert data into a table. Following is its general syntax,

INSERT INTO table_name VALUES(data1, data2, ...)

Lets see an example,

Consider a table student with the following fields.

s_id name age

INSERT INTO student VALUES(101, 'Adam', 15);

Copy

The above command will insert a new record into student table.

s_id name age

101 Adam 15
Insert value into only specific columns

We can use the INSERT command to insert values for only some specific columns of a row. We can specify the
column names along with the values to be inserted like this,

INSERT INTO student(id, name) values(102, 'Alex');

The above SQL query will only insert id and name values in the newly inserted record.

Insert NULL value to a column

Both the statements below will insert NULL value into age column of the student table.

INSERT INTO student(id, name) values(102, 'Alex');

Copy

Or,

INSERT INTO Student VALUES(102,'Alex', null);

Copy

The above command will insert only two column values and the other column is set to null.

S_id S_Name age

101 Adam 15

102 Alex

Insert Default value to a column

INSERT INTO Student VALUES(103,'Chris', default)


S_id S_Name age

101 Adam 15

102 Alex

103 chris 14

Suppose the column age in our tabel has a default value of 14.
Also, if you run the below query, it will insert default value into the age column, whatever the default value
may be.

INSERT INTO Student VALUES(103,'Chris')

Using UPDATE SQL command


Let's take an example of a real-world problem. These days, Facebook provides an option for Editing your status
update, how do you think it works? Yes, using the Update SQL command.

Let's learn about the syntax and usage of the UPDATE command.

UPDATE command

UPDATE command is used to update any record of data in a table. Following is its general syntax,

UPDATE table_name SET column_name = new_value WHERE some_condition;

Copy

WHERE is used to add a condition to any SQL query, we will soon study about it in detail.

Lets take a sample table student,

student_id name age

101 Adam 15
102 Alex

103 chris 14

UPDATE student SET age=18 WHERE student_id=102;

Copy

S_id S_Name age

101 Adam 15

102 Alex 18

103 chris 14

In the above statement, if we do not use the WHERE clause, then our update query will update age for all the
columns of the table to 18.

Updating Multiple Columns

We can also update values of multiple columns using a single UPDATE statement.

UPDATE student SET name='Abhi', age=17 where s_id=103;

s_id name age

101 Adam 15
102 Alex 18

103 Abhi 17

Copy

The above command will update two columns of the record which has s_id 103.

UPDATE Command: Incrementing Integer Value

When we have to update any integer value in a table, then we can fetch and update the value in the table in a
single statement.

For example, if we have to update the age column of student table every year for every student, then we can
simply run the following UPDATE statement to perform the following operation:

UPDATE student SET age = age+1;

Copy

As you can see, we have used age = age + 1 to increment the value of age by 1.

NOTE: This style only works for integer values.

Using DELETE SQL command


When you ask any question in Studytonight's Forum it gets saved into a table. And using the Delete option, you
can even delete a question asked by you. How do you think that works? Yes, using the Delete DML command.

Let's study about the syntax and the usage of the Delete command.

DELETE command

DELETE command is used to delete data from a table.

Following is its general syntax,

DELETE FROM table_name;

Let's take a sample table student:

s_id name age

101 Adam 15
102 Alex 18

103 Abhi 17

Delete all Records from a Table

DELETE FROM student;

The above command will delete all the records from the table student.

Delete a particular Record from a Table

In our student table if we want to delete a single record, we can use the WHERE clause to provide a condition
in our DELETE statement.

DELETE FROM student WHERE s_id=103;

Copy

The above command will delete the record where s_id is 103 from the table student.

S_id S_Name age

101 Adam 15

102 Alex 18
Isn't DELETE same as TRUNCATE

TRUNCATE command is different from DELETE command. The delete command will delete all the rows from
a table whereas truncate command not only deletes all the records stored in the table, but it also re-initializes the
table(like a newly created table).

For eg: If you have a table with 10 rows and an auto_increment primary key, and if you
use DELETE command to delete all the rows, it will delete all the rows, but will not re-initialize the primary
key, hence if you will insert any row after using the DELETE command, the auto_increment primary key will
start from 11. But in case of TRUNCATE command, primary key is re-initialized, and it will again start from 1.

Commit, Rollback and Savepoint SQL commands


Transaction Control Language(TCL) commands are used to manage transactions in the database.

Before moving forward with TCL commands, check these topics out first

 DML commands
 DDL COMMAND
These are used to manage the changes made to the data in a table by DML statements. It also allows statements
to be grouped together into logical transactions.

COMMIT command

COMMIT command is used to permanently save any transaction into the database.

When we use any DML command like INSERT, UPDATE or DELETE, the changes made by these commands
are not permanent, until the current session is closed, the changes made by these commands can be rolled back.

To avoid that, we use the COMMIT command to mark the changes as permanent.

Following is commit command's syntax,

COMMIT;

ROLLBACK command

This command restores the database to last commited state. It is also used with SAVEPOINT command to jump
to a savepoint in an ongoing transaction.

If we have used the UPDATE command to make some changes into the database, and realise that those changes
were not required, then we can use the ROLLBACK command to rollback those changes, if they were not
commited using the COMMIT command.

Following is rollback command's syntax,


ROLLBACK TO savepoint_name;

SAVEPOINT command

SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point
whenever required.

Following is savepoint command's syntax,

SAVEPOINT savepoint_name;

In short, using this command we can name the different states of our data in any table and then rollback to that
state using the ROLLBACK command whenever required.

Using the WHERE SQL clause


WHERE clause is used to specify/apply any condition while retrieving, updating or deleting data from a table.
This clause is used mostly with SELECT, UPDATE and DELETEquery.

When we specify a condition using the WHERE clause then the query executes only for those records for which
the condition specified by the WHERE clause is true.

Syntax for WHERE clause

Here is how you can use the WHERE clause with a DELETE statement, or any other statement,

DELETE FROM table_name WHERE [condition];

The WHERE clause is used at the end of any SQL query, to specify a condition for execution.

Time for an Example

Consider a table student,

s_id name age address

101 Adam 15 Chennai


102 Alex 18 Delhi

103 Abhi 17 Banglore

104 Ankit 22 Mumbai

Now we will use the SELECT statement to display data of the table, based on a condition, which we will add to
our SELECT query using WHERE clause.
Let's write a simple SQL query to display the record for student with s_id as 101.

SELECT s_id,
name,
age,
address
FROM student WHERE s_id = 101;

Following will be the result of the above query.

s_id name age address

101 Adam 15 Noida

Applying condition on Text Fields


In the above example we have applied a condition to an integer value field, but what if we want to apply the
condition on name field. In that case we must enclose the value in single quote ' '. Some databases even accept
double quotes, but single quotes is accepted by all.

SELECT s_id,
name,
age,
address
FROM student WHERE name = 'Adam';
Following will be the result of the above query.
s_id name age address

101 Adam 15 Noida

You might also like