0% found this document useful (0 votes)
7 views3 pages

Pratcice

The document contains a series of MySQL commands demonstrating database management, including showing databases, selecting a database, and querying tables. It shows the creation of a table named 'enrollment' with an auto-increment primary key and foreign key constraints. Additionally, it provides instructions for truncating a table and altering it to add primary and foreign keys.

Uploaded by

sushankc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Pratcice

The document contains a series of MySQL commands demonstrating database management, including showing databases, selecting a database, and querying tables. It shows the creation of a table named 'enrollment' with an auto-increment primary key and foreign key constraints. Additionally, it provides instructions for truncating a table and altering it to add primary and foreign keys.

Uploaded by

sushankc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

mysql> show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student_management |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from student;


ERROR 1046 (3D000): No database selected
mysql> select * from students;
ERROR 1046 (3D000): No database selected
mysql> use databases;
ERROR 1049 (42000): Unknown database 'databases'
mysql> use student_management;
Database changed
mysql> select * from student_management;
ERROR 1146 (42S02): Table 'student_management.student_management' doesn't
exist
mysql> select * from students;
ERROR 1146 (42S02): Table 'student_management.students' doesn't exist
mysql> select * from student;
+----+--------+--------------------+---------------------+
| id | name | email | created_at |
+----+--------+--------------------+---------------------+
| 1 | sushan | [email protected] | 2024-12-13 07:32:55 |
| 2 | rohan | [email protected] | 2024-12-13 07:35:00 |
| 3 | sundar | [email protected] | 2024-12-13 07:35:33 |
| 4 | roshan | [email protected] | 2024-12-13 07:35:49 |
| 5 | yenush | [email protected] | 2024-12-13 07:36:35 |
| 6 | ishwor | [email protected] | 2024-12-13 07:36:54 |
+----+--------+--------------------+---------------------+
6 rows in set (0.04 sec)

mysql> select * from course;


+------+------------+--------+
| c_id | c_name | c_type |
+------+------------+--------+
| 1 | management | NULL |
| 2 | management | NULL |
| 3 | management | NULL |
| 4 | Science | NULL |
| 5 | Science | NULL |
+------+------------+--------+
5 rows in set (0.04 sec)

mysql> create table enrollment(


-> enrollment id int auto_increment primary key,
-> student_id int not null,
-> course_id int not null,
-> enrollmentDate DATE);
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 'id int
auto_increment primary key,
student_id int not null,
course_id int not nu' at line 2
mysql> create table enrollment(
-> enrollment_id int auto_increment primary key,
-> student_id int not null,
-> course_id int not null,
-> enrollmentDate DATE,
-> foreign key (student_id)
->References student (id) ;
Query OK, 0 rows affected (0.07 sec)

mysql> describe enrollment;


+----------------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------+------+-----+---------+----------------+
| enrollment_id | int | NO | PRI | NULL | auto_increment |
| student_id | int | NO | | NULL | |
| course_id | int | NO | | NULL | |
| enrollmentDate | date | YES | | NULL | |
+----------------+------+------+-----+---------+----------------+
4 rows in set (0.04 sec)

 Truncate table enrollment; i.e it cleans the tables


 Alter table enrollment add foreign key (student_id)
References student(id)
 Alter table enrollment add constraint pk_enrollment_id primary
key(enrollment_id).i.e if primary key Is not added at first use this syntax and
add primary key

You might also like