MySQL Class 11 Examples
MySQL Class 11 Examples
The SHOW DATABASES command lists all existing databases on the server, providing an overview of available data environments. Similarly, SHOW TABLES displays all tables within a selected database, offering insights into the data structure and available resources within that database. Both commands are vital for managing data visibility and navigating through multiple databases and tables .
The DESCRIBE command in MySQL is crucial for understanding the structure of a table. It lists all columns, their types, and attributes such as keys and defaults. For beginners, DESCRIBE is fundamental as it provides a clear view of how data is organized within a table, aiding in writing accurate queries and understanding database design .
To insert multiple records into a table, use an INSERT command with multiple value sets: INSERT INTO student VALUES (2,'Riya',11,'B'), (3,'Kabir',11,'A');. After insertion, to ensure only distinct sections are selected, use the DISTINCT clause: SELECT DISTINCT section FROM student;. This selects each unique section present in the 'student' table .
The UPDATE command changes existing data within a table. For example, to increase an employee's salary, use: UPDATE employee SET salary=30000 WHERE emp_id=101;. This changes the salary of the employee with 'emp_id' 101 to 30,000. The command allows targeted alterations based on specified conditions .
To add a new column to an existing table, use the ALTER TABLE command. For example, to add a column 'marks' to the 'student' table, use ALTER TABLE student ADD marks INT;. To verify the change, the DESCRIBE command can be used: DESCRIBE student;. This command displays the table structure, confirming the addition of the 'marks' column .
To create a table named EMPLOYEE with columns for employee ID, name, and salary, use the command: CREATE TABLE employee ( emp_id INT, emp_name VARCHAR(40), salary INT );. This defines a table with three columns, each specified with an appropriate data type to store employee details .
The WHERE clause is used in a SELECT statement to filter records based on specified conditions. For instance, SELECT * FROM student WHERE class = 11; retrieves all records where the class is 11. This process allows for targeted data retrieval, focusing on specific criteria within the dataset .
Using ALTER TABLE, various modifications can be conducted: adding a new column (ALTER TABLE student ADD marks INT;), modifying a column's datatype (ALTER TABLE student MODIFY name VARCHAR(60);), dropping a column (ALTER TABLE student DROP marks;), and renaming the table (ALTER TABLE student RENAME TO student_details;). Each modification adjusts the table to meet evolving data needs .
To create a database named 'school', the command is CREATE DATABASE school;. To select this database for use, the command is USE school;. This sequence creates a database and then selects it for further operations .
The DELETE command removes records from a table. To delete a specific record, use a condition: DELETE FROM student WHERE roll_no=3;. This deletes the record where 'roll_no' is 3. To delete all records, simply use DELETE FROM student;, which removes all entries without dropping the table itself .