Student database using MySQL Part 1
MYSQL EXAMPLE
Will practice with an example. Will have an example for the Student database.
CREATE DATABASE:
Create a database to store the tables.
Will create a Student database to store the details about the Students.
CREATE DATABASE Students;
SHOW DATABASE:
The SHOW DATABASES statement lists all the databases.
USE DATABASE:
The USE DATABASE statement is used to choose the database as a current database.
USE Students;
This statement makes the Student database a current database.
- Create a new table in the Students Database.
- Create department and Student tables.
- The id column should be NOT NULL and AUTO_INCREMENT. And it is a Primary key.
- In the department table, the id column is the Foreign key for the Student table.
CREATE TABLE Department(id int NOT NULL AUTO_INCREMENT, name varchar(50), hod varchar(20), PRIMARY KEY(id));
CREATE TABLE ClgStudents(rollno int NOT NULL AUTO_INCREMENT, name varchar(50), age int, department int, district varchar(40), fees int, PRIMARY KEY(rollno), FOREIGN KEY (department) REFERENCES Department(id));
RENAME TABLE:
Rename the ClgStudents table name as Students.
RENAME TABLE ClgStudents TO Students;
INSERT DATA:
- Insert data for the department table and student table.
- Will insert multiple values for the table in a single query.
INSERT INTO Department(name, hod) VALUES('CSE', 'Rajendran'), ('IT', 'Venkat'), ('ECE', 'Hema');
INSERT INTO Students(name, age, department, district, fees) VALUES('Vetri', 16, 1, 'Madurai', 50000);
Thanks for choosing our blog 😊.
Will discuss an example of table structure modification in the next part. - Part-2
Comments
Post a Comment