You are on page 1of 7

Hour-1

create database rev2022;


use rev2022;
Create a table StdMaster with the following structure:

SNo Field Type Constraint


1. AdmNo Char(10) Primary Key
2. AdmDate Date
3. Name varchar(20) Not Null
4. Email Varchar(30) Unique
5. Class Int(2)
6. Section Char(1)
7. RNo Int(2)

#Null is not allowed in Primary key field; Null is allowed in Unique field.
1. create table stdmaster
( AdmNo char(10) primary key,
admdate date,
name varchar(20) not null,
email varchar(30) unique,
class int(2), section char(1), rno int(2) );
2. desc stdmaster;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | char(10) | NO | PRI | NULL | |
| admdate | date | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| email | varchar(30) | YES | UNI | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| rno | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+

3. drop table stdmaster


4. create table stdmaster
(AdmNo char(10), admdate date, name varchar(20),
email varchar(30), class int(2), section char(1), rno int(2));

5. desc stdmaster;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | char(10) | YES | | NULL | |
| admdate | date | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| email | varchar(30) | YES | | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| rno | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+

6. alter table stdmaster add primary key (admno);


7. desc stdmaster;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | char(10) | NO | PRI | NULL | |
| admdate | date | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| email | varchar(30) | YES | | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| rno | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
8. alter table stdmaster add unique(email);
9. desc stdmaster;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | char(10) | NO | PRI | NULL | |
| admdate | date | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| email | varchar(30) | YES | UNI | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| rno | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+

10. alter table stdmaster add not null(name);


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'not null(email)' at line 1
11. alter table stdmaster change name name varchar(20) not null;
12. desc stdmaster;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| AdmNo | char(10) | NO | PRI | NULL | |
| admdate | date | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
| email | varchar(30) | YES | UNI | NULL | |
| class | int(2) | YES | | NULL | |
| section | char(1) | YES | | NULL | |
| rno | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
show databases;
13. insert into stdmaster values('200700001','2007-04-01','Abhay','a@b.c',1,'A',1);
14. insert into stdmaster values('200700002','2007-04-01','Azhar','ba@b.c',1,'A',2);
15. insert into stdmaster values('200800012','2008-06-13','Bharat','b@b.c',5,'B',6);
16. insert into stdmaster values('200800007','2008-04-11','Sukhdeep','s@b.c',3,'B',11);
17. insert into stdmaster values('200800009','2008-04-13','Benita','d@b.c',3,'C',4);
18. select * from stdmaster;
+-----------+------------+----------+--------+-------+---------+------+
| AdmNo | admdate | name | email | class | section | rno |
+-----------+------------+----------+--------+-------+---------+------+
| 200700001 | 2007-04-01 | Abhay | a@b.c | 1 | A | 1 |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 |
| 200800007 | 2008-04-11 | Sukhdeep | s@b.c | 3 | B | 11 |
| 200800009 | 2008-04-13 | Benita | d@b.c | 3 | C | 4 |
| 200800012 | 2008-06-13 | Bharat | b@b.c | 5 | B | 6 |
+-----------+------------+----------+--------+-------+---------+------+

19. insert into stdmaster values('200800009','2008-04-13','Benita','d@b.c',3,'C',4);


ERROR 1062 (23000): Duplicate entry '200800009' for key 'PRIMARY'
20. insert into stdmaster (admno) values('200900012');
ERROR 1364 (HY000): Field 'name' doesn't have a default value
21. select * from stdmaster order by class;
22. select * from stdmaster order by class asc;
23. select * from stdmaster order by class desc;
24. select * from stdmaster order by class, name;
25. select * from stdmaster order by class desc, name;
26. select * from stdmaster;
+-----------+------------+----------+--------+-------+---------+------+
| AdmNo | admdate | name | email | class | section | rno |
+-----------+------------+----------+--------+-------+---------+------+
| 200700001 | 2007-04-01 | Abhay | a@b.c | 1 | A | 1 |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 |
| 200800007 | 2008-04-11 | Sukhdeep | s@b.c | 3 | B | 11 |
| 200800009 | 2008-04-13 | Benita | d@b.c | 3 | C | 4 |
| 200800012 | 2008-06-13 | Bharat | b@b.c | 5 | B | 6 |
+-----------+------------+----------+--------+-------+---------+------+
Hour-2
27. select * from stdmaster where class=1;
28. select * from stdmaster where class>1;
29. select * from stdmaster where class >1 and section='B';

30. select * from stdmaster where class in (1,5);


31. select * from stdmaster where class not in (1,5);
32. select * from stdmaster where not class in (1,3);

33. select distinct class from stdmaster;

34. select * from stdmaster where email is null;


35. select * from stdmaster where email is not null;

36. select admno, admdate, email from stdmaster where name like 'A%';
37. select name from stdmaster where name not like '%a';
38. select admno, admdate, email from stdmaster where email not like '_%@%.%_';
39. select admno, admdate, email from stdmaster where email like '%@%@%';

40. select * from stdmaster where section between 'B' and 'C';
41. select * from stdmaster;
+-----------+------------+----------+--------+-------+---------+------+
| AdmNo | admdate | name | email | class | section | rno |
+-----------+------------+----------+--------+-------+---------+------+
| 200700001 | 2007-04-01 | Abhay | a@b.c | 1 | A | 1 |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 |
| 200800007 | 2008-04-11 | Sukhdeep | s@b.c | 3 | B | 11 |
| 200800009 | 2008-04-13 | Benita | d@b.c | 3 | C | 4 |
| 200800012 | 2008-06-13 | Bharat | b@b.c | 5 | B | 6 |
+-----------+------------+----------+--------+-------+---------+------+
42. alter table stdmaster add fee float;
43. select * from stdmaster;
44. update stdmaster set fee=class*100;
45. select * from stdmaster;
46. select admno, fee, fee*0.05 as fine from stdmaster;
47. select max(fee),min(fee),avg(fee),count(fee),sum(fee) from stdmaster;
48. select max(email),min(email),avg(email),count(email),sum(email) from stdmaster;
49. select max(fee),min(fee),avg(fee),count(fee),sum(fee) from stdmaster where class=5;
50. select min(fee)*2 as "Lowest Sch", max(fee)*2 as "Highest Sch" from stdmaster;

51. select section, count(section) from stdmaster group by section;


52. select section, count(section) from stdmaster group by section having count(section)>1;
53. select section, count(section) from stdmaster where name like 'A%' group by section;
54. select section, count(section) from stdmaster where name like 'A%' group by section having
count(section)>1;

Hour-3
Create a table HouseAct with the following structure:

SNo Field Type Constraint


1. AdmNo Char(10) Foreign key references stdmaster(AdmNo)
2. House Varchar(10) Not null
3. Activity varchar(20)

55. create table houseact


(admno char(10), house varchar(10) not null, activity varchar(20),
foreign key (admno) references stdmaster(admno));

56. desc houseact;


+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| admno | char(10) | YES | MUL | NULL | |
| house | varchar(10) | NO | | NULL | |
| activity | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
57. insert into houseact values('200700001','Peace','Dance');
58. insert into houseact values('200700002','Harmony','Music');
59. insert into houseact values('200800009','Hope','Quiz');
60. insert into houseact values('200700002','Harmony','Dance');
61. insert into houseact values('200700001','Peace','Drama');
62. insert into houseact values('200700002','Harmony','Photography');

63. select * from houseact;


64. select * from stdmaster, houseact; #Cartesian product
65. select * from stdmaster natural join houseact; #natural join
+-----------+------------+--------+--------+-------+---------+------+------+---------+-------------+
| AdmNo | admdate | name | email | class | section | rno | fee | house | activity |
+-----------+------------+--------+--------+-------+---------+------+------+---------+-------------+
| 200700001 | 2007-04-01 | Abhay | a@b.c | 1 | A | 1 | 100 | Peace | Dance |
| 200700001 | 2007-04-01 | Abhay | a@b.c | 1 | A | 1 | 100 | Peace | Drama |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 | 100 | Harmony | Music |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 | 100 | Harmony | Dance |
| 200700002 | 2007-04-01 | Azhar | ba@b.c | 1 | A | 2 | 100 | Harmony | Photography |
| 200800009 | 2008-04-13 | Benita | d@b.c | 3 | C | 4 | 300 | Hope | Quiz |
+-----------+------------+--------+--------+-------+---------+------+------+---------+-------------+
66. select h.admno,house,activity,email from stdmaster s, houseact h
where s.admno=h.admno; #Equi-join
+-----------+---------+-------------+--------+
| admno | house | activity | email |
+-----------+---------+-------------+--------+
| 200700001 | Peace | Dance | a@b.c |
| 200700001 | Peace | Drama | a@b.c |
| 200700002 | Harmony | Music | ba@b.c |
| 200700002 | Harmony | Dance | ba@b.c |
| 200700002 | Harmony | Photography | ba@b.c |
| 200800009 | Hope | Quiz | d@b.c |
+-----------+---------+-------------+--------+

67. select sm.admno, name, house, activity from stdmaster sm, houseact ha where
sm.admno=ha.admno;
68. select stdmaster.admno, name, class, section, activity from stdmaster, houseact where
stdmaster.admno=houseact.admno;
69. select stdmaster.admno, name, class, section, activity from stdmaster, houseact where
stdmaster.admno=houseact.admno and activity = 'dance';

70. alter table houseact add grade float;


71. alter table houseact add column level float;
72. desc houseact;

73. alter table houseact drop column level;


74. delete from stdmaster;
75. delete from houseact;
76. show tables;
77. drop table houseact;
78. show tables;
79. drop database rev2022;
80. show databases;
Hour-4
Database Concepts
1. Database: A database is an organised collection of data.
Software used to manage databases are called Data Base Management System (DBMS).
2. Relational Database: A database in which the data is stored in the form of relations (also
called tables) is called a Relational Database. In other words, a Relational Database is a
collection of one or more tables.
3. RDBMS: A DBMS used to manage Relational Databases is called an RDBMS (Relational Data
Base Management System). Some popular RDBMS software available are: Oracle, MySQL,
Sybase, Ingress. MySQL is a free and Open Source RDBMS.
4. Benefits of using a DBMS are:
a. Data Redundancy can be controlled. (Data Redundancy: Having multiple copies of the
same data)
b. Inconsistency can be avoided (Inconsistency: Having multiple mismatching copies of
the same data)
c. Data can be shared among multiple applications.
d. Security restrictions can be applied.
5. MySQL: It is an Open-Source RDBMS Software.
6. Relation/Table: A table refers to a two-dimensional representation of data arranged in
columns (attribute, fields) and rows (records, tuples). The tables in a database are generally
related to each other to facilitate efficient management of the database. Interrelated tables
also reduce the chances of errors in the database.
7. Domain: A set of legal (valid) values for a column is called the domain of that column. For
example, for the column Age of a table storing school students' data, the domain may be {3,
4, 5, …, 20}.
8. Key: A column or a combination of columns which can be used to identify one or more rows
(tuples) in a table is called a key of the table.
9. Primary Key: The group of one or more columns used to uniquely identify each row of a
relation is called its Primary Key. A table cannot have more than one primary key.
10. Candidate Key: A column or a group of columns which can be used as the primary key of a
relation is called a candidate key. A table can have multiple candidate keys.
11. Alternate Key: Candidate keys, other than the primary key, of a table are called its Alternate
Keys.
12. Foreign Key: A Foreign Key is a column or a combination of columns whose values match the
Primary Key in a different table of the same database. Foreign key is used to ensure
referential integrity.
13. Degree: The degree of a table is the number of columns in the table.
14. Cardinality: The cardinality of a table is the number of rows in the table.

Working with MySQL


1. SQL (Structured Query Language): It is the language used to manipulate and manage databases
using an RDBMS.
2. DDL (Data Definition Language): This is a category of SQL commands. All the commands which
are used to create, destroy, or restructure databases and tables fall under this category.
Examples of DDL commands are – CREATE, DROP, ALTER.
3. DML (Data Manipulation Language): This is a category of SQL commands. All the commands
which are used to manipulate data within tables fall under this category. Examples of DML
commands are – INSERT, UPDATE, DELETE.
4. DQL (Data Query Language): This is a category of SQL commands. DQL statements are used
for performing queries on the database tables. SELECT is the only command in this category.

You might also like