0% found this document useful (0 votes)
102 views

CA-01 DBMS LAB Reference Manual

MySQL is an open source database management software that allows users to store, organize, and retrieve data through the use of databases, tables, rows, and columns. Key commands for interacting with MySQL databases include CREATE DATABASE to make a new database, USE to select an existing database, SHOW DATABASES to view databases, CREATE TABLE to define a table structure within a database, and INSERT to add rows of data to tables.

Uploaded by

SRI KARTHIK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

CA-01 DBMS LAB Reference Manual

MySQL is an open source database management software that allows users to store, organize, and retrieve data through the use of databases, tables, rows, and columns. Key commands for interacting with MySQL databases include CREATE DATABASE to make a new database, USE to select an existing database, SHOW DATABASES to view databases, CREATE TABLE to define a table structure within a database, and INSERT to add rows of data to tables.

Uploaded by

SRI KARTHIK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CSE 253 DBMS LAB MANUAL

Introduction to MySQL
MySQL is an open source database management software that helps users store, organize, and
retrieve data. It is a very powerful program with a lot of flexibility.

How to Install MySQL on Ubuntu:


Ubuntu: Execute the following command.

sudo apt-get install mysql-server

We can access the MySQL shell by typing the following command into terminal:

mysql -u root -p

After entering the root MySQL password into the prompt , you will be able to start building your
MySQL database.

A MySQL database server contains many databases (or schemas). Each database consists of one
or more tables. A table is made up of columns (or fields) and rows (records).
The SQL keywords and commands are NOT case-sensitive. For clarity, they are shown in
uppercase. The names or identifiers (database names, table names, column names, etc.) are case-
sensitive in some systems, but not in other systems. Hence, it is best to treat identifiers as case-
sensitive.
SHOW DATABASES;
You can use SHOW DATABASES to list all the existing databases in the server.

mysql> SHOW DATABASES;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
........

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 1


CSE 253 DBMS LAB MANUAL

The databases "mysql", "information_schema" and "performance_schema" are system databases


used internally by MySQL.

Summary of MYSQL commands:

Database-Level:
DROP DATABASE databaseName -- Delete the database (irrecoverable!)
DROP DATABASE IF EXISTS databaseName -- Delete if it exists
CREATE DATABASE databaseName -- Create a new database
CREATE DATABASE IF NOT EXISTS databaseName -- Create only if it does not exists
SHOW DATABASES -- Show all the databases in this server
USE databaseName -- Set the default (current) database
SELECT DATABASE() -- Show the default database
SHOW CREATE DATABASE databaseName -- Show the CREATE DATABASE
statement

Table-Level:
DROP TABLE [IF EXISTS] tableName, ...
CREATE TABLE [IF NOT EXISTS] tableName (
columnName columnType columnAttribute, ...
PRIMARY KEY(columnName),
FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae)
)
SHOW TABLES -- Show all the tables in the default database
DESCRIBE|DESC tableName -- Describe the details for a table
ALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN and DROP COLUMN
ALTER TABLE tableName ADD columnDefinition
ALTER TABLE tableName DROP columnName
ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES tableName
(columnNmae)
ALTER TABLE tableName DROP FOREIGN KEY constraintName

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 2


CSE 253 DBMS LAB MANUAL

SHOW CREATE TABLE tableName -- Show the CREATE TABLE statement for this
tableName

Row-Level:
INSERT INTO tableName VALUES (column1Value, column2Value,...) -- Insert on all
Columns
INSERT INTO tableName VALUES (column1Value, column2Value,...), ... -- Insert
multiple rows
INSERT INTO tableName (column1Name, ..., columnNName) VALUES (column1Value, ...,
columnNValue) -- Insert on selected Columns
DELETE FROM tableName WHERE criteria
UPDATE tableName SET columnName = expr, ... WHERE criteria
SELECT * | column1Name AS alias1, ..., columnNName AS aliasN
FROM tableName

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 3


CSE 253 DBMS LAB MANUAL

Experiment No.1 : To Study and Implement Data Definition Language


Commands
Data Definition Language:
Syntax:
CREATE TABLE table_name
(Column_name datatype[(size)],
Column_name datatype[(size)],
)
Example:
CREATE TABLE STUDENT
(SNUM VARCHAR(10),
SNAME VARCHAR(20),
MAJOR VARCHAR(20),
LEVEL VARCHAR(10),
DOB DATE);
• Creates a table with five columns
Data Types in SQL:
Following below are the broad categories of data types exist in most databases:
• String Data
• Numeric Data
• Temporal Data
• Large Objects

i) String Data Types:


a).Fixed Length:
Occupies the same length of space in memory no matter how much data is stored in them.
Syntax:
char(n) where n is the length of the String

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 4


CSE 253 DBMS LAB MANUAL

e.g. name char(50)


If the variable stored for name is ‘Presidency’ the extra 40 fields are padded with blanks
b).Variable Length string is specified with maximum length of characters possible in the
string, however, the allocation is sized to the size of the data stored in memory.
Syntax:
Varchar(n) – n is the maximum length of data possible for the type
There may be a restriction in the maximum length of the data that you can specify in the
declaration which will vary according to the database.
All character data has to be enclosed in single quotes during specification
ii). Numeric Data Types:

• Store all the data related to purely numeric data.

• Some numeric data may also be stored as a character field e.g. zip codes
• Common Numeric Types:
Decimal :Floating point number
Float: Floating point number
Integer(size):Integer of specified length
Money: A number which contains exactly two digits after the decimal point
Number:A standard number field that can hold a floating point data.

iii). Temporal Data Types:


• These represent the dates and time:
• Three basic types are supported:
• Dates
• Times
• Date-Time Combinations
• MySQL comes with the following data types for storing a date or a date/time value in the
database:
• DATE - format YYYY-MM-DD
• DATETIME - format: YYYY-MM-DD HH:MI:SS

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 5


CSE 253 DBMS LAB MANUAL

• TIMESTAMP - format: YYYY-MM-DD HH:MI:SS


• YEAR - format YYYY or YY
iv). Large Data Objects:
• These are used for storing data objects like files and images:
• There are two types:
• Character Large Objects (clobs)
• Binary Large Objects (blobs)

1. To create a new database called STUDENTDB


mysql> CREATE DATABASE STUDENTDB;
Query OK, 1 row affected (0.12 sec)

2. To display the databases;


mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| studentdb |
| test |
+--------------------+
5 rows in set (0.02 sec)

3. To use the created database STUDENTDB


mysql> USE STUDENTDB;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 6


CSE 253 DBMS LAB MANUAL

Database changed

mysql> CREATE TABLE STUDENT(


-> SNUM INT PRIMARY KEY,
-> SNAME VARCHAR(20) NOT NULL,
-> MAJOR VARCHAR(5) NOT NULL,
-> LEVEL CHAR(5),
-> DOB DATE);
Query OK, 0 rows affected (0.13 sec)

4. To display/view all the tables in the STUDENTDB database.


mysql> SHOW TABLES;
+---------------------+
| Tables_in_studentdb |
+---------------------+
| student |
+---------------------+
1 row in set (0.00 sec)

5. To display the table description/schema/intension/structure of the database


mysql> DESC STUDENT;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11)| NO | PRI | NULL | |
| SNAME | varchar(20) | NO | | NULL | |
| MAJOR | varchar(5) | NO | | NULL | |
| LEVEL | char(5) | YES | | NULL | |

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 7


CSE 253 DBMS LAB MANUAL

| DOB | date | YES | | NULL | |


+-------+-------------+------+-----+---------+-------+
5 rows in set (0.08 sec)

6. To add a new column called “SEM” to the existing table STUDENT and whose default
value is “4”

mysql> ALTER TABLE STUDENT ADD SEM INT DEFAULT 4;


Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC STUDENT;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | NO | PRI | NULL | |
| SNAME | varchar(20) | NO | | NULL | |
| MAJOR | varchar(5) | NO | | NULL | |
| LEVEL | char(5) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| SEM | int(11) | YES | |4 | |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.13 sec)

7. To modify the definition of a column of the existing table STUDENT

mysql> ALTER TABLE STUDENT MODIFY MAJOR VARCHAR(20) NULL;


Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 8


CSE 253 DBMS LAB MANUAL

mysql> DESC STUDENT;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | NO | PRI | NULL | |
| SNAME | varchar(20) | NO | | NULL | |
| MAJOR | varchar(20) | YES | | NULL | |
| LEVEL | char(5) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| SEM | int(11) | YES | |4 | |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.03 sec)

8. To make other column as primary key(unique) in an existing table STUDENT


mysql> ALTER TABLE STUDENT MODIFY SNAME VARCHAR(20) UNIQUE;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC STUDENT;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | NO | PRI | NULL | |
| SNAME | varchar(20) | YES | UNIQUE | NULL | |
| MAJOR | varchar(20) | YES | | NULL | |
| LEVEL | char(5) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| SEM | int(11) | YES | |4 | |

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 9


CSE 253 DBMS LAB MANUAL

+-------+-------------+------+-----+---------+-------+

9. To drop a column in an already created table.

mysql> ALTER TABLE STUDENT DROP COLUMN LEVEL;


Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC STUDENT;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | NO | PRI | NULL | |
| SNAME | varchar(20) | YES | UNI | NULL | |
| MAJOR | varchar(20) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| SEM | int(11) | YES | |4 | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

10. Truncating the tables.


Syntax:
Truncate table <tablename>;
mysql> TRUNCATE TABLE ENROLL1;
Query OK, 0 rows affected (0.11 sec)

11. To delete the definition/schema/description/structure of a table.


Syntax:
Drop table <tablename>;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 10


CSE 253 DBMS LAB MANUAL

Example:
mysql> DROP TABLE ENROLL1;
Query OK, 0 rows affected (0.08 sec)

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 11


CSE 253 DBMS LAB MANUAL

Experiment No.2: To Study and Implement Data Manipulation Language


commands:
Data Manipulation Language:
i)Insert Statement:
Allows you to add new records to the Table
Syntax:
insert into table_name[(column_list)] values (value_list)
Example:
INSERT INTO student VALUES (1, ‘Ganesh’, ‘CSE’, ‘JR’, ’2000-05-01’)
INSERT INTO Student (snum, sname, major, level, DOB) VALUES (2, ‘ramesh’, ‘CSE’, ‘SR’,
’2000-07-31’))
• Note1: If the columns are not specified as in the first example the data goes in the order
specified in the table
• Note2: There are two ways of inserting Null values
1. If the field has a default value of Null, you can use an Insert statement that ignores the
column where the value is to be Null.
2. You can specify the column in the column list specification and assign a value of Null
to the corresponding value field.

ii)Delete Statement:
It is used to remove records from a table of the database. The where clause in the syntax is used to
restrict the rows deleted from the table otherwise all the rows from the table are deleted.
Syntax: DELETE FROM table_name [WHERE Condition]
Example:
DELETE FROM STUDENT
WHERE SNAME = ‘Ramesh’
• Deletes all the rows where the sname is ‘Ramesh’ keeps all the other rows.

iii) Update Statement:

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 12


CSE 253 DBMS LAB MANUAL

It is used to make changes to existing rows of the table. It has three parts. First, you ,must specify
which table is going to be updated. The second part of the statement is the set clause, in which you
should specify the columns that will be updated as well as the values that will be inserted. Finally,
the where clause is used to specify which rows will be updated.
Syntax:
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, …..
[WHERE Condition]
Example:
UPDATE STUDENT SET SNAME = ‘Vignesh’, MAJOR = ‘IS’
WHERE snum = 1;

1. Inserting Data into Tables: - once a table is created the most natural thing to do is load this
table with data to be manipulated later.
Syntax:
i)insert into <tablename> (<col1>,<col2>) values(<exp>,<exp>);
mysql> INSERT INTO STUDENT(SNUM,SNAME,MAJOR,DOB,SEM) VALUES
(1001,'CHETHAN','CSE','2000-05-03',4);
Query OK, 1 row affected (0.07 sec)

ii) insert into <tablename> values(<exp>,<exp>);


mysql> INSERT INTO STUDEN VALUES (1002,'KIRAN','CSE','2000-05-03',4);
Query OK, 1 row affected (0.07 sec)

2. Delete operations.

i) To remove all rows of a table


Syntax: delete from <tablename>;
mysql> DELETE FROM STUDENT;
Query OK, 1 row affected (0.09 sec)

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 13


CSE 253 DBMS LAB MANUAL

ii) removal of a specified row/s


mysql> DELETE FROM STUDENT WHERE SNUM=1001;
Query OK, 1 row affected (0.10 sec)

3. Updating the contents of a table.


i) updating all rows
Syntax: Update <tablename> set <col>=<exp>,<col>=<exp>;
Before updating the snapshot of Student Table
mysql> SELECT * FROM STUDENT;
+------+----------------+-------+------------+------+
| SNUM | SNAME | MAJOR | DOB | SEM |
+------+----------------+-------+------------+------+
| 1001 | CHETHAN | CSE | 2000-11-03 | 4 |
| 1002 | CHETHAN CHAVAN | CSE | 2000-11-03 | 4 |
+------+----------------+-------+------------+------+
2 rows in set (0.00 sec)

mysql> UPDATE STUDENT SET MAJOR='ISE';


Query OK, 2 rows affected (0.07 sec)
Rows matched: 2 Changed: 2 Warnings: 0
After updating the snapshot of Student Table
mysql> SELECT * FROM STUDENT;
+------+----------------+-------+------------+------+
| SNUM | SNAME | MAJOR | DOB | SEM |
+------+----------------+-------+------------+------+
| 1001 | CHETHAN | ISE | 2000-11-03 | 4 |
| 1002 | CHETHAN CHAVAN | ISE | 2000-11-03 | 4 |
+------+----------------+-------+------------+------+

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 14


CSE 253 DBMS LAB MANUAL

2 rows in set (0.00 sec)

ii) updating seleted records.


Syntax: Update <tablename> set <col>=<exp>, <col>=<exp> where <condition>;
mysql> UPDATE STUDENT SET MAJOR='ECE' WHERE SNUM=1001;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 15


CSE 253 DBMS LAB MANUAL

Experiment No.3: To Study and Implement SQL Constraints

Types of Constraints in SQL


• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
• FOREIGN KEY - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column when no value is specified

1. To create a new table Faculty with FID auto_increment constraint

mysql> CREATE TABLE FACULTY


-> ( FID INT PRIMARY KEY AUTO_INCREMENT,
-> FNAME VARCHAR(20) NOT NULL,
-> ADDRESS VARCHAR(20),
-> DEPTID INT);
Query OK, 0 rows affected (0.13 sec)

mysql> DESC FACULTY;


+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| FID | int(11) | NO | PRI | NULL | auto_increment |
| FNAME | varchar(20) | NO | | NULL | |
| ADDRESS | varchar(20) | YES | | NULL | |
| DEPTID | int(11) | YES | | NULL | |

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 16


CSE 253 DBMS LAB MANUAL

+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)

2. To create a relationship table called COURSE with ON DELETE CASCADE Referential


Integrity Constraint.
mysql> CREATE TABLE COURSE (
-> CNAME VARCHAR(10) PRIMARY KEY,
-> MEETS_AT VARCHAR(10),
-> ROOM VARCHAR(5),
-> FID INTEGER,
-> FOREIGN KEY(FID) REFERENCES FACULTY(FID) ON DELETE CASCADE);
Query OK, 0 rows affected (0.13 sec)
mysql> DESC COURSE;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| CNAME | varchar(10) | NO | PRI | NULL | |
| MEETS_AT | varchar(10) | YES | | NULL | |
| ROOM | varchar(5) | YES | | NULL | |
| FID | int(11) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.03 sec)

3. To CREATE a Many to Many relationship table “ENROLL” between STUDENT and


COURSE relations with ON DELETE CASCADE

mysql> CREATE TABLE ENROLL(


-> SNUM INTEGER,CNAME VARCHAR(10),
-> PRIMARY KEY(SNUM,CNAME),

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 17


CSE 253 DBMS LAB MANUAL

-> FOREIGN KEY(SNUM) REFERENCES STUDENT(SNUM) ON DELETE


CASCADE,
-> FOREIGN KEY(CNAME) REFERENCES COURSE(CNAME) ON DELETE
CASCADE);
Query OK, 0 rows affected (0.15 sec)

mysql> DESC ENROLL;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | NO | PRI | 0 | |
| CNAME | varchar(10) | NO | PRI | | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

4. To CREATE a Many to Many relationship table “ENROLL” between STUDENT and


COURSE relations with ON DELETE SET NULL ON UPDATE CASCADE

mysql> CREATE TABLE ENROLL1(


-> SNUM INTEGER,CNAME VARCHAR(10),
-> FOREIGN KEY (SNUM) REFERENCES STUDENT(SNUM) ON DELETE SET NULL
ON UPDATE CASCADE,
-> FOREIGN KEY (CNAME) REFERENCES COURSE(CNAME) ON DELETE SET NULL
ON UPDATE CASCADE);
Query OK, 0 rows affected (0.14 sec)
mysql> DESC ENROLL1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SNUM | int(11) | YES | MUL | NULL | |

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 18


CSE 253 DBMS LAB MANUAL

| CNAME | varchar(10) | YES | MUL | NULL | |


+-------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec)

i)Entity Integrity Constraint: Primary Key Value cannot be NULL and duplicate.
mysql> INSERT INTO STUDENT VALUES (NULL,'CHETHAN','CSE','2000-20-03',4);
ERROR 1048 (23000): Column 'SNUM' cannot be null.
mysql> INSERT INTO STUDENT(SNUM,SNAME,MAJOR,DOB,SEM) VALUES
(1001,'CHETHAN','CSE','2000-05-03',4);
ERROR 1062 (23000): Duplicate entry '1001' for key 'PRIMARY'.

ii)DOMAIN Constraint: incorrect date format(‘YYYY-MM-DD’)


mysql> INSERT INTO STUDENT VALUES (1002,'CHETHAN','CSE','2000-20-03',4);
ERROR 1292 (22007): Incorrect date value: '2000-20-03' for column 'DOB' at row 1
mysql> INSERT INTO COURSE VALUES('PYTHON','10:05','DGL0123',123);
ERROR 1406 (22001): Data too long for column 'ROOM' at row 1

iii)Referential Integrity Constraint:


mysql> INSERT INTO COURSE VALUES('PYTHON','10:05','DGL01',123);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`studentdb`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`FID`) REFERENCES
`faculty` (`FID`) ON DELETE CASCADE).

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 19


CSE 253 DBMS LAB MANUAL

Experiment No. 4: To study and implement SQL data retrieval using SELECT,
FROM and WHERE clause.
To retrieve information from a database we can query the databases. SQL SELECT statement is
used to select rows and columns from a database/relation
SELECT Command
This command can perform selection as well as projection.
Selection: This is the capability of SQL can return you the tuples form a relation with all the
attributes.
Projection:This is the capability of SQL to return only specific attributes in the relation.

Data Retrieval/Manipulation Commands:

Operators in SQL:
The following are the commonly used operators in SQL
Arithmetic Operators +, -, *, /
Relational Operators =, <, >, <=, >=, <>
Logical Operators OR, AND, NOT
Arithmetic operators are used to perform simple arithmetic operations.

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 20


CSE 253 DBMS LAB MANUAL

Relational Operators are used when two values are to be compared and
Logical operators are used to connect search conditions in the WHERE Clause in SQL.

SELECT Command:
SELECT count(*) AS “Total Number of Records” FROM student;
Display the total number of records with title as “Total Number of Records” i.e an alias

We can also use arithmetic operators in select statement, like


SELECT Roll_no, name, marks+20 FROM student;
SELECT name, (marks/500)*100 FROM student WHERE Roll_no > 103;

Eliminating Duplicate/Redundant data:

DISTINCT keyword is used to restrict the duplicate rows from the results of a SELECT
statement.
e.g. SELECT DISTINCT name FROM student;

Conditions based on a range


SQL provides a BETWEEN operator that defines a range of values that the column value must fall
for the condition to become true.
e.g. SELECT Roll_no, name FROM student WHERE Roll_no BETWENN 100 AND 103;
The above command displays Roll_no and name of those students whose Roll_no lies in the range
100 to 103 (both 100 and 103 are included in the range).

Conditions based on Pattern


SQL provides two wild card characters that are used while comparing the strings with LIKE
operator.
percent ( % ) Matches any string
Underscore ( _ ) Matches any one character

e.g SELECT Roll_no, name, city FROM student WHERE Roll_no LIKE “%3”;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 21


CSE 253 DBMS LAB MANUAL

displays those records where last digit of Roll_no is 3 and may have any number of characters in
front.
Illustration:
1. Viewing data in the tables: - once data has been inserted into a table, the next most logical
operation would be to view what has been inserted.
a) all rows and all columns
Syntax: Select <col> to <col n> from tablename;
mysql> SELECT * FROM STUDENT;
+------+----------------+-------+------------+------+
| SNUM | SNAME | MAJOR | DOB | SEM |
+------+----------------+-------+------------+------+
| 1001 | CHETHAN | ECE | 2000-11-03 | 4 |
| 1002 | CHETHAN CHAVAN | ISE | 2000-11-03 | 4 |
+------+----------------+-------+------------+------+
2 rows in set (0.00 sec)

2. Filtering table data: - while viewing data from a table, it is rare that all the data from table will
be required each time. Hence, sql must give us a method of filtering out data that is not required
data.
a) All columns and all rows:
Syntax: select <col1>,<col2> from <tablename>;
mysql> SELECT * FROM STUDENT;
+------+----------------+-------+------------+------+
| SNUM | SNAME | MAJOR | DOB | SEM |
+------+----------------+-------+------------+------+
| 1001 | CHETHAN | ECE | 2000-11-03 | 4 |
| 1002 | CHETHAN CHAVAN | ISE | 2000-11-03 | 4 |
+------+----------------+-------+------------+------+
2 rows in set (0.00 sec)

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 22


CSE 253 DBMS LAB MANUAL

b) selected rows and all columns:


Syntax:
select * from <tablename> where <condition>;

mysql> SELECT * FROM STUDENT WHERE SNUM=1001;


+------+---------+-------+------------+------+
| SNUM | SNAME | MAJOR | DOB | SEM |
+------+---------+-------+------------+------+
| 1001 | CHETHAN | ECE | 2000-11-03 | 4 |
+------+---------+-------+------------+------+
1 row in set (0.04 sec)
c) selected columns and selected rows
Syntax: select <col1>,<col2> from <tablename> where<condition>;
mysql> SELECT SNUM,SNAME FROM STUDENT WHERE SNUM=1001;
+------+---------+
| SNUM | SNAME |
+------+---------+
| 1001 | CHETHAN |
+------+---------+
1 row in set (0.00 sec)

Example: STUDENT Database:


TABLE CREATION:

mysql> CREATE TABLE STUDENT


(SNUM INTEGER PRIMARY KEY,
SNAME VARCHAR(10),
MAJOR VARCHAR(10),
LEVEL VARCHAR(10),
AGE INTEGER);

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 23


CSE 253 DBMS LAB MANUAL

mysql> CREATE TABLE FACULTY


(FID INTEGER PRIMARY KEY,
FNAME VARCHAR(10),
DEPTID INTEGER);

mysql> CREATE TABLE CLASS


(CNAME VARCHAR(10) PRIMARY KEY,
MEETS_AT VARCHAR(10), ROOM VARCHAR(5),
FID INTEGER, FOREIGN KEY (FID) REFERENCES FACULTY (FID) ON DELETE
CASCADE);

mysql> CREATE TABLE ENROLLED


(SNUM INTEGER, CNAME VARCHAR(10),
PRIMARY KEY(SNUM, CNAME),
FOREIGN KEY(SNUM) REFERENCES STUDENT (SNUM) ON DELETE CASCADE,
FOREIGN KEY(CNAME) REFERENCES CLASS (CNAME) ON DELETE CASCADE);

mysql> INSERT INTO STUDENT VALUES(101,'ABHI','CSE','JR',19);


mysql> INSERT INTO STUDENT VALUES(102,'ANIL','ISE','JR',18);
mysql> INSERT INTO STUDENT VALUES(103,'BHAVYA','ISE','SR',20);
mysql> INSERT INTO STUDENT VALUES(104,'CHETHAN','CSE','JR',19);
mysql> INSERT INTO STUDENT VALUES(105,'SURESH','MECH','JR',18);
mysql> INSERT INTO STUDENT VALUES(106,'JAYANTH','CSE','SR',20);

mysql> SELECT * FROM STUDENT;


+---------+-------------------+----------+-----------+------+
| SNUM | SNAME | MAJOR | LEVEL | AGE |
+-------- +-------------------+-----------+----------+------+
| 101 | ABHI | CSE | JR | 19 |
| 102 | ANIL | ISE | JR | 18 |
| 103 | BHAVYA | ISE | SR | 20 |
| 104 | CHETHAN | CSE | JR | 19 |
| 105 | SURESH | MECH | JR | 18 |
| 106 | JAYANTH | CSE | SR | 20 |
+--------+--------------------+-----------+---------+------+

mysql> INSERT INTO FACULTY VALUES(1001,'JAMES',41);


mysql> INSERT INTO FACULTY VALUES(1002,'SUNIL',42);
mysql> INSERT INTO FACULTY VALUES(1002,'SUKRUTH',43);

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 24


CSE 253 DBMS LAB MANUAL

mysql> INSERT INTO FACULTY VALUES(1003,'SUKRUTH',43);


mysql> INSERT INTO FACULTY VALUES(1004,'NARASIMHA',44);
mysql> INSERT INTO FACULTY VALUES(1005,'AFROZ',41);
mysql> INSERT INTO FACULTY VALUES(1006,'JOHN',42);
mysql> INSERT INTO FACULTY VALUES(1007,'AHMED',45);
mysql> INSERT INTO FACULTY VALUES(1008,'SUNITHA',46);
mysql> INSERT INTO FACULTY VALUES(1009,'SRIVINAY',42);

mysql> SELECT * FROM FACULTY;


+------+-------------------+-----------+
| FID | FNAME | DEPTID |
+------+-------------------+-----------+
| 1001 | JAMES | 41 |
| 1002 | SUNIL | 42 |
| 1003 | SUKRUTH | 43 |
| 1004 | NARASIMHA | 44 |
| 1005 | AFROZ | 41 |
| 1006 | JOHN | 42 |
| 1007 | AHMED | 45 |
| 1008 | SUNITHA | 46 |
| 1009 | SRIVINAY | 42 |
+------+------------------+-----------+
9 rows in set (0.00 sec)

mysql> INSERT INTO CLASS VALUES('4CSE1','9:00','NG01',1001);


mysql> INSERT INTO CLASS VALUES('4CSE2','10:00','NG02',1001);
mysql> INSERT INTO CLASS VALUES('4CSE3','11:15','NG03',1002);
mysql> INSERT INTO CLASS VALUES('4CSE4','12:10','NG04',1002);
mysql> INSERT INTO CLASS VALUES('4CSE5','1:05','NG05',1003);
mysql> INSERT INTO CLASS VALUES('4CSE6','1:05','NG06',1004);
mysql> INSERT INTO CLASS VALUES('4CSE7','2:05','KG01',1005);
mysql> INSERT INTO CLASS VALUES('4CSE8','3:00','KG02',1006);
mysql> INSERT INTO CLASS VALUES('4CSE9','9:00','KG03',1007);
mysql> INSERT INTO CLASS VALUES('4CSE10','10:00','KG04',1008);
mysql> INSERT INTO CLASS VALUES('4CSE11','10:00','KF04',1009);

mysql> INSERT INTO CLASS VALUES('4CSE12','10:00','KF03',1009);


mysql> SELECT * FROM CLASS;
+--------+----------+------+------+

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 25


CSE 253 DBMS LAB MANUAL

| CNAME | MEETS_AT | ROOM | FID |


+--------+----------+------+------+
| 4CSE1 | 9:00 | NG01 | 1001 |
| 4CSE10 | 10:00 | KG04 | 1008 |
| 4CSE11 | 10:00 | KF04 | 1009 |
| 4CSE12 | 10:00 | KF03 | 1009 |
| 4CSE2 | 10:00 | NG02 | 1001 |
| 4CSE3 | 11:15 | NG03 | 1002 |
| 4CSE4 | 12:10 | NG04 | 1002 |
| 4CSE5 | 1:05 | NG05 | 1003 |
| 4CSE6 | 1:05 | NG06 | 1004 |
| 4CSE7 | 2:05 | KG01 | 1005 |
| 4CSE8 | 3:00 | KG02 | 1006 |
| 4CSE9 | 9:00 | KG03 | 1007 |
+--------+----------+------+------+
12 rows in set (0.00 sec)

mysql> INSERT INTO ENROLLED VALUES(101,'4CSE1');


mysql> INSERT INTO ENROLLED VALUES(102,'4CSE2');
mysql> INSERT INTO ENROLLED VALUES(103,'4CSE3');
mysql> INSERT INTO ENROLLED VALUES(101,'4CSE4');
mysql> INSERT INTO ENROLLED VALUES(104,'4CSE5');
mysql> INSERT INTO ENROLLED VALUES(105,'4CSE5');
mysql> INSERT INTO ENROLLED VALUES(106,'4CSE5');

mysql> INSERT INTO ENROLLED VALUES(101,'4CSE6');


mysql> INSERT INTO ENROLLED VALUES(102,'4CSE7');
mysql> INSERT INTO ENROLLED VALUES(103,'4CSE8');
mysql> INSERT INTO ENROLLED VALUES(104,'4CSE9');
mysql> INSERT INTO ENROLLED VALUES(105,'4CSE10');
mysql> INSERT INTO ENROLLED VALUES(106,'4CSE11');
mysql> INSERT INTO ENROLLED VALUES(106,'4CSE12');
mysql> SELECT * FROM ENROLLED;
+------+---------------+
| SNUM | CNAME |
+------+---------------+
| 101 | 4CSE1 |
| 105 | 4CSE10 |
| 106 | 4CSE11 |
| 106 | 4CSE12 |
| 102 | 4CSE2 |
| 103 | 4CSE3 |
| 101 | 4CSE4 |

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 26


CSE 253 DBMS LAB MANUAL

| 104 | 4CSE5 |
| 105 | 4CSE5 |
| 106 | 4CSE5 |
| 101 | 4CSE6 |
| 102 | 4CSE7 |
| 103 | 4CSE8 |
| 104 | 4CSE9 |
+------+--------------+
14 ows in set (0.00 sec)

Basic Queries:
1. Add the columns 'Fees' & 'Email' to the STUDENT table with default value '30000' &
'[email protected]'.

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 27


CSE 253 DBMS LAB MANUAL

2. Update the fees & email of students with different values.

3. Display the Average Fees of students department-wise.

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 28


CSE 253 DBMS LAB MANUAL

4. Find the names of students having fees between 25000 to 30000.

5. Find the names of students having domain 'gmail.com'

6. Display Names of students in CAPITAL Letters.

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 29


CSE 253 DBMS LAB MANUAL

7. Increase the fees of all students by 10%;

UPDATE STUDENT SET FEES=FEES*1.1;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 30


CSE 253 DBMS LAB MANUAL

UPDATE STUDENT SET FEES=FEES-FEES/11;

SELECT SNUM, FEES AS OLD_FEE, FEES+2000 AS NEW_FEE FROM STUDENT;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 31


CSE 253 DBMS LAB MANUAL

8.Display the details of the student whose email id is missing.(NULL)


SELECT * FROM STUDENT WHERE EMAILID IS NULL;

Other than NULL:


SELECT * FROM STUDENT WHERE EMAILID IS NOT NULL;

9.Display the details of the student whose student name starts with letter A.

10.Delete the first two records of a student table.

DELETE FORM STUDENT LIMIT 2;

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 32


CSE 253 DBMS LAB MANUAL

Note: Nested and Complex Queries will be implemented in Experiment No.9

Department of CS&E, SOE, PRESIDENCY UNIVERSITY 33

You might also like