1.DDL Commands: 1. Creating A Table
1.DDL Commands: 1. Creating A Table
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
5. Rename
mysql> alter table newstudents rename to Students;
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)
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.
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;
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)
+---------+-----------+------+------+--------+
| 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?
c. Since keeping the age of the student as an attribute requires frequent changes (each year)
propose a
solution and implement it.
+---------+-----------+------+------+--------+
| 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)
- List the names and prices for all items that have a quantity in hand >20.
+--------------+---------------------+
| 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