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

MYSQL

The document outlines the steps to create a database schema for students and courses, including the creation of tables and insertion of sample data. It also provides SQL commands for fetching course information linked to student names and demonstrates variable assignment in SQL. Key operations include creating tables, inserting records, and performing joins in queries.

Uploaded by

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

MYSQL

The document outlines the steps to create a database schema for students and courses, including the creation of tables and insertion of sample data. It also provides SQL commands for fetching course information linked to student names and demonstrates variable assignment in SQL. Key operations include creating tables, inserting records, and performing joins in queries.

Uploaded by

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

📘 Step 1: Create the students table

CREATE TABLE students (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50),

age INT,

gender ENUM('Male', 'Female', 'Other'),

grade VARCHAR(10)

);

Step 2: Create the courses table

CREATE TABLE courses (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50),

student_id INT,

FOREIGN KEY (student_id) REFERENCES students(id)

ON DELETE CASCADE

ON UPDATE CASCADE

);

Step 3: Insert data into students

INSERT INTO students (name, age, gender, grade) VALUES

('Alice', 14, 'Female', '8th'),

('Bob', 15, 'Male', '9th'),

('Diana', 14, 'Female', '8th');

Step 4: Insert data into courses

INSERT INTO courses (name, student_id) VALUES

('Mathematics', 1),

('Science', 1),

('English', 2),

('History', 3),

('Mathematics', 3);

Step 5: Fetch all courses with student names

SELECT c.id, c.name AS course_name, s.name AS student_name


FROM courses c

JOIN students s ON c.student_id = s.id;


Assigning values using SET (usually in stored procedures or scripts):

DECLARE @name VARCHAR(50);

SET @name = 'John Doe';

2. Assigning values during UPDATE:

UPDATE employees

SET salary = 50000

WHERE id = 101;

3. Assignment in SELECT using variables:

SELECT @total := SUM(price) FROM orders;

4. Inserting data with INSERT INTO:

INSERT INTO students (name, age)

VALUES ('Alice', 20);

You might also like