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

LN 12

Uploaded by

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

LN 12

Uploaded by

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

Lesson – 12 STRUCTURED QUERY LANGUAGE

SQL - Structured Query Language


 SQL is a standard programming language to access and manipulate databases.
 It allows the user to create, retrieve, alter, and transfer information among databases.
 It is designed for managing and accessing data in a RDBMS.
Versions of SQL
 The original version is Sequel developed at IBM’s Research in early 1970’s.
 Later the language was changed to SQL published by ANSI (American National Standard
Institute) in 1986.
 SQL standard was updated again in 1992.
 Latest version of SQL is SQL 2008 released in 2008.

In general, Database is a collection of tables.


Table: It is a collection of related data entries and it consists of rows and columns.
Row: It is called as Record, which is a collection of related fields or columns that exist
in a table. A record is a horizontal entity.
Column: It is called as field, that is designed to maintain specific related information
about every record in the table. It is a vertical entity.

RDBMS : It is a type of DBMS which stores data in table structure. It includes functions
related to Create, Read, Update and Delete operations, collectively known as
CRUD. RDBMS packages are Oracle, MySQL, MS SQL Server, IBM DB2 and
Microsoft Access

MySQL is a RDBMS.

Creating Database

To create a database:
CREATE DATABASE database_name;
Example:
CREATE DATABASE stud;
To work with the database:
USE DATABASE;
Example:
USE stud;

Part - II
Answer the following questions: (2 Marks)

1. Write a query that selects all students whose age is less than 18 in order wise.
Example: student table
Admno Name Age Place
105 Revathi 18 Cochin
106 Devika 20 Bangalore
107 Bala 16 Cochin
108 Ganesh 17 Tirunelveli
SELECT * FROM student WHERE age < 18 ORDER BY Age ASC;
Will display the following data:
Admno Name Age Place
107 Bala 16 Cochin
108 Ganesh 17 Tirunelveli

2. Differentiate Unique and Primary Key constraint.


Unique Key Primary Key
Unique Key constraint ensures that no two Primary Key constraint helps to uniquely
rows have the same value in the specified identify a record.
columns.
The UNIQUE constraint can be applied only It does not accept NULL values.
to fields that have also been declared as
NOT NULL.
A table can have more than one unique key. A table can have only one Primary key.
Example: Example:
CREATE TABLE student ( CREATE TABLE student (
admno INTEGER NOT NULL UNIQUE, admno INTEGER NOT NULL PRIMARY KEY ,
name CHAR (20), gender CHAR (1), name CHAR (20), gender CHAR (1),
age INTEGER , place CHAR (10) ); age INTEGER , place CHAR (10) );
3. Write the difference between table constraint and column constraint?
Table constraint Column constraint
Table constraint applies to a group of one Column constraint applies only to individual
or more columns. column.
Example: Example:
CREATE TABLE Student ( CREATE TABLE student (
firstname CHAR (20), lastname CHAR (20), admno INTEGER NOT NULL PRIMARY KEY ,
gender CHAR (1), age INTEGER, name CHAR (20), gender CHAR (1),
place CHAR (10), age INTEGER, place CHAR (10) );
PRIMARY KEY (firstname, lastname) );
4. Which component of SQL lets insert values in tables and which lets to create a table?
DDL – (DATA DEFINITION LANGUAGE) component of SQL is used to create table.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY KEY ,
name CHAR (20), gender CHAR (1),
age INTEGER, place CHAR (10) );

DML – (DATA MANIPULATION LANGUAGE) component of SQL is used to insert values in to table.
Example:
INSERT INTO student VALUES (107, 'Bala', 20, 'Cochin');

5. What is the difference between SQL and MySQL?


SQL-Structured Query Language is a language used for accessing databases while
MySQL is a database management system, like SQL Server, Oracle, Informix,
Postgres, etc. MySQL is a RDBMS.

Part - III
Answer the following questions: (3 Marks)

1. What is a constraint? Write short note on Primary key constraint.


Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the database. Constraints could be either on a
column level or a table level.
Primary Key constraint
 Primary Key constraint helps to uniquely identify a record.
 It does not accept NULL values and therefore a field declared as primary key must
have the NOT NULL constraint.
 A table can have only one Primary key.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY KEY ,
name CHAR (20), gender CHAR (1),
age INTEGER, place CHAR r (10) );

2. Write a SQL statement to modify the student table structure by adding a new field.
The ALTER command is used to alter the table structure like adding a column, renaming
the existing column, change the data type of any column or size of the column or delete
the column from the table.
Syntax:
ALTER TABLE <table-name> ADD <column-name><datatype><size>;
Example:
To add a new column “Address” of type ‘char’ to the Student table, command is used as:
ALTER TABLE Student ADD address CHAR(10);

3. Write any three DDL commands.


DATA DEFINITION LANGUAGE
The Data Definition Language (DDL) consists of SQL statements used to define the
database structure or schema. It simply deals with descriptions of the database schema
and is used to create and modify the structure of database objects in databases. SQL
commands which come under Data Definition Language are: Create Alter, Drop and
Truncate.
Create To create tables in the database.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY KEY,
name char (20), gender char (1),
age integer, place char (10) );
Alter Alters the structure of the database.
Example:
ALTER TABLE student ADD address char;
Drop Delete tables from database.
Example:
DROP TABLE student;

4. Write the use of Savepoint command with an example.


The SAVEPOINT command is used to temporarily save a transaction so that we can
rollback to the point whenever required. The different states of our table can be saved at
anytime using different names and the rollback to that state can be done using the
ROLLBACK command.
SAVEPOINT savepoint_name;
Example: student table
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore
SAVEPOINT A;
INSERT INTO student VALUES (107, 'Bala', 20 , 'Cochin');
SELECT * FROM student;
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore
107 Bala 20 Cochin
ROLLBACK TO A;
SELECT * FROM student;
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore

5. Write a SQL statement using DISTINCT keyword.


The DISTINCT keyword is used along with the SELECT command to eliminate duplicate
rows in the table.
Example: student table
Admno Name Age Place
105 Revathi 19 Cochin
106 Devika 19 Bangalore
107 Bala 20 Cochin
108 Param 18 Tirunelveli
SELECT DISTINCT Place FROM Student;
Will display the following data:
Place
Cochin
Bangalore
Tirunelveli
Part – IV
Answer the following questions: (5 Marks)
1. Write the different types of constraints and their functions.
Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the database. Constraints could be either on a
column level or a table level. The different types of constraints are:
 Unique Constraint
 Primary Key constraint
 DEFAULT Constraint
 Check Constraint
Unique Key constraint
 Unique Key constraint ensures that no two rows have the same value in the
specified columns.
 It accepts only one NULL value.
 A table can have more than one unique key.
Example:
CREATE TABLE student ( admno integer Unique,
name char (20), gender char (1),
age integer, place char (10) );
Primary Key constraint
 Primary Key constraint helps to uniquely identify a record.
 It does not accept NULL values and therefore a field declared as primary key must
have the NOT NULL constraint.
 A table can have only one Primary key.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY KEY ,
name char (20), gender char (1),
age integer, place char (10) );

DEFAULT constraint
 The DEFAULT constraint is used to assign a default value for the field.
 When no value is given for the specified field having DEFAULT constraint,
automatically the default value will be assigned to the field.
Example:
CREATE TABLE student ( admno integer NOT NULL PRIMARY
KEY,
name char (20), gender char (1),
age integer DEFAULT = “17”,
place char (10) );
Check Constraint
 Check Constraint helps to set a limit value placed for a field.
 When we define a check constraint on a single column, it allows only the restricted
values on that field.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY KEY,
name char (20), gender char (1),
age integer (CHECK<=19),
place char (10) );

2. Consider the following employee table. Write SQL commands for the qtns.(i) to (v).

(i) To display the details of all employees in descending order of pay.


SELECT * FROM employee ORDER BY pay DESC;

(ii) To display all employees whose allowance is between 5000 and 7000.
SELECT * FROM employee WHERE allowance BETWEEN 5000 AND 7000;

(iii) To remove the employees who are mechanic.


DELETE FROM employee WHERE DESIG=’Mechanic’;

(iv) To add a new row.


INSERT INTO employee VALUES (‘E1006, 'Bala', ‘Engineer’, 30000, 15000);

(v) To display the details of all employees who are operators.


SELECT * FROM employee WHERE DESIG=’operator’;

3. What are the components of SQL? Write the commands in each.


Components of SQL are divided into five categories:
 DML - Data Manipulation Language
 DDL - Data Definition Language
 DCL - Data Control Language
 TCL - Transaction Control Language
 DQL - Data Query Language

DATA MANIPULATION LANGUAGE


A Data Manipulation Language (DML) is a computer programming language used for adding
(inserting), removing (deleting), and modifying (updating) data in a database. In SQL, the
data manipulation language comprises the SQL-data change statements, which modify
stored data.
Insert Inserts data into a table
Example:
INSERT INTO student VALUES (107, 'Bala', 20 , 'Cochin');
Update Updates the existing data within a table.
Example:
UPDATE Student SET Age = 22 WHERE Place = “Cochin”;
Delete Deletes all records from a table, but not the space occupied by
them.
Example:
DELETE * FROM Student;

DATA DEFINITION LANGUAGE


The Data Definition Language (DDL) consists of SQL statements used to define the database
structure or schema. It simply deals with descriptions of the database schema and is used to
create and modify the structure of database objects in databases. SQL commands which come
under Data Definition Language are: Create Alter, Drop and Truncate.
Create To create tables in the database.
Example:
CREATE TABLE student (admno integer NOT NULL PRIMARY
KEY,
name char (20), gender char (1),
age integer, place char (10) );
Alter Alters the structure of the database.
Example:
ALTER TABLE student ADD address char;
Drop Delete tables from database.
Example:
DROP TABLE student;
Truncate Remove all records from a table, also release the space occupied
by those records.
Example:
TRUNCATE TABLE student;

DATA CONTROL LANGUAGE


A Data Control Language (DCL) is a programming language used to control the access of data
stored in a database. It is used for controlling privileges in the database (Authorization). The
privileges are required for performing all the database operations such as creating sequences,
views of tables etc.
Grant Grants permission to one or more users to perform specific tasks.
Revoke Withdraws the access permission given by the GRANT statement.

TRANSACTIONAL CONTROL LANGUAGE


Transactional control language (TCL) commands are used to manage transactions in the
database. These are used to manage the changes made to the data in a table by DML
statements.
Commit Saves any transaction into the database permanently.
Roll back Restores the database to last commit state.
Save point Temporarily save a transaction so that you can rollback.
Example: student table
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore
COMMIT;
SAVEPOINT A;
INSERT INTO student VALUES (107, 'Bala', 20 , 'Cochin');
SELECT * FROM student;
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore
107 Bala 20 Cochin
ROLLBACK TO A;
SELECT * FROM student;
Admno Name Age Place
105 Revath 19 Chennai
106 Devika 19 Bangalore

DATA QUERY LANGUAGE


The Data Query Language consists of commands used to query or retrieve data from a
database.
Select The SELECT command is used to query or retrieve data from a table in
the database. It is used to retrieve a subset of records from one or
more tables.
Syntax : SELECT <column-list>FROM<table-name>;
Example: SELECT * FROM student;

4. Construct the following SQL statements in the student table-


(i) SELECT statement using GROUP BY clause.
SELECT Gender FROM student GROUP BY Gender;
(ii) SELECT statement using ORDER BY clause.
SELECT * FROM student ORDER BY Name;
5. Write a SQL statement to create a table for employee having any five fields and create a
table constraint for the employee table.

CREATE TABLE employee(empcode char(10) NOT NULL,


ename char(20),
desig char(10),
pay integer,
place char(10),
PRIMARY KEY (empcode) → Table constraint );

6. Explain SELECT command in detail (OR)


Various forms of SELECT command with example.
The SELECT command is used to query or retrieve data from a table in the
database. It is used to retrieve a subset of records from one or more tables.
Select
Syntax : SELECT <column-list>FROM<table-name>;
Example: SELECT * FROM student;
The SELECT command can be used in various forms:
(i)DISTINCT Keyword
The DISTINCT keyword is used along with the SELECT command to eliminate
duplicate rows in the table. This helps to eliminate redundant data. When the
keyword DISTINCT is used, only one NULL value is returned, even if more
NULL values occur.
Example: SELECT DISTINCT place FROM student;

(ii)ALL Keyword
The ALL keyword retains duplicate rows. It will display every row of the table
without considering duplicate entries. Example: SELECT ALL place FROM
student;

(iii) BETWEEN and NOT BETWEEN Keywords


The BETWEEN keyword defines a range of values the record must fall into to
make the condition true. The range may include an upper value and a lower
value between which the criteria must fall into. Example: SELECT admno,name FROM
student WHERE age BETWEEN 18 AND 20;

The NOT BETWEEN is reverse of the BETWEEN operator where the records
not satisfying the condition are displayed. Example: SELECT admno,name FROM student WHERE
age Not BETWEEN 18 AND 20;

(iv) IN Keyword and NOT IN keyword


The IN keyword is used to compare a column with more than one value. It is
similar to an OR condition. Example: SELECT admno,name FROM student WHERE place
IN (“TVl”, “Tuty”);

The NOT IN keyword displays only those records that do not match in the list.
Example: SELECT admno,name FROM student WHERE place NOT IN (“TVl”, “Tuty”);

NULL Value :
The NULL value in a field can be searched in a table using the IS NULL in the
WHERE clause. Example: SELECT * FROM student WHERE place IS NULL;
The Non NULL values in a field can be searched in a table using the IS NOT
NULL in the WHERE clause. Example: SELECT * FROM student WHERE place IS NOT
NULL;
(v)ORDER BY clause
The ORDER BY clause in SQL is used to sort the data in either ascending or
descending based on one or more columns. The ORDER BY clause does not
affect the original table.
1. By default ORDER BY sorts the data in ascending order.
2. We can use the keyword DESC to sort the data in descending order and the
keyword ASC to sort in ascending order.
Syntax:
SELECT <column-name>[,<column-name>,….] FROM <table-name>ORDER BY
<column1>,<column2>,…ASC | DESC ;
Example: SELECT * FROM Student ORDER BY Name;

(vi)WHERE clause:
The WHERE clause is used to filter the records. It helps to extract only those records which satisfy
a given condition
Syntax: SELECT <column-name>[,<column-name>,….] FROM <table-name>WHERE condition>;
Example: SELECT * FROM Student WHERE Place =”Chennai”;

(vii)GROUP BY clause
The GROUP BY clause is used with the SELECT statement to group the table
on rows or columns having identical values or divide the table in to groups. It is
mostly used in conjunction with aggregate functions to produce summary
reports from the database.
Syntax: SELECT <column-names> FROM <table-name> GROUP BY <column-name>HAVING [condition];
Example: SELECT Gender, count(*) FROM Student GROUP BY Gender;

(viii)HAVING clause
The HAVING clause can be used along with GROUP BY clause in the SELECT
statement to place condition on groups and can include aggregate functions on
them.
Syntax: SELECT <column-names> FROM <table-name> GROUP BY <column-name>HAVING [condition];
Example: SELECT Gender , count(*) FROM Student GROUP BY Gender HAVING Place = ‘Chennai’;

7. Relational and Logical operators in Select command:


The relational operators like =, <, <=, >, >=, <> can be used to compare two
values in the SELECT command used with WHERE clause. The logical
operators AND, OR, NOT can also be used to connect search conditions in the WHERE
clause.
Example:
SELECT admno,name FROM student WHERE place=’TVL’;
SELECT admno,name FROM student WHERE place< > ’TVL’;
SELECT admno,name FROM student WHERE age<=18;
SELECT admno,name FROM student WHERE age>=18 AND place=’TVL’;
SELECT admno,name FROM student WHERE palce=’Tuty’ OR place=’TVL’;
SELECT admno,name FROM student WHERE (NOT age>=18);

You might also like