0% found this document useful (0 votes)
26 views14 pages

MySQL Database and Table Operations

This document contains 15 questions and MySQL programs that create databases and tables, insert, update, and retrieve data from tables using various SQL queries. The programs demonstrate how to create a database and table, insert data, set default values, use aggregation functions, add/drop primary keys, use wildcards in queries, and convert case in queries.
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)
26 views14 pages

MySQL Database and Table Operations

This document contains 15 questions and MySQL programs that create databases and tables, insert, update, and retrieve data from tables using various SQL queries. The programs demonstrate how to create a database and table, insert data, set default values, use aggregation functions, add/drop primary keys, use wildcards in queries, and convert case in queries.
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

SQL

QUERIES

23 | P a g e
Q1. Write down syntax and mysql program to create a database STUDENT and its
OUTPUT.

SYNTAX:

CREATE DATABASE dbname;

PROGRAM:

mysql> create database student;

Q2. Write down syntax and mysql program to delete a database STUDENT
and its OUTPUT.

SYNTAX:

DROP DATABASE dbname;

PROGRAM:

mysql> drop database student;

24 | P a g e
Q3. Write down a mysql program to create a table TEACHER with the following field names
given below and its OUTPUT.

a. Teacher_ID,
b. First_Name,
c. Last_Name,
d. Gender,
e. Salary,
f. Date_of_Birth
g. Dept_No

PROGRAM:

mysql> CREATE TABLE TEACHER


(
Teacher_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);

25 | P a g e
Q4. Write down mysql program to show the databases and tables created?

PROGRAM:
mysql> show databases;

PROGRAM
mysql> show tables;

26 | P a g e
Q5. Write a mysql program to list out the field name Teacher_ID, First_Name, Last_Name,
Gender, Salary, Date_of_Birth, Dept_No along with its field type and constraints for the table
TEACHER in the database STUDENT.

PROGRAM

mysql > use student;


Database changed

mysql > desc teacher;

27 | P a g e
Q6. Write down mysql program to set a default value for the field SALARY as 30000 to
the table name TEACHER whose field names are listed below
a. Teacher_ID,
b. First_Name,
c. Last_Name,
d. Gender,
e. Salary,
f. Date_of_Birth
g. Dept_No

PROGRAM

mysql > desc teacher;

mysql > ALTER TABLE TEACHER ALTER SALARY SET DEFAULT 30000;

28 | P a g e
Q7. Write a mysql command to INSERT VALUES INTO table TEACHER in its field
Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No

PROGRAM:

mysql > INSERT INTO Teacher (Teacher_ID, First_Name, Last_Name, Gender, Salary,
Date_of_Birth, Dept_No) VALUES(101,"Shanaya", "Batra", 'F', 50000, '1984-08-11', 1);

29 | P a g e
Q8. Write a mysql command to display the details of a Teacher whose
Teacher_ID=101.

The field names in Teacher table is listed below.

 Teacher_ID,
 First_Name,
 Last_Name,
 Gender,
 Salary,
 Date_of_Birth,
 Dept_No

PROGRAM

mysql > SELECT * FROM TEACHER WHERE Teacher_ID=101;

30 | P a g e
Q9. Write a mysql command to UPDATE the details of Salary as 55000 in Teacher
table whose Teacher_ID=101.

The field names in Teacher table is listed below.

Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No

PROGRAM

mysql> UPDATE Teacher SET Salary=55000 WHERE Teacher_ID=101;

31 | P a g e
Q10. Write a mysql command to find the following using AGGREGATE FUNCTIONS in Table
Teacher. The field names in Teacher table is listed below.
Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No

(i) Calculate the sum of salary given to the Teachers and name it as Total_Salary.
(ii) Calculate the maximum and minimum salary of the Teacher and name it
as Max_Salary and Min_Salary.

(iii) Calculate the total number of Teachers whose salary is >40000.

PROGRAM 1

mysql > SELECT SUM(Salary) AS Total_Salary FROM Teacher;

32 | P a g e
PROGRAM 2
mysql > SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary FROM Teacher;

PROGRAM 3
mysql > SELECT COUNT(Salary) FROM Teacher WHERE Salary > 40000;

Q11. Write a mysql command to add a primary key.

PROGRAM:

mysql > ALTER TABLE item ADD PRIMARY KEY(ino);

33 | P a g e
Q12. Write a mysql command to drop a primary key.

PROGRAM:

mysql > ALTER TABLE item DROP PRIMARY KEY;

Q13. Write a mysql command to display item record which name begins from s.

PROGRAM:

Select * from item where itemname like ‘s%’;

34 | P a g e
Q14. Write a mysql command to add a unique key.

PROGRAM:

Q15. Write a mysql command to display name in upper case.

PROGRAM:

mysql> select upper(pname) as 'patient name, year(admitdate) as 'admit year' -> from hospital;

35 | P a g e
Made and Design by : Mr. Shiv Kailash Shukla

36 | P a g e

Common questions

Powered by AI

The UPPER function in SQL converts text to uppercase, aiding in the standardization of data representation. This is useful when uniformity in textual data entries is required for operations like comparison, sorting, or reporting. For example, using 'SELECT UPPER(pname) AS 'patient name' FROM hospital;' standardizes all patient names to uppercase, ensuring consistency and preventing discrepancies due to case differences during data handling processes .

The SQL command 'ALTER TABLE item DROP PRIMARY KEY;' is used to remove the primary key constraint. This might be necessary during table restructuring or when modifying the primary key to include different columns. However, dropping a primary key can lead to potential issues like data duplication and reduced query performance, as the unique identifier for table records is lost. It is crucial to ensure that data integrity and uniqueness requirements are met by other means before dropping a primary key .

To set a default value for the 'Salary' field in a MySQL table, the ALTER TABLE command is used as follows: 'ALTER TABLE TEACHER ALTER SALARY SET DEFAULT 30000;'. This command ensures that any new records inserted into the TEACHER table without a specified salary will automatically have a salary of 30000. Setting default values helps maintain database integrity by providing baseline data values and reducing errors due to missing information .

To display records with names starting with a specific letter, use the SQL command 'SELECT * FROM item WHERE itemname LIKE 's%';'. This command uses the LIKE operator and percent wildcard to match any names beginning with 's'. Such filtering is important for efficiently retrieving relevant subsets of data, simplifying data inspection, and allowing users to quickly find and analyze records that meet specific criteria .

A primary key in a MySQL table ensures each record can be uniquely identified, preventing duplicate entries and maintaining the accuracy of the data. By using 'ALTER TABLE item ADD PRIMARY KEY(ino);', the 'ino' field is set as the primary key. This key constraint automatically rejects any record with a duplicate 'ino' value, enforcing the uniqueness and integrity of the dataset. Implementing primary keys is essential for relational database functionality, facilitating efficient indexing and reliable connections between tables .

To create a database and a table in MySQL, first execute 'CREATE DATABASE student;' to define a new database named STUDENT. Next, use 'USE student;' to select this database for further operations. Then, create a table using 'CREATE TABLE TEACHER (Teacher_ID INTEGER, First_Name VARCHAR(20), Last_Name VARCHAR(20), Gender CHAR(1), Salary DECIMAL(10,2), Date_of_Birth DATE, Dept_No INTEGER);'. These steps are fundamental for organizing and storing data in a structured manner, enabling efficient data retrieval and management .

Setting a unique key in a table ensures that the values in specified fields remain distinct across all records. This is accomplished using the command 'ALTER TABLE table_name ADD UNIQUE(column_name);'. Unlike a primary key, which ensures uniqueness and cannot hold null values, a unique key allows nulls, but any non-null entry must be unique. Unique keys prevent data duplication in specific columns, maintaining data integrity while allowing more flexibility than primary keys regarding null values .

Aggregate functions in MySQL such as SUM, MAX, MIN, and COUNT are used to perform calculations on a set of values, returning a single value. In the TEACHER table, SUM(Salary) calculates the total salary, while MAX(Salary) and MIN(Salary) provide the highest and lowest salaries respectively. COUNT(Salary) is used to determine the number of teachers earning more than 40000. These functions are critical for analyzing large datasets to derive meaningful insights about the salary distributions and trends .

MySQL commands like 'SELECT SUM(Salary) AS Total_Salary FROM Teacher;' and 'SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary FROM Teacher;' are crucial for calculating statistical values. These commands allow users to quickly gauge collective metrics such as total salaries, and identify extremes such as maximum and minimum salaries within a dataset. Statistical analysis using SQL is critical for summarizing data, identifying patterns, conducting performance assessments, and making data-driven business decisions .

To insert records in a database, use 'INSERT INTO Teacher (Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No) VALUES(101,"Shanaya", "Batra", 'F', 50000, '1984-08-11', 1);'. To update existing data, use 'UPDATE Teacher SET Salary=55000 WHERE Teacher_ID=101;'. These commands ensure data relevance by allowing users to populate and modify the database according to the latest information. Consistency is maintained by updating only specified records, ensuring accurate reflection of real-world data changes .

You might also like