SQL and Relational Databases

Chapter 12 · Tamil Nadu State Board

  • 19 min read
  • 4-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson teaches how SQL is used to create, manage, and query relational databases. It explains tables, fields, records, data types, constraints, and the main groups of SQL commands. Students learn how to add, change, remove, sort, and retrieve table data. These skills help users organize database information and control how it is accessed and changed.

Written by AI from this lesson. How we check it

Key points

  • SQL is used to manage and access data held in relational databases.
    From your textbook
    “It is a language designed for managing and accessing data in a Relational Data Base Management System (RDBMS).”
  • A database contains tables, and each table organizes related data into rows and columns.
    From your textbook
    “A table is a collection of related data entries and it consist of rows and columns.”
  • A field is a column, while a record is a row containing related fields.
    From your textbook
    “A Record is a row, which is a collection of related fields or columns that exist in a table.”
  • SQL commands are grouped into five components: DML, DDL, DCL, TCL, and DQL.
    From your textbook
    “SQL commands are divided into five categories:”
  • Constraints limit the values that can be entered and help keep database information accurate and reliable.
    From your textbook
    “Constraints are used to limit the type of data that can go into a table.”
  • INSERT adds records, UPDATE changes data, and DELETE removes records.
    From your textbook
    “The DML commands consist of inserting, deleting and updating rows into the table.”
  • SELECT retrieves table data, and clauses can filter, sort, or group the results.
    From your textbook
    “The SELECT command is used to query or retrieve data from a table in the database.”
Test yourself: 4 questions

Formulas in this chapter

  • CREATE TABLE syntaxCREATE TABLE <table-name> (<column name><data type>[<size>] (<column name><data type>[<size>]…… );Explained here
  • INSERT syntaxINSERT INTO <table-name> [column-list] VALUES (values);Explained here
  • DELETE syntaxDELETE FROM table-name WHERE condition;Explained here
  • UPDATE syntaxUPDATE <table-name> SET column-name = value, column-name = value,… WHERE condition;Explained here
  • SELECT syntaxSELECT <column-list>FROM<table-name>;Explained here
  • SELECT with WHERESELECT <column-name>[,<column-name>,….] FROM <table-name>WHERE condition>;Explained here
  • Find students with no Age valueSELECT * FROM Student WHERE Age IS NULL;Explained here
  • Filter and sort studentsSELECT * FROM Student WHERE Age>=18 ORDER BY Name;Explained here
  • Count students in each gender groupSELECT Gender, count(*) FROM Student GROUP BY Gender;Explained here
  • Count students from ChennaiSELECT Place, count(*) FROM Student GROUP BY Place HAVING place = 'Chennai';Explained here
  • Save transaction changesCOMMIT;Explained here
  • Create a savepointSAVEPOINT savepoint_name;Explained here

SQL and Its Role in a Database

SQL means Structured Query Language. It is used to access and manage data in a Relational Data Base Management System (RDBMS). With SQL, users can create, retrieve, alter and transfer database information.

An RDBMS stores data in tables. A table has rows and columns, and stores related entries. A database is a collection of tables that can be queried for use in other applications.

Definition

Field
A field is a column in a table that holds a particular kind of information for every record.

Definition

Record
A record is a row in a table containing related fields about one entry, such as one student.

Field and record

FieldRecord
A column; a vertical part of a tableA row; a horizontal part of a table
Holds one kind of information for every recordHolds related information about one entry

RDBMS stands for Relational DataBase Management System. Oracle, MySQL, MS SQL Server, IBM DB2 and Microsoft Access are RDBMS packages. Create, Read, Update and Delete operations are collectively called CRUD.

SQL and MySQL

SQLMySQL
A language used to access databasesA database management system and an RDBMS

Processing skills of SQL

  • Data Definition Language (DDL) defines, deletes and modifies relation schemas, and creates indexes.
  • Data Manipulation Language (DML) inserts, deletes and modifies data in a database.
  • Embedded Data Manipulation Language uses SQL within high level programming languages.
  • View Definition provides commands to define views of tables.
  • Authorization provides commands for access rights to relations and views.
  • Integrity supports condition-based integrity checking.
  • Transaction control provides commands for transactions and their processing.

Creating and Using a Database

Use CREATE DATABASE to create a database. Use USE to work with a chosen database.

Worked example

Create a database named stud, then select it for use.

Try it first, then show the solution
  1. Type CREATE DATABASE stud;
  2. Type USE stud;

Answer: The stud database is created and selected for use.

SQL Command Categories

Five categories of SQL commands

  • DML - Data Manipulation Language
  • DDL - Data Definition Language
  • DCL - Data Control Language
  • TCL - Transaction Control Language
  • DQL - Data Query Language

DDL defines the database structure or schema. It is used to create and modify database objects. DDL can specify data types, data item sizes and, in some cases, value ranges and privacy locks.

DDL commands

  • Create creates tables in the database.
  • Alter changes the structure of the database.
  • Drop deletes tables from the database.
  • Truncate removes all records from a table and releases the space occupied by those records.

Data Manipulation Language

DML changes data stored in a database, but does not change the database table schema. It is used to insert, retrieve, delete and modify information.

Types of DML

Procedural DMLNon-Procedural DML
The user specifies what data is needed and how to get it.The user specifies what data is needed, but not how to get it.

DML commands

  • Insert adds data to a table.
  • Update changes existing data in a table.
  • Delete removes all records from a table but does not release the space they occupy.

Remember: Remember: Truncate removes all records and releases their space; Delete removes all records but does not release their space.

DCL, TCL and DQL Commands

Database command groups

GroupPurpose and commands
DCLGRANT gives users permission to perform specific tasks. REVOKE withdraws permission given by GRANT.
TCLCOMMIT permanently saves a transaction. ROLLBACK restores the database to its last commit state. SAVE POINT temporarily saves a transaction so that you can roll
DQLSELECT displays records from a table.

TCL manages changes made to table data by DML statements. DCL privileges are needed for database operations, including creating sequences and views of tables.

Remember: Remember: GRANT gives permission, REVOKE withdraws it, COMMIT saves a transaction, and SELECT displays records.

SQL Data Types

A data type describes the kind of value stored in a field. All values in a given field must have the same type. ANSI SQL recognizes Text and Number data types, while some commercial programs also use types such as Date and Time.

Character data types

Data typeWhat it stores
char (Character)A fixed-width string. Values are enclosed in single quotes.
varcharA variable-width character string. Its data-entry size can vary considerably.

Number data types

Data typeWhat it stores or means
dec (Decimal)A fractional number, such as 15.12 or 0.123. Its size has precision and scale. Precision is the number of digits allowed; scale is the maximum number of digits
numericLike decimal, but the maximum number of digits may not exceed the precision argument.
int (Integer)A number without a decimal point. The size argument is not used.
smallintLike integer, but its default size may be smaller.
floatA floating point number in base 10 exponential notation. Precision may be up to a maximum of 64.
realLike float, but the size argument is not used. Precision may be up to a maximum of 64.
doubleLike real, except precision may exceed 64.

Remember: For decimal data, precision counts the digits allowed and scale counts the digits allowed after the decimal point.

SQL Terms and Creating Tables

Basic SQL terms

TermMeaning
KeywordsWords with a special meaning in SQL that are understood as instructions.
CommandsInstructions given by the user to the database; also called statements.
ClausesParts that begin with a keyword and contain a keyword and an argument.
ArgumentsValues given to complete a clause.

Tables are used to store data, so information must be arranged in tables. CREATE TABLE makes a table and specifies its column names, data types and sizes. Every table must have at least one column.

CREATE TABLE syntax

CREATE TABLE <table-name> (<column name><data type>[<size>] (<column name><data type>[<size>]…… );

<table-name> is the table name. Each column is given a name, data type and optional size.

Worked example

Create a Student table with Admno, Name, Gender, Age and Place.

Try it first, then show the solution
  1. Give Admno and Age the integer data type.
  2. Give Name the char(20) data type and Gender the char(1) data type.
  3. Give Place the char(10) data type.

Answer: CREATE TABLE Student (Admno integer, Name char(20), Gender char(1), Age integer, Place char(10), );

Definition

Constraint
A constraint is a condition applied to a field or a set of fields to limit the data that can go into a table.

Constraint levels

TypeWhere it applies
Column constraintApplies only to an individual column.
Table constraintApplies to a group of one or more columns.

Constraints help ensure the accuracy and reliability of database data. In the Student table, NOT NULL makes a field require a value. The example also declares Admno as a PRIMARY KEY.

Worked example

Create a Student table where Admno is NOT NULL and is the PRIMARY KEY, and Name is NOT NULL.

Try it first, then show the solution
  1. Set Admno to integer and add NOT NULL PRIMARY KEY.
  2. Set Name to char(20) and add NOT NULL.
  3. Add Gender, Age and Place with their data types.

Answer: CREATE TABLE Student ( Admno integer NOT NULL PRIMARY KEY, Name char(20) NOT NULL, Gender char(1), Age integer, Place char(10), );

Unique Constraint

Definition

UNIQUE constraint
A UNIQUE constraint ensures that no two rows have the same value in the specified columns.

For example, applying UNIQUE to Admno prevents two students from having the same admission number. UNIQUE can be applied only to fields also declared as NOT NULL. When both constraints apply to one field, they are multiple constraints and are separated by a space.

Worked example

Create a Student table where Admno must have a value and must not be duplicated.

Try it first, then show the solution
  1. Set Admno to integer.
  2. Add NOT NULL and UNIQUE to Admno.

Answer: CREATE TABLE Student ( Admno integer NOT NULL UNIQUE, Name char (20) NOT NULL, Gender char (1), Age integer, Place char (10), );

Remember: NOT NULL means a value is required; UNIQUE means that value must not be duplicated.

Primary Key, Default and Check Constraints

How the constraints work

ConstraintPurpose and rule
PRIMARY KEYUniquely identifies a record. Only one field in a table can be set as the primary key, and its value cannot be NULL.
DEFAULTAutomatically supplies a chosen value when no value is entered for that field.
CHECKLimits the values allowed in a field by applying a condition.

In the Student table, Admno is the primary key, so it identifies each record and cannot be empty. Age integer DEFAULT 17 gives Age the value 17 when no value is supplied. Age integer CHECK (Age <=19) allows only ages that meet the condition.

Definition

Table constraint
A constraint applied to a group of fields is called a table constraint and is normally written at the end of the table definition.

Worked example

How does PRIMARY KEY (Firstname, Lastname) work in Student1?

Try it first, then show the solution
  1. The table constraint uses both Firstname and Lastname.
  2. Together, these two fields are defined as the primary key.

Answer: Firstname and Lastname form a table constraint that acts as the primary key.

INSERT Command

Definition

INSERT command
The INSERT command adds new data or records to a table.

Use INSERT INTO with the table name and VALUES to add a record. The values must follow the same order as the columns. If data is supplied for every column, listing the column names is optional.

INSERT syntax

INSERT INTO <table-name> [column-list] VALUES (values);

<table-name> is the table receiving data, [column-list] names the columns if needed, and values are the data to insert.

Worked example

Add a Student record with Admno 103, Name Ayush and Place Delhi, leaving Gender and Age out of the command.

Try it first, then show the solution
  1. Specify the columns Admno, Name and Place.
  2. Supply the matching values 103, ‘Ayush’ and ‘Delhi’.
  3. The omitted fields take their defined default values, or NULL if no default is defined.

Answer: INSERT INTO Student(Admno, Name, Place) VALUES (103, ‘Ayush’, ‘Delhi’);

Remember: For omitted fields, a defined default value is used; otherwise, NULL is stored.

DELETE and UPDATE Commands

What each command changes

CommandEffect
DELETEPermanently removes entire records, not individual fields. A WHERE condition selects records; without it, all rows are deleted.
UPDATEChanges data values in one or more records. SET gives the new values, and WHERE selects the records to change.
DELETE syntax

DELETE FROM table-name WHERE condition;

table-name is the table to change, and condition selects the records to remove.

Worked example

Delete the Student record whose admission number is 104.

Try it first, then show the solution
  1. Choose Student as the table.
  2. Use Admno=104 as the condition.

Answer: DELETE FROM Student WHERE Admno=104;

UPDATE syntax

UPDATE <table-name> SET column-name = value, column-name = value,… WHERE condition;

<table-name> is the table to change, SET assigns new values to columns, and WHERE selects the records to update.

Worked example

Change Age to 20 for students whose Place is Bangalore.

Try it first, then show the solution
  1. Use Student as the table.
  2. Set Age to 20.
  3. Select records where Place is ‘Bangalore’.

Answer: UPDATE Student SET Age = 20 WHERE Place = ‘Bangalore’;

To change several fields in one record, include multiple assignments in SET and separate them with commas. For example, UPDATE Student SET Age=18, Place = ‘Chennai’ WHERE Admno = 102; changes both Age and Place for the selected record.

Remember: Check the WHERE condition carefully: it determines which records DELETE or UPDATE affects.

ALTER TABLE command

ALTER changes a table’s structure. It can add, modify, rename or remove a column. Use the form that matches the change you need.

Common ALTER TABLE commands

  • Add a column: ALTER TABLE <table-name> ADD <column-name><data type><size>;
  • Modify a column: ALTER TABLE <table-name> MODIFY<column-name><data type><size>;
  • Rename a column: ALTER TABLE <table-name> CHANGE old-column-name new-column-name new column definition;
  • Remove a column: ALTER TABLE <table-name> DROP COLUMN <column-name>;

Worked example

Add an Address column of type char to Student.

Try it first, then show the solution
  1. Use ALTER TABLE with ADD and the column name and data type.
  2. Command: ALTER TABLE Student ADD Address char;

Answer: The Student table now has an Address column.

Worked example

Change Address in Student to hold 25 characters.

Try it first, then show the solution
  1. Use MODIFY with the column name, type and size.
  2. Command: ALTER TABLE Student MODIFY Address char (25);

Answer: Address is modified to hold 25 characters.

Worked example

Rename Address to City and then remove City from Student.

Try it first, then show the solution
  1. Rename the column: ALTER TABLE Student CHANGE Address City char(20);
  2. Remove the column: ALTER TABLE Student DROP COLUMN City;

Answer: Address is renamed to City, and City is then removed.

TRUNCATE and DROP TABLE commands

TRUNCATE removes all rows from a table but keeps its structure and frees the space used by the table. DROP TABLE removes the table, its rows and its structure from the database. A dropped table cannot be recovered.

TRUNCATE and DROP TABLE

TRUNCATEDROP TABLE
Deletes all rows; keeps the table structure and frees the table space.Deletes all rows and removes the table structure from the database.
Command: TRUNCATE TABLE Student;Command: DROP TABLE Student;
The table structure remains.The table cannot be got back once dropped.

DELETE removes rows based on a condition in the WHERE clause. If no condition is given, it removes all rows. It does not free the space containing the table.

Remember: DELETE removes rows, TRUNCATE removes all rows but keeps the structure, and DROP TABLE removes the table itself.

SELECT command and retrieving data

Definition

Query
A Query is a command given to get a desired result from a database table.

SELECT retrieves data from one or more tables. Choose the columns you want in the column-list. Use * to display all fields and rows.

SELECT syntax

SELECT <column-list>FROM<table-name>;

<column-list> names the columns to retrieve, and <table-name> names the table.

Worked example

Display students’ admission numbers and names from Student.

Try it first, then show the solution
  1. Choose Admno and Name as the columns.
  2. Command: SELECT Admno, Name FROM Student;

Answer: The result displays the Admno and Name columns.

Worked example

Display all fields and rows from Student.

Try it first, then show the solution
  1. Use * to select all fields.
  2. Command: SELECT * FROM STUDENT;

Answer: The result displays all fields and rows.

DISTINCT, ALL and WHERE

DISTINCT and ALL

DISTINCTALL
Eliminates duplicate rows from the result.Retains duplicate rows and displays every value.
SELECT DISTINCT Place FROM Student;SELECT ALL Place FROM Student;
Only one NULL value is returned, even if more NULL values occur.Duplicate entries are not eliminated.

WHERE sets the condition for selecting rows. Relational operators such as =, <, <=, >, >= and <> compare values. AND, OR and NOT can connect or change search conditions.

SELECT with WHERE

SELECT <column-name>[,<column-name>,….] FROM <table-name>WHERE condition>;

The column names specify what to retrieve, the table name specifies where to retrieve it from, and condition determines which rows are selected.

Worked example

Display admission number, name and place for students from Chennai.

Try it first, then show the solution
  1. Set Place = ῾Chennai᾿ as the condition.
  2. Command: SELECT Admno, Name, Place FROM Student WHERE Place = ῾Chennai᾿;

Answer: The result includes students whose place is Chennai.

More WHERE examples

  • Age condition: SELECT Admno, Name, Age FROM Student WHERE Age >= 18;
  • Both conditions must be true: SELECT Admno, Name, Age, Place FROM Student WHERE (Age>=18 AND Place = ῾Delhi᾿);
  • At least one condition must be true: SELECT Admno, Name, Age, Place FROM Student WHERE (Age>=18 OR Place =῾Delhi᾿);
  • Exclude students whose place is Delhi: SELECT Admno, Name, Place FROM Student WHERE (NOT Place =῾Delhi᾿);

SQL Conditions: BETWEEN, IN and NULL

BETWEEN checks whether a value falls within a stated range. NOT BETWEEN selects values outside that range. IN checks whether a value matches any value in a list, while NOT IN selects values that do not match the list.

Examples of conditions

  • Age BETWEEN 18 AND 19 selects students aged 18 or 19.
  • Age NOT BETWEEN 18 AND 19 selects students whose ages are outside that range.
  • Place IN ('Chennai', 'Delhi') selects students from either place.
  • Place NOT IN ('Chennai', 'Delhi') selects students from other places.

Definition

NULL value
A NULL value means that a field contains no value.

Use IS NULL in the WHERE clause to find fields with no value. Use IS NOT NULL to list fields that do contain values.

Find students with no Age value

SELECT * FROM Student WHERE Age IS NULL;

Selects all Student records where Age contains no value.

Remember: Use IS NULL or IS NOT NULL to test for missing values.

Filtering, Sorting and Grouping Records

Definition

WHERE clause
The WHERE clause filters records and returns only those that satisfy a condition.

Definition

ORDER BY clause
The ORDER BY clause sorts data using one or more columns.

Sorting order

KeywordEffect
ASCSorts in ascending order; this is the default.
DESCSorts in descending order.
Filter and sort students

SELECT * FROM Student WHERE Age>=18 ORDER BY Name;

Selects students aged 18 and above, then sorts them by Name in ascending order.

A WHERE condition can be combined with ORDER BY. Sorting can use multiple fields. ORDER BY changes the displayed order, not the original table.

Definition

GROUP BY clause
The GROUP BY clause puts records with identical values in a column into groups.

GROUP BY is often used with aggregate functions to make summary reports. For example, grouping by Gender returns one group for each gender value.

Count students in each gender group

SELECT Gender, count(*) FROM Student GROUP BY Gender;

Groups records by Gender and counts the records in each group.

Definition

HAVING clause
The HAVING clause applies a condition to groups created by GROUP BY and can use aggregate functions.
Count students from Chennai

SELECT Place, count(*) FROM Student GROUP BY Place HAVING place = 'Chennai';

Groups students by Place, then shows the count for Chennai.

Transaction Commands: COMMIT, ROLLBACK and SAVEPOINT

Definition

COMMIT
COMMIT permanently saves transaction changes to the database.

Changes made using INSERT, UPDATE or DELETE are not permanent until COMMIT is given. After COMMIT, those changes cannot be rolled back.

Save transaction changes

COMMIT;

Permanently saves the current transaction changes.

Definition

SAVEPOINT
SAVEPOINT marks a temporary position in a transaction that can be returned to later.

Definition

ROLLBACK
ROLLBACK restores the database to the last committed state or to a named savepoint.
Create a savepoint

SAVEPOINT savepoint_name;

Marks the current transaction position using the given savepoint name.

Return to a savepoint

ROLL BACK TO save point name;

Restores the transaction to the named savepoint.

Transaction example sequence

  • Insert a student record and use COMMIT to save it permanently.
  • Update a student's name and create SAVEPOINT A.
  • Insert another student record and create SAVEPOINT B.
  • Use ROLLBACK TO A to return to the state marked by A.

Remember: COMMIT makes changes permanent; a savepoint gives ROLLBACK a named position to return to.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is SQL used for?

SQL is a language used to access and manage data in a Relational Data Base Management System (RDBMS). It lets users create, retrieve, alter and transfer information among databases.

What is the difference between a field and a record in a table?

A field is a column that holds a particular kind of information for every record. A record is a row containing related fields about one entry, such as the details of one student.

What are the five categories of SQL commands?

The five categories are DML (Data Manipulation Language), DDL (Data Definition Language), DCL (Data Control Language), TCL (Transaction Control Language) and DQL (Data Query Language).

What is the difference between Delete and Truncate?

Delete removes all records from a table but does not remove the space they occupied. Truncate removes all records and also releases the space occupied by those records.

What is the difference between SQL and MySQL?

SQL is a language used to access databases. MySQL is a database management system and an RDBMS, like SQL Server, Oracle, Informix and Postgres.

What is the difference between GRANT and REVOKE in SQL?

GRANT gives permission to one or more users to perform specific tasks. REVOKE withdraws access permission that was given by a GRANT statement. These commands belong to Data Control Language, which controls access to database data.

Show 16 more questions

What do COMMIT, ROLLBACK and SAVE POINT do in SQL?

COMMIT permanently saves a transaction into the database. ROLLBACK restores the database to its last commit state. SAVE POINT temporarily saves a transaction so that you can roll back. These are Transactional Control Language commands.

What is the difference between char and varchar in SQL?

char stores a fixed-width string, while varchar stores a variable-width character string. The size of a varchar data entry can vary considerably. Character values are enclosed in single quotes.

What do precision and scale mean in a decimal data type?

Precision indicates how many digits a number may have. Scale indicates the maximum number of digits to the right of the decimal point. For size (5, 2), precision is 5 and scale is 2. Scale cannot exceed precision.

What is a UNIQUE constraint in SQL?

A UNIQUE constraint ensures that no two rows have the same value in the specified columns. It can be used only on fields also declared as NOT NULL. Together, NOT NULL and UNIQUE require a value and prevent that value from being duplicated.

What is a primary key constraint?

A primary key constraint identifies a record uniquely. Only one field in a table can be set as its primary key, and that field cannot contain a NULL value. For example, Admno can identify a Student record.

What does the DEFAULT constraint do in SQL?

The DEFAULT constraint assigns a chosen value to a field when no value is entered for it. For example, if Age has DEFAULT 17, the value 17 is supplied when Age is left out of the INSERT command.

What is the difference between DELETE and UPDATE commands?

DELETE removes entire records from a table. UPDATE changes data values in selected records. DELETE uses a WHERE condition to choose records to remove, while UPDATE uses SET to provide new values and WHERE to choose records to change.

What happens to fields omitted from an INSERT command?

An omitted field receives its defined default value, if one exists. If no default value is defined for that field, NULL is stored. When data is inserted for every column, the column names can be left out of the command.

How do I add a column to a table using ALTER TABLE?

Use ALTER TABLE with ADD, followed by the column name and its data type. For example, ALTER TABLE Student ADD Address char; adds an Address column of type char to Student.

What is the difference between TRUNCATE and DROP TABLE?

TRUNCATE deletes all rows but keeps the table structure and frees the table space. DROP TABLE deletes all rows and removes the table structure from the database. A dropped table cannot be got back.

How do SELECT and WHERE work in SQL?

SELECT retrieves chosen columns from a table. WHERE adds a condition to decide which rows appear in the result. For example, SELECT Admno, Name FROM Student WHERE Place = ῾Chennai᾿; selects admission numbers and names for students from Chennai.

What is the difference between DISTINCT and ALL in SQL?

DISTINCT eliminates duplicate rows from the result. ALL retains duplicates and displays every row’s value. With DISTINCT, only one NULL value is returned, even if more NULL values occur.

What is the difference between BETWEEN and NOT BETWEEN in SQL?

BETWEEN selects records whose values fall within the stated range. NOT BETWEEN selects records whose values fall outside that range. For example, Age BETWEEN 18 AND 19 selects ages 18 and 19, while Age NOT BETWEEN 18 AND 19 selects ages outside that range.

How do IN and NOT IN work in SQL?

IN checks whether a column value matches any value in a given list. NOT IN selects records whose values do not match the list. For example, Place IN ('Chennai', 'Delhi') selects students from either place, while NOT IN selects students from other places.

What is the difference between WHERE and HAVING?

WHERE filters individual records using a condition. HAVING places a condition on groups created with GROUP BY and can include aggregate functions. Use WHERE to filter records before grouping, and HAVING to filter the resulting groups.

What do COMMIT, SAVEPOINT and ROLLBACK do in SQL?

COMMIT permanently saves transaction changes. SAVEPOINT marks a temporary position in a transaction. ROLLBACK restores the database to the last committed state or returns it to a named savepoint, allowing changes after that point to be undone.

Book back answers

17 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

Choose the best answer (1 Mark)

  1. Which commands provide definitions for creating table structure, deleting relations, and modifying relation schemas.

    • a. DDL
    • b. DML
    • c. DCL
    • d. DQL
    Try it, then show the answer

    a. DDL

    DDL commands define, delete, and modify database structures.

    From the book: “The SQL DDL provides commands for defining relation schemas (structure), deleting relations”

  2. Which command lets to change the structure of the table?

    • a. SELECT
    • b. ORDER BY
    • c. MODIFY
    • d. ALTER
    Try it, then show the answer

    d. ALTER

    ALTER is used to change a table's structure, such as adding or modifying a column.

    From the book: “The ALTER command is used to alter the table structure like adding a column”

  3. The command to delete a table including the structure is

    • a. DROP
    • b. DELETE
    • c. DELETE ALL
    • d. ALTER TABLE
    Try it, then show the answer

    a. DROP

    DROP TABLE deletes the rows and removes the table structure from the database.

    From the book: “If you drop a table, all the rows in the table is deleted and the table structure is removed”

  4. Queries can be generated using

    • a. SELECT
    • b. ORDER BY
    • c. MODIFY
    • d. ALTER
    Try it, then show the answer

    a. SELECT

    The SELECT command is used to query or retrieve data from a database table.

    From the book: “The SELECT command is used to query or retrieve data from a table in the database.”

All 17 book back answers →

Practice

Question 1 of 4Score 0

Which SQL component is used to retrieve data from a table?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is the difference between a field and a record?2 marks

A field is a column designed to hold specific related information. A record is a row containing related fields and representing one set of details.

State the purpose of the DDL and DML components of SQL.2 marks

DDL defines and modifies database structures such as tables. DML inserts, deletes, and modifies data stored in tables.

How do DELETE, TRUNCATE, and DROP differ?5 marks

DELETE, TRUNCATE and DROP are SQL commands used to remove data or database objects. They differ in what they remove and whether the table structure remains.

  1. DELETE: DELETE removes rows from a table. A WHERE condition can select particular rows; without a condition, all rows are deleted, but the table’s space is not freed.
  2. TRUNCATE: TRUNCATE deletes all rows from a table. The table structure remains, and the space occupied by the rows is freed.
  3. DROP: DROP removes the table from the database, deleting all its rows and its structure. Once dropped, the table cannot be got back.
  4. Scope of DELETE: Unlike TRUNCATE, DELETE can remove rows based on a WHERE condition, or remove all rows when no condition is specified.
  5. Structure after the command: After DELETE or TRUNCATE, the table structure remains; DROP removes the table structure from the database.

Example: To remove all records from Student, the chapter gives: TRUNCATE TABLE Student;

Thus, DELETE removes selected or all rows, TRUNCATE removes all rows while retaining the table, and DROP removes the table itself.

Explain how WHERE and ORDER BY can be used together in a SELECT command.5 marks

In a SELECT command, WHERE filters records according to a condition, while ORDER BY sorts the records that meet the condition.

  1. Filter records: Use the WHERE clause to retrieve only records that satisfy the given condition.
  2. Sort the filtered records: Place ORDER BY after the WHERE condition to sort the records that were selected.
  3. Ascending order by default: If no sorting direction is specified, ORDER BY arranges the results in ascending order.
  4. Specify sorting direction: Use ASC for ascending order or DESC for descending order.
  5. General command form: Write the SELECT command with the table name, WHERE condition and ORDER BY column in that order.
  6. Effect on the table: The combined command filters and sorts the displayed results; it does not change the original table.

Example: SELECT * FROM Student WHERE Age>=18 ORDER BY Name DESC; This displays students aged 18 and above in descending order of their names.

Thus, WHERE selects the required records and ORDER BY arranges those records in the specified order.

Watch out

  • Do not confuse DELETE with DROP: DELETE removes rows, while DROP removes the table structure as well.
  • Do not confuse SQL with MySQL: SQL is a language, while MySQL is a database management system.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →