DBMSmyfileee
DBMSmyfileee
Roll no.:
Department of Computer Science & Engineering
Continuous Assessment Sheet(CAS)
Roll No.:
Total Marks
Marks (outof100)
Signature Sign
DDL COMMANDS -Data Definition Language (DDL) commands are used to create,
manipulate, and modify objects in Snowflake, such as users, virtual warehouses,
databases, schemas, tables, views, columns, functions, and stored procedures.
Create database:
Use database:
Create table:
1: Customers
2: Products
3: Payments
4: Orders
5: Order Details
Show tables:
1. Customers
2.Products
2.
3.Payments
4.Orders
5. Order Details
Inserting Records:
1.Insert Records into Customers
SELECT Statement:
The SELECT statement is used to retrieve specific data from one or more tables in a database.
LIKE Operator:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
• Wildcards:
o %: Matches zero or more characters.
o _: Matches exactly one character.
ASCENDING (ASC):
Used to sort query results in ascending order (smallest to largest or A to Z). This is the default
order in SQL.
DESCENDING (DESC):
Used to sort query results in descending order (largest to smallest or Z to A).
Select:
Like:
Order By:
ASC:
DESC:
Experiment 3
PRIMARY KEY- A primary key, also called a primary keyword, is a key in a relational
database that is unique for each record.
FOREIGN KEY- FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY
is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
CANDIDATE KEY- CANDIDATE KEY is a set of attributes that uniquely identify tuples in a
table. Candidate Key is a super key with no repeated attributes.
NOT NULL: Ensures that a column cannot have NULL values, meaning every record must
contain a valid (non-NULL) value in this field.
UNIQUE KEY: Ensures all values in a column or group of columns are distinct across the
table, allowing only one NULL value (if permitted).
DEFAULT CONSTRAINTS- The DEFAULT constraint is used to provide a default value for
a column. The default value will be added to all new records IF no other value is specified
Primary key:
Foreign Key:
Candidate Key:
Unique Key:
Default constraint:
Experiment 4
COUNT FUNCTION - The COUNT function returns the total number of values in the
specified field. It works on both numeric and non-numeric data types.
MIN FUNCTION - The COUNT function returns the total number of values in the specified
field. It works on both numeric and non-numeric data types.
MAX FUNCTION - The MAX function is the opposite of the MIN function. It returns the
largest value from the specified table field.
SUM FUNCTION - MySQL SUM function which returns the sum of all the values in the
specified column. SUM works on numeric fields only. Null values are excluded from the result
returned.
AVG FUNCTION - AVG function returns the average of the values in a specified column. Just
like the SUM function, it works only on numeric data types.
LOWER FUNCTION - MySQL last function is used to return the last value of the selected
column.
INNER JOIN- The INNER JOIN selects all rows from both participating tables as long as
there is a match between the columns.
LEFT JOIN - he LEFT JOIN allows you to query data from two or more tables. Similar to
the INNER JOIN clause, the LEFT JOIN is an optional clause of the SELECT statement,
which appears immediately after the FROM clause. Suppose that you want to join two
tables t1 and t2.
RIGHT JOIN - The Right Join is used to joins two or more tables and returns all rows from
the right-hand table, and only those results from the other table that fulfilled the join
condition. If it finds unmatched records from the left side table, it returns Null value.
CROSS JOIN- MySQL CROSS JOIN is used to combine all possibilities of the two or
more tables and returns the result that contains every row from all contributing tables. The
CROSS JOIN is also known as CARTESIAN JOIN, which provides the Cartesian product
of all associated tables.
NATURAL JOIN - A NATURAL JOIN is a JOIN operation that creates an implicit join
clause for you based on the common columns in the two tables being joined. Common
columns are columns that have the same name in both tables. A NATURAL JOIN can be an
INNER join, a LEFT OUTER join, or a RIGHT OUTER join. The default is INNER join
INNER JOIN
Query: Retrieve Customer details along with their Orders status and payment details
LEFT JOIN
Query: Retrieve all Customers with product names, including those who have not ordered
any product
RIGHT JOIN
Query: Retrieve all Customer ID, Order ID and Product ID
SELF JOIN
Query: Find Customer with the same City.
FULL OUTER JOIN Query: Combine all Customer and Orders, even if no matching
record exists
CROSS JOIN Query: Get all possible pairs of Customer, order and order status
Experiment 6
Single-Column Index:
A database index created on a single column to improve query performance by allowing faster
retrieval of rows based on that column's values.
Composite Index:
An index created on two or more columns to speed up queries that filter or sort data based on a
combination of these columns.
Unique Index:
Ensures that all values in the indexed column(s) are distinct, preventing duplicate entries while
enhancing query performance.
View Indexes:
Indexes created on materialized views to improve performance by precomputing and storing
query results for faster access.
Drop Index Query:
Used to delete an existing index from a table to reduce maintenance overhead or if the index is
no longer required.
Single-Column Index: Create an index on the FirstName column in the Customer table to
speed up searches by name.
Composite Index: Create a composite index on the FirstName and LastName columns in
the Customer table..
Unique Index: Create a unique index on the Email column in the Customers table to ensure
no two customer can have the same email.
View Indexes
Drop Index Query: Drop an index (e.g., idx_firstname) if it’s no longer needed.
Add Index to Other Tables: Add an index on the CustomersName column in the
Customers table
Experiment 7
1. Create User Accounts
This operation is used to create new user accounts in the database system. Each user account is
assigned a unique username and password, allowing individual access and management of
database resources based on specific privileges.
4. View Privileges
This operation allows the user to check what permissions are assigned to a specific user. It is
useful for verifying and auditing user roles and access control.
5. Revoke Privilege
This operation removes specific permissions that were previously granted to a user. It is used to
limit or withdraw access to database resources when necessary.
Revoke Privileges:
Experiment 8
1. Create Views:
Views are virtual tables created from a query, representing specific data from one or
more tables without storing the data physically.
o sView for Customer Order Details: A virtual table showing selected customer
order information, such as customer name, order ID, and order date.
o View for Product Sales Count: Displays the total number of units sold for each
product by aggregating sales data.
2. Query Views:
Views can be queried like regular tables to fetch data as per the defined structure.
o Query the CustomerOrderDetails View: Retrieve specific customer order
information by selecting columns from the CustomerOrderDetails view.
o Query the ProductSalesCount View: Fetch the total sales count for each
product by querying the ProductSalesCount view.
3. Modify a View:
Views can be updated to include additional columns or alter existing query logic.
o Update the CustomerOrderDetails View to Include Customer Emails: Add
an email column to the CustomerOrderDetails view definition for enhanced
customer records.
4. Drop a View:
The DROP VIEW statement removes a view from the database without affecting the
underlying data.
o Drop the ProductSalesCount View: Deletes the ProductSalesCount view to
remove its access point.
Create Views
1. View for Customer Orders
2. View for ProductDetails Count
Query Views
1. Query the CustomerOrdersCount view
Modify a View
Update the CustomerOrders view to include email addresses
Drop a View
Drop the CustomerOrders count view
Experiment 9
Control flow functions in SQL
1.IF
The IF function evaluates a condition and returns a value based on whether the condition is
true or false.
2.CASE
The CASE function performs conditional logic, returning a value based on specified
conditions. It works like an IF-ELSE statement in programming.
3.IFNULL
The IFNULL function checks whether a value is NULL and returns a specified replacement
value if it is, otherwise, it returns the original value.
4.NULLIF
The NULLIF function compares two values and returns NULL if they are equal; otherwise,
it returns the first value.
Using NULLIF() :-
Mark Customers who have the same first and last name
Count TotalProducts:-
Experiment 10
Union: Combines the results of two SELECT statements, removing duplicates. The first
query merges data from Products with data where Stock > 50.
Intersection: Retrieves rows common to two SELECT queries using an INNER JOIN, as
shown where Stock >= 50 in both cases.
Difference: Returns rows in one SELECT query but not in another, demonstrated by
filtering products where Name is not in the list of Stock > 50.
UNION ALL:
• Combines the results of two SELECT queries without removing duplicates.
• Unlike UNION, UNION ALL keeps all rows, including repeated values.
Experiment 11
1. • Stored Procedure:
A stored procedure is a precompiled set of SQL statements stored in the database
that can be executed as a single unit to perform a specific task.
2. • Compound Functions:
Compound functions combine multiple SQL functions to perform complex
operations, such as aggregations, on data in a single query.
3. • Trigger:
A trigger is a special procedure in a database that automatically executes in
response to specific events (e.g., INSERT, UPDATE, or DELETE) on a table.
1. Stored Procedures
Add a New Student
Query to Create Procedure
Call Procedure
2. Compound Functions
Calculate Age Based on DOB
Query to Create Function
Use the Function
3. Triggers
Log Changes in Enrollment Table
Create the Log Table