Basic
Basic
mysql> create table enroll(roll_no integer, c_id integer, date date); -- Create
'enroll' table to link students and courses
Query OK, 0 rows affected (0.01 sec)
mysql> alter table course add primary key(c_id); -- Set 'c_id' as the primary
key for 'course' table
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc course; -- Check 'course' table (should show an error since it was
renamed)
ERROR 1146 (42S02): Table 'university.course' doesn't exist
mysql> alter table course_info add column duration integer; -- Add 'duration'
column to 'course_info'
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc course_info; -- Describe the renamed table 'course_info'
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| c_id | int | NO | PRI | NULL | |
| c_name | char(20) | YES | | NULL | |
| duration | int | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from student where roll_no = 1; -- Fetch student details where
roll_no is 1
+---------+--------------+---------+-----------+
| roll_no | name | address | mob_no |
+---------+--------------+---------+-----------+
| 1 | Yash Dabhade | Jalna | 785694123 |
+---------+--------------+---------+-----------+
1 row in set (0.00 sec)
mysql> delete from student where roll_no = 3; -- Delete student with roll_no 3
Query OK, 1 row affected (0.01 sec)
mysql> select * from student; -- Display remaining students
+---------+--------------+---------+-----------+
| roll_no | name | address | mob_no |
+---------+--------------+---------+-----------+
| 1 | Yash Dabhade | Jalna | 785694123 |
| 2 | Om Jejurkar | Shirdi | 879645123 |
+---------+--------------+---------+-----------+
2 rows in set (0.00 sec)
mysql> alter table enroll drop constraint fk_roll_no; -- Remove foreign key
constraint on 'roll_no' in 'enroll'
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> truncate table student; -- Delete all records from 'student' without
affecting table structure
Query OK, 0 rows affected (0.02 sec)
mysql> exit;
Bye
C:\Users\ameyk>mysql -u user1 -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.40 MySQL Community Server - GPL
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit;
Bye
C:\Users\ameyk>mysql -u user1 -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.40 MySQL Community Server - GPL
mysql> exit;
Bye
C:\Users\ameyk>mysql -u user1 -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.40 MySQL Community Server - GPL
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.