Professional Documents
Culture Documents
22MCC20125 Anshuman Ba WS 2.2
22MCC20125 Anshuman Ba WS 2.2
Worksheet -2.2
Aim:
Q1. Explain the meaning of Database. Write the steps to create and alter database
Q2. Discuss the steps to create and alter a table, views, and functions
SOLUTION: -
1)
DATABASE
• A database is a structured collection of data that is organized and stored in a way that makes it easy to retrieve,
manage, and update.
• It acts as a repository for information, allowing users or applications to interact with the data efficiently.
• Databases are used in various applications like websites, mobile apps, business applications, and more.
To create, alter, and drop databases, tables, views, functions and indexes.
Objective: Practice the essential SQL operations, including creating, altering, and dropping
databases, tables, views, functions, and indexes, to effectively manage and manipulate database
structures and data.
Database Operations:
1. Create Database: To Create a new database Use, CREATE DATABASE database_name;
2. Alter Database: In SQL, you cannot directly alter a database in the same way you alter
tables or columns. Databases are usually altered by making changes to their contents
(e.g., adding or modifying tables and data).
3. Drop Database: Use DROP DATABASE database_name;
Table Operations:
1. Create Table: To create a new table within a database, you can use the ‘CREATE
TABLE’ statement. Here's an example:
2. Alter Table: To modify an existing table, you can use the ‘ALTER TABLE’ statement.
You can add, modify, or delete columns, among other operations.
3. Drop Table: To delete or drop a table, you can use the ‘DROP TABLE’ statement.
View Operations:
1. Create View: To create a view in SQL, you use the ‘CREATE VIEW’ statement. A view
is a virtual table based on the result of a SELECT query.
2. Alter View: Views are usually altered or replaced by creating a new view with desired
changes. You can use the ‘CREATE OR REPLACE VIEW’ statement or simply create a
new view with the same name.
3. Drop View: To delete or drop a view, you can use the ‘DROP VIEW’ statement.
Index Operations:
1. Create Index: To create an index on a table in SQL, you use the ‘CREATE INDEX’
statement.
2. Alter Index: In most database systems, you cannot directly alter an index. If you need
to change the index structure, you typically need to drop and recreate it with the desired
changes.
3. Drop Index: To delete or drop an index, you can use the ‘DROP INDEX’ statement.
Function Operations:
1. Create Function: To create a user-defined function in SQL, you can use the ‘CREATE
FUNCTION’ statement.
2. Alter Function: MySQL does not provide a direct ‘ALTER FUNCTION’ statement. If
you need to modify a function, you typically need to drop and recreate it with the
desired changes.
3. Drop Function: To delete or drop a function, you can use the ‘DROP FUNCTION’
statement.