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

1.DDL Commands: 1. Creating A Table

The document discusses various SQL commands including DDL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE TABLE, and RENAME TABLE. It also discusses DML commands like INSERT, DELETE, UPDATE, and defines constraints. Examples are provided to create tables, insert and manipulate data, add constraints, and more. Assignments include creating tables for student and shop item records, performing queries to retrieve specific data, and adding a constraint to prevent negative prices.

Uploaded by

vivek srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views

1.DDL Commands: 1. Creating A Table

The document discusses various SQL commands including DDL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE TABLE, and RENAME TABLE. It also discusses DML commands like INSERT, DELETE, UPDATE, and defines constraints. Examples are provided to create tables, insert and manipulate data, add constraints, and more. Assignments include creating tables for student and shop item records, performing queries to retrieve specific data, and adding a constraint to prevent negative prices.

Uploaded by

vivek srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

DDL commands
1. Creating a table
mysql> create table newstudents(ID int,Name Varchar(255),Course Varchar(255),Marks float,City
Varchar(60),DOB date);
Query OK, 0 rows affected (0.05 sec)

2. Altering a table
alter table newstudents drop column DOB;

3. Drop a table
drop table Students;

4. Truncating a table
insert into
newstudents(ID,Name,Course,Marks,City)values(1,'Mansi','MCA',7.6,'Allahabad'),(2,'Shammi','BSC',7
,'Delhi'),(3,'Aryan','BCOM',8,'Varanasi');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> truncate newstudents


after truncating table became empty
select* from newstudents;
Empty set (0.00 sec)

5. Rename
mysql> alter table newstudents rename to Students;

after renaming the table


show tables;
+----------------------+
| Tables_in_db2018CA31 |
+----------------------+
| Students |
+----------------------+
1 row in set (0.00 sec)

b. Practicing all the following DML commands


1. Inserting Rows into table
mysql> create table Employees(ID int,Name varchar(70),City varchar(50),Age int,Department
varchar(30));
mysql> insert into Employees(ID,Name,
City,Age,Department)values(100,'Reena','Kanpur',22,'Technical'),
-> (101,'Arpita','Noida',24,'Law'),
-> (102,'Divyansh','Delhi',23,'Software'),
-> (103,'Shammi','Dehradoon',21,'Medicine');
Query OK, 4 rows affected (0.00 sec)

OUTPUT
mysql> Select * from Employees;
+------+----------+-----------+------+------------+
| ID | Name | City | Age | Department |
+------+----------+-----------+------+------------+
| 100 | Reena | Kanpur | 22 | Technical |
| 101 | Arpita | Noida | 24 | Law |
| 102 | Divyansh | Delhi | 23 | Software |
| 103 | Shammi | Dehradoon | 21 | Medicine |
+------+----------+-----------+------+------------+
4 rows in set (0.00 sec)

2. Deleting Rows from table


mysql> delete from Employees where Name='Arpita';

OUTPUT
mysql> Select * from Employees;
+------+----------+-----------+------+------------+
| ID | Name | City | Age | Department |
+------+----------+-----------+------+------------+
| 100 | Reena | Kanpur | 22 | Technical |
| 102 | Divyansh | Delhi | 23 | Software |
| 103 | Shammi | Dehradoon | 21 | Medicine |
+------+----------+-----------+------+------------+
Employee name with Arpita is deleted.

3. Updating data in a table


mysql> update Employees set City='Pune' where ID=100;

OUTPUT
mysql> Select * from Employees;

+------+----------+-----------+------+------------+
| ID | Name | City | Age | Department |
+------+----------+-----------+------+------------+
| 100 | Reena | Pune | 22 | Technical |
| 102 | Divyansh | Delhi | 23 | Software |
| 103 | Shammi | Dehradoon | 21 | Medicine |
+------+----------+-----------+------+------------+
City name of employee with ID 100 is updated.

4. Defining constraints
mysql> alter table Employees modify Age int NOT NULL;

mysql> alter table Employees add UNIQUE(ID);


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

2. Assignment on DDL and DML commands


A. Suppose that a faculty wants to keep records about the students, the desired records should
maintain this information about each student: (Roll No, Full name, CGPA, Age, and Gender)

a. Create the appropriate Table.


mysql> create table STUDENT(Roll_no int,Full_Name Varchar(40),CGPA float,Age int,Gender
char(10));

mysql> insert into STUDENT(Roll_no,Full_Name,CGPA,Age,Gender)values(1,'Ayushi',8,22,'F'),


(2,'Abhishek',8.4,23,'M'), (3,'Ankita',8.9,20,'F'), (4,'Dwarkesh',8.3,22,'M'), (5,'Kaustubh',7.5,23,'M'),
(6,'Liza',9.3,22,'F');

OUTPUT
mysql> select* from STUDENT;
+---------+-----------+------+------+--------+
| Roll_no | Full_Name | CGPA | Age | Gender |
+---------+-----------+------+------+--------+
| 1 | Ayushi | 8 | 22 | F |
| 2 | Abhishek | 8.4 | 23 | M |
| 3 | Ankita | 8.9 | 20 | F |
| 4 | Dwarkesh | 8.3 | 22 | M |
| 5 | Kaustubh | 7.5 | 23 | M |
| 6 | Liza | 9.3 | 22 | F |
+---------+-----------+------+------+--------+
6 rows in set (0.00 sec)

b. Write SQL Queries to answer the following problems:


- List the names of all female students.

mysql> Select * from STUDENT where Gender='F';

+---------+-----------+------+------+--------+
| Roll_no | Full_Name | CGPA | Age | Gender |
+---------+-----------+------+------+--------+
| 1 | Ayushi | 8 | 22 | F |
| 3 | Ankita | 8.9 | 20 | F |
| 6 | Liza | 9.3 | 22 | F |
+---------+-----------+------+------+--------+
- What is the age of the youngest male student?

mysql> Select min(Age) from STUDENT where Gender='M';


+----------+
| min(Age) |
+----------+
| 22 |
+----------+
-List the name and the CGPA for all students who got above 6.5 CGPA ordered by their
CGPAs then their names.

mysql> Select CGPA,Full_Name from STUDENT where CGPA>6.5 ORDER BY CGPA;


+------+-----------+
| CGPA | Full_Name |
+------+-----------+
| 7.5 | Kaustubh |
| 8 | Ayushi |
| 8.3 | Dwarkesh |
| 8.4 | Abhishek |
| 8.9 | Ankita |
| 9.3 | Liza |
+------+-----------+
- What is the Topper name?

mysql> Select Full_Name, MAX(CGPA) from STUDENT


where CGPA=(Select MAX(CGPA) from STUDENT);
+-----------+------------------+
| Full_Name | MAX(CGPA) |
+-----------+------------------+
| Liza | 9.30000019073486 |
+-----------+------------------+

c. Since keeping the age of the student as an attribute requires frequent changes (each year)
propose a
solution and implement it.

mysql> Update STUDENT set Age=Age+1;


Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0

mysql> Select * from STUDENT;

+---------+-----------+------+------+--------+
| Roll_no | Full_Name | CGPA | Age | Gender |
+---------+-----------+------+------+--------+
| 1 | Ayushi | 8 | 23 | F |
| 2 | Abhishek | 8.4 | 24 | M |
| 3 | Ankita | 8.9 | 21 | F |
| 4 | Dwarkesh | 8.3 | 23 | M |
| 5 | Kaustubh | 7.5 | 24 | M |
| 6 | Liza | 9.3 | 23 | F |
+---------+-----------+------+------+--------+

3. A supermarket manager likes to keep records about all the items in his store these records should
hold the
following information about each item (the item identifier item_id, the item name, the item price,
expiration date of the item, quantity in hand)

a.Create the appropriate table.

mysql> create table shop(Item_id int,Item_name varchar(40),Item_price float,Expiry_date


date,Quantity int);

insert into shop values(101,'Fruit Juices',25.00,'2019-08-07',20), (102,'Noodles',10,'2019-10-


24',50),(103,'Milk',12,'2019-08-04',14),(104,'Chocolates',50.05,'2019-11-
07',35),(105,'Breads',25.02,'2019-08-03',4);

mysql> select * from shop;


+---------+--------------+------------+-------------+----------+
| Item_id | Item_name | Item_price | Expiry_date | Quantity |
+---------+--------------+------------+-------------+----------+
| 101 | Fruit Juices | 25 | 2019-08-07 | 20 |
| 102 | Noodles | 10 | 2019-10-24 | 50 |
| 103 | Milk | 12 | 2019-08-04 | 14 |
| 104 | Chocolates | 50.05 | 2019-11-07 | 35 |
| 105 | Breads | 25.02 | 2019-08-03 | 4|
+---------+--------------+------------+-------------+----------+

b.Write sql queries to answer the following problems:

- List the names and prices for all items that have a quantity in hand >20.

mysql> Select *from shop where Quantity>20;


+---------+------------+------------+-------------+----------+
| Item_id | Item_name | Item_price | Expiry_date | Quantity |
+---------+------------+------------+-------------+----------+
| 102 | Noodles | 10 | 2019-10-24 | 50 |
| 104 | Chocolates | 50.05 | 2019-11-07 | 35 |
+---------+------------+------------+-------------+----------+
- List the names of expired items.

mysql> Select *from shop where Expiry_date<'2019-08-05';


+---------+-----------+------------+-------------+----------+
| Item_id | Item_name | Item_price | Expiry_date | Quantity |
+---------+-----------+------------+-------------+----------+
| 103 | Milk | 12 | 2019-08-04 | 14 |
| 105 | Breads | 25.02 | 2019-08-03 | 4|
+---------+-----------+------------+-------------+----------+
- name the second most expensive item.

mysql> select Item_name,MAX(Item_price) as Second_highestPrice from shop where Item_price <


(Select MAX(Item_price) from shop);

+--------------+---------------------+
| Item_name | Second_highestPrice |
+--------------+---------------------+
| Fruit Juices | 25.0200004577637 |
+--------------+---------------------+

Alter the table structure to make sure that no negative value can be assigned to the price field

mysql> alter table shop ADD CONSTRAINT CHK_Price CHECK (Item_price>0);

You might also like