SQL COMMANDS for Lab Record
I. Create a table student with attributes like sno, sname, marks, address and city
mysql> create table student(sno int(3), sname varchar(20), marks int(4),address varchar(10),city
varchar(10));
Query OK, 0 rows affected
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sno | int(3) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| marks | int(4) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
| city | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set
II. Create a table student with attributes like sno, sname, marks, address and city , set sno as
primary key and not null. Sname is not null.
mysql> create table stud_new(sno int(3) not null primary key, sname varchar(20)
not null, gender char(1), marks int(3),dob date);
Query OK, 0 rows affected
mysql> desc stud_new;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sno | int(3) | NO | PRI | NULL | |
| sname | varchar(20) | NO | | NULL | |
| gender | char(1) | YES | | NULL | |
| marks | int(3) | YES | | NULL | |
| dob | date | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
III. Insert the values into student table which we created earlier.
mysql> insert into stud_new values(1,'anil','m',456,'1990-09-9');
Query OK, 1 row affected
mysql> insert into stud_new values(2,'arul','f',476,'1991-09-9');
Query OK, 1 row affected
mysql> insert into stud_new values(3,'aravinda','f',376,'1990-07-9');
Query OK, 1 row affected
mysql> insert into stud_new values(4,'aravind','m',499,'1990-03-29'),(5,'anand',
'm',423,'2000-01-09'),(6,'nandini','f',432,'1999-12-31');
IV. View the STUDENT table data
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 456 | 1990-09-09 |
| 2 | arul |f | 476 | 1991-09-09 |
| 3 | aravinda | f | 376 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 432 | 1999-12-31 |
+-----+----------+--------+-------+------------+
V. Update the student table with new marks for student number “6”
mysql> update stud_new set marks=455 where sno=6;
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 456 | 1990-09-09 |
| 2 | arul |f | 476 | 1991-09-09 |
| 3 | aravinda | f | 376 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 455 | 1999-12-31 |
+-----+----------+--------+-------+------------+
6 rows in set
VI. Add five extra marks to the student whose student number is ‘1’
mysql> update stud_new set marks=marks+5 where sno=1;
Query OK, 1 row affected
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 461 | 1990-09-09 |
| 2 | arul |f | 476 | 1991-09-09 |
| 3 | aravinda | f | 376 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 455 | 1999-12-31 |
+-----+----------+--------+-------+------------+
VII. Update the student table marks by adding eight to original marks to roll number three or six
mysql> update stud_new set marks=marks+8 where (sno=3 or sno=6);
Query OK, 1 row affected
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 460 | 1990-09-09 |
| 2 | arul |f | 476 | 1991-09-09 |
| 3 | aravinda | f | 384 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 455 | 1999-12-31 |
VIII. Delete the record of a particular student whose name is “arul”
mysql> delete from stud_new where sname="arul";
Query OK,
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 460 | 1990-09-09 |
| 3 | aravinda | f | 384 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 455 | 1999-12-31 |
+-----+----------+--------+-------+------------+
IX. Display the student name, number and gender of student table
mysql> select sname,sno,gender from stud_new;
+----------+-----+--------+
| sname | sno | gender |
+----------+-----+--------+
| anil | 1|m |
| aravinda | 3 | f |
| aravind | 4 | m |
| anand | 5 | m |
| nandini | 6 | f |
+----------+-----+--------+
X. Display the student name, number and gender of student table where the gender is female.
mysql> select sname,sno,gender from stud_new where gender='f';
+----------+-----+--------+
| sname | sno | gender |
+----------+-----+--------+
| aravinda | 3 | f |
| nandini | 6 | f |
+----------+-----+--------+
XI. Use of between in sql command
mysql> select sname,sno,gender,dob from stud_new where dob between '1990-03-01'
and '1990-12-30';
+----------+-----+--------+------------+
| sname | sno | gender | dob |
+----------+-----+--------+------------+
| anil | 1|m | 1990-09-09 |
| aravinda | 3 | f | 1990-07-09 |
| aravind | 4 | m | 1990-03-29 |
+----------+-----+--------+------------+
XII. Entering “null values” if the value of a record is not known to the user.
mysql> insert into stud_new values(7,'aravind','m',499,null);
Query OK, 1 row affected
mysql> insert into stud_new values(8,'arun','m',null,null);
Query OK, 1 row affected (0.03 sec)
mysql> insert into stud_new values(9,'aruna','m',null,null);
Query OK,
mysql> select * from stud_new;
+-----+----------+--------+-------+------------+
| sno | sname | gender | marks | dob |
+-----+----------+--------+-------+------------+
| 1 | anil |m | 460 | 1990-09-09 |
| 2 | arul |f | 476 | 1991-09-09 |
| 3 | aravinda | f | 384 | 1990-07-09 |
| 4 | aravind | m | 499 | 1990-03-29 |
| 5 | anand | m | 423 | 2000-01-09 |
| 6 | nandini | f | 455 | 1999-12-31 |
| 7 | aravind | m | 499 | NULL |
| 8 | arun |m | NULL | NULL |
| 9 | aruna | m | NULL | NULL |
+-----+----------+--------+-------+------------+
XIII. DDL COMMANDS to create database and tables
1. To view the databases in our computer (mysql client version)
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| 11B |
| 11astudents |
| mysql |
| student |
| DPS |
| test |
| xiic_students |
| xiiBstudents |
+---------------------+
2. To create the databases in our computer with our name (mysql client version)
mysql> create database db_dps;
Query OK,
3. To delete the databases from our computer
mysql> drop database db_dps;
Query OK,
4. To use the databases in our computer
mysql> use db_dps;
Query OK,
5.Delete a table
mysql> drop table stud_new;
Query OK,
6.Viewing the tables in a database
mysql> show tables;
+--------------+
| Tables_in_db |
+--------------+
| new_student |
+--------------+
XIV. Applying Alter command on student table
1. Adding a new column”mobileno” to stud_new table
mysql> Alter table stud_new add ( mobile_no integer(12) );
Query OK,
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sno | int(3) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| marks | int(4) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
| city | varchar(10) | YES | | NULL | |
| mobile_no | int(12) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2. Adding a new column ”COURSE” to stud_new table with a default value “MPC”.
mysql> Alter table stud_new add ( course varchar(10) DEFAULT ‘mpc’ );
Query OK,
3. Removing a column
mysql> alter table stud_new drop(city);
Query OK,
4. rename an old column ”mobileno” to “phone” of stud_new table
mysql> Alter table stud_new rename mobile_no to phone int(15) ;
Query OK,
*****************