The document provides a step-by-step guide on creating and managing a MySQL database named 'school', including creating a 'student' table, inserting records, modifying table structure, and performing various SQL operations. It details commands for displaying table structures, updating records, and deleting both records and the database itself. The document concludes with the deletion of the 'school' database, indicating the completion of the operations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views
IP My Sql class 11
The document provides a step-by-step guide on creating and managing a MySQL database named 'school', including creating a 'student' table, inserting records, modifying table structure, and performing various SQL operations. It details commands for displaying table structures, updating records, and deleting both records and the database itself. The document concludes with the deletion of the 'school' database, indicating the completion of the operations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14
MYSQL REVISION
1.CREATE A DATABASE SCHOOL
mysql> create database school; 2.Open the database school. mysql> use school; 3.Create a table student with the following specification FieldName FieldType Size Constraint Roll Int Primary key Name Varchar 20 Not Null Marks Decimal Dob date
mysql> create table student(roll int primary key,name
varchar(20)not null,marks decimal, -> dob date,fees double(16,2)); 3.Show the structure of the table student. mysql> desc student; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | roll | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | marks | decimal(10,0) | YES | | NULL | | | dob | date | YES | | NULL | | | fees | double(16,2) | YES | | NULL | | +-------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) 4.Show the contents of the table student. mysql> select * from student; Empty set (0.00 sec) 5.Inserting 1 record into the table student. mysql> insert into student values(1,'Ram',95.5,'1996-03- 23',45000); 6.inserting a record into table student wuth the null values in fees column. mysql> insert into student values(2,'Shyam',99.5,'1997-03- 23',NULL); 7.Inserting values only in specific columns like roll and name column. mysql> insert into student(Roll,Name)values(3,'Hari'); 8.displaying contents of the table student. mysql> select * from student; +------+-------+-------+------------+----------+ | roll | name | marks | dob | fees | +------+-------+-------+------------+----------+ | 1 | Ram | 96 | 1996-03-23 | 45000.00 | | 2 | Shyam | 100 | 1997-03-23 | NULL | | 3 | Hari | NULL | NULL | NULL | +------+-------+-------+------------+----------+ 3 rows in set (0.00 sec) 9.adding a unique key constraint in to the column marks after creating the table. mysql> alter table student add unique key(marks); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 10.inserting a null value in the unique key column marks as u know unique key allows NULL value. mysql> insert into student values(4,'Govind',NULL,'1999-08- 25',NULL); 11.display the contents of the table student.(that means displaying all the columns) mysql> select * from student; +------+--------+-------+------------+----------+ | roll | name | marks | dob | fees | +------+--------+-------+------------+----------+ | 1 | Ram | 96 | 1996-03-23 | 45000.00 | | 2 | Shyam | 100 | 1997-03-23 | NULL | | 3 | Hari | NULL | NULL | NULL | | 4 | Govind | NULL | 1999-08-25 | NULL | +------+--------+-------+------------+----------+ 4 rows in set (0.00 sec) 12.Change the name of the column dob to date_of_birth mysql>Alter table student change dob date_of_birth date; 13.diaplay the names of all the database mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | 11a1_2023 | | bank | | classa1 | | demo | | exam | | ip | | loans | | mk | | mysql | | ronit | | school | | school1 | | student | | test | | tirth | +--------------------+ 16 rows in set (0.02 sec) 14.Open the database school. mysql> use school; Database changed 15.display all the table names present in the database school. mysql> show tables; +------------------+ | Tables_in_school | +------------------+ | student | +------------------+ 1 row in set (0.00 sec) 16.display the structure of the table student mysql> desc student; +---------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------+------+-----+---------+-------+ | roll | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | marks | decimal(10,0) | YES | UNI | NULL | | | date_of_birth | date | YES | | NULL | | | fees | double(16,2) | YES | | NULL | | +---------------+---------------+------+-----+---------+-------+ 5 rows in set (0.02 sec) 17.Change the size of the column roll to int(3) mysql> alter table student modify roll int(3); 18.display the structure of the table student mysql> desc student; +---------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------+------+-----+---------+-------+ | roll | int(3) | NO | PRI | 0 | | | name | varchar(20) | NO | | NULL | | | marks | decimal(10,0) | YES | UNI | NULL | | | date_of_birth | date | YES | | NULL | | | fees | double(16,2) | YES | | NULL | | +---------------+---------------+------+-----+---------+-------+ 5 rows in set (0.02 sec) 19.Rename the table student to st mysql> rename table student to st; 20.Remove the primary key from the table . mysql> alter table student drop primary key; 21.Remoe the column marks mysql> alter table student drop marks;
mysql> desc student;
+---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | roll | int(3) | NO | |0 | | | name | varchar(20) | NO | | NULL | | | date_of_birth | date | YES | | NULL | | | fees | double(16,2) | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) 22.Add a column marks of float type with size(7,2) mysql> alter table student add marks float(7,2); 23.Change all the student marks to 55.54 mysql> update student set marks=55.54; 24.Display all the contents of the table student. mysql> select * from student; +------+--------+---------------+----------+-------+ | roll | name | date_of_birth | fees | marks | +------+--------+---------------+----------+-------+ | 1 | Ram | 1996-03-23 | 45000.00 | 55.54 | | 2 | Shyam | 1997-03-23 | NULL | 55.54 | | 3 | Hari | NULL | NULL | 55.54 | | 4 | Govind | 1999-08-25 | NULL | 55.54 | +------+--------+---------------+----------+-------+ 4 rows in set (0.00 sec) 25.add a new column DOJ of date datatype. mysql> alter table student add DOJ date; 26.set the value of DOJ to current date using function. mysql> update student set DOJ=curdate(); 27.display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+-------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+-------+------------+ | 1 | Ram | 1996-03-23 | 45000.00 | 55.54 | 2023-03-20 | | 2 | Shyam | 1997-03-23 | NULL | 55.54 | 2023-03-20 | | 3 | Hari | NULL | NULL | 55.54 | 2023-03-20 | | 4 | Govind | 1999-08-25 | NULL | 55.54 | 2023-03-20 | +------+--------+---------------+----------+-------+------------+ 4 rows in set (0.00 sec) 28. Change the date of birth to 1996-03-21 where date of birth is null mysql> update student set date_of_birth='1996-03-21' where date_of_birth is NULL; 29. display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+-------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+-------+------------+ | 1 | Ram | 1996-03-23 | 45000.00 | 55.54 | 2023-03-20 | | 2 | Shyam | 1997-03-23 | NULL | 55.54 | 2023-03-20 | | 3 | Hari | 1996-03-21 | NULL | 55.54 | 2023-03-20 | | 4 | Govind | 1999-08-25 | NULL | 55.54 | 2023-03-20 | +------+--------+---------------+----------+-------+------------+ 4 rows in set (0.00 sec) 30.Increae the fees by 10% mysql> update student set fees=fees+fees*(10/100); 31.display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+-------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+-------+------------+ | 1 | Ram | 1996-03-23 | 49500.00 | 55.54 | 2023-03-20 | | 2 | Shyam | 1997-03-23 | NULL | 55.54 | 2023-03-20 | | 3 | Hari | 1996-03-21 | NULL | 55.54 | 2023-03-20 | | 4 | Govind | 1999-08-25 | NULL | 55.54 | 2023-03-20 | +------+--------+---------------+----------+-------+------------+ 4 rows in set (0.00 sec) 32.Change the fees to 50000 where fees is null. mysql> update student set fees=50000 where fees is null; 33. .display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+-------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+-------+------------+ | 1 | Ram | 1996-03-23 | 49500.00 | 55.54 | 2023-03-20 | | 2 | Shyam | 1997-03-23 | 50000.00 | 55.54 | 2023-03- 20 | | 3 | Hari | 1996-03-21 | 50000.00 | 55.54 | 2023-03-20 | | 4 | Govind | 1999-08-25 | 50000.00 | 55.54 | 2023-03- 20 | +------+--------+---------------+----------+-------+------------+ 4 rows in set (0.00 sec) 34. Increase the fees by 10000 where fees is less than 50000 mysql> update student set fees=fees+10000 where fees<50000; 35. .display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+--------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+--------+------------+ | 1 | Ram | 1996-03-23 | 59500.00 | 100.00 | 2023-03- 20 | | 2 | Shyam | 1997-03-23 | 50000.00 | 100.00 | 2023-03- 20 | | 3 | Hari | 1996-03-21 | 50000.00 | 100.00 | 2023-03-20 | | 4 | Govind | 1999-08-25 | 50000.00 | 100.00 | 2023-03- 20 | +------+--------+---------------+----------+--------+------------+ 4 rows in set (0.00 sec) 36.delete the records of student table where name starts with S mysql> delete from student where name like 'S%'; 37.display the contents of the table to show the changes. mysql> select * from student; +------+--------+---------------+----------+--------+------------+ | roll | name | date_of_birth | fees | marks | DOJ | +------+--------+---------------+----------+--------+------------+ | 1 | Ram | 1996-03-23 | 59500.00 | 100.00 | 2023-03- 20 | | 3 | Hari | 1996-03-21 | 50000.00 | 100.00 | 2023-03-20 | | 4 | Govind | 1999-08-25 | 50000.00 | 100.00 | 2023-03- 20 | +------+--------+---------------+----------+--------+------------+ 3 rows in set (0.00 sec) 38.Delete all the records of table student mysql> delete from student; 39. display the contents of the table to show the changes. mysql> select * from student; Empty set (0.00 sec) 40.Remove or delete the table student mysql> drop table student; Query OK, 0 rows affected (0.01 sec) 41. display the contents of the table to show the changes. mysql> select * from student; ERROR 1146 (42S02): Table 'school.student' doesn't exist 42.delete the database school mysql> drop database school; Query OK, 0 rows affected (0.01 sec) 43. display the contents of the table to show the changes. mysql> use school; ERROR 1049 (42000): Unknown database 'school'