LN 12
LN 12
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
DML – (DATA MANIPULATION LANGUAGE) component of SQL is used to insert values in to table.
Example:
INSERT INTO student VALUES (107, 'Bala', 20, 'Cochin');
Part - III
Answer the following questions: (3 Marks)
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);
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).
(ii) To display all employees whose allowance is between 5000 and 7000.
SELECT * FROM employee WHERE allowance BETWEEN 5000 AND 7000;
(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;
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;
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’;