CA-01 DBMS LAB Reference Manual
CA-01 DBMS LAB Reference 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.
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.
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
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
• 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.
Database changed
6. To add a new column called “SEM” to the existing table STUDENT and whose default
value is “4”
+-------+-------------+------+-----+---------+-------+
Example:
mysql> DROP TABLE ENROLL1;
Query OK, 0 rows affected (0.08 sec)
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.
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)
2. Delete operations.
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.03 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'.
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.
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.
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
DISTINCT keyword is used to restrict the duplicate rows from the results of a SELECT
statement.
e.g. SELECT DISTINCT name FROM student;
e.g SELECT Roll_no, name, city FROM student WHERE Roll_no LIKE “%3”;
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)
| 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]'.
9.Display the details of the student whose student name starts with letter A.