0% found this document useful (0 votes)
19 views7 pages

DBMS POE DBATU Practice

Uploaded by

7seagaming007
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)
19 views7 pages

DBMS POE DBATU Practice

Uploaded by

7seagaming007
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/ 7

DBMS POE

1. Data Definition Language


DDL (Data Definition Language) commands in are used to define and modify the structure of a
database. Here’s a quick demonstration with one-line syntax examples:

1. CREATE - To create a new database or table.

Syntax: CREATE TABLE Students (ID INT PRIMARY KEY, Name VARCHAR(50), Age INT);

2. ALTER - To modify an existing table structure.

Syntax: ALTER TABLE Students ADD Email VARCHAR(100);

3. DROP - To delete a table or database.

Syntax: DROP TABLE Students;

4. TRUNCATE - To remove all rows from a table (resetting it).

Syntax: TRUNCATE TABLE Students;

5. RENAME - To rename a table.

Syntax: RENAME TABLE Students TO Learners;

Each command manages the schema or structure of the database. If you'd like further examples
or explanations, let me know!
DBMS POE
2. Data Manipulation Language
DML (Data Manipulation Language) commands in are used to manage data within database
tables. Here's a demonstration with concise one-line syntax examples:

1. INSERT - To add new records into a table.

Syntax: INSERT INTO Students (ID, Name, Age) VALUES (1, 'John Doe', 20);

2. UPDATE - To modify existing records in a table.

Syntax: UPDATE Students SET Age = 21 WHERE ID = 1;

3. DELETE - To remove records from a table.

Syntax: DELETE FROM Students WHERE ID = 1;

4. SELECT - To retrieve data from a table (technically DQL but often considered part of DML).

Syntax: SELECT * FROM Students;


DBMS POE
3. Transaction Control Language
TCL (Transaction Control Language) commands in SQL are used to manage transactions
in a database. Here's a demonstration of the key commands:

1. COMMIT - Saves the changes made by the transaction permanently.


Syntax: COMMIT;

2. ROLLBACK - Reverts the changes made by the transaction to the last


committed state.
Syntax: ROLLBACK;

3. SAVEPOINT - Sets a point within a transaction to which you can later


roll back.
Syntax: SAVEPOINT Save1;

4. ROLLBACK TO SAVEPOINT - Rolls back a transaction to a specific


savepoint.
Syntax: ROLLBACK TO Save1;
DBMS POE
4. Aggregate Function
Aggregate functions in SQL perform calculations on a set of values and return a
single value. Below are examples demonstrating their use:

1. COUNT() - Returns the number of rows in a result set.


Syntax: SELECT COUNT(*) AS TotalStudents FROM Students;

2. SUM() - Returns the total sum of a numeric column.


Syntax: SELECT SUM(Age) AS TotalAge FROM Students;

3. AVG() - Returns the average value of a numeric column.


Syntax: SELECT AVG(Age) AS AverageAge FROM Students;

4. MAX() - Returns the maximum value in a column.


Syntax: SELECT MAX(Age) AS OldestStudent FROM Students;

5. MIN() - Returns the minimum value in a column.


Syntax: SELECT MIN(Age) AS YoungestStudent FROM Students;

6. GROUP BY with Aggregates - Applies the aggregate function to groups


of rows.
Syntax: SELECT Department, AVG(Age) AS AvgAgePerDept
FROM Students
GROUP BY Department;

7. HAVING Clause with Aggregates - Filters groups based on aggregate


results.
Syntax: SELECT Department, COUNT(*) AS StudentCount
FROM Students
GROUP BY Department
HAVING COUNT(*) > 5;
DBMS POE
5. Create a view and insert and delete in view
Creating a View
A view is a virtual table that contains the result of a SQL query. It doesn’t store data
itself but allows you to simplify complex queries and encapsulate them.

Syntax: CREATE VIEW View_AdultStudents AS


SELECT ID, Name, Age
FROM Students
WHERE Age > 18;

Inserting Data into a View


To insert data into a view, the view must be updatable, meaning it directly maps to
the base table without any complex logic (e.g., joins, groupings).

Syntax: INSERT INTO View_AdultStudents (ID, Name, Age)


VALUES (3, 'Alice', 20);
This will insert the record into the Students table, as the view reflects the table.

Deleting Data Through a View


Similar to insertion, you can delete rows via a view, provided the view is updatable.
Syntax: DELETE FROM View_AdultStudents
WHERE ID = 3;
DBMS POE
6. String Operations
1. CONCAT - Concatenate two or more strings.
Syntax: SELECT CONCAT(FirstName, ' ', LastName) AS FullName FROM
Students;

2. LENGTH - Find the length of a string.


Syntax: SELECT LENGTH(Name) AS NameLength FROM Students;

3. LOWER - Convert a string to lowercase.


Syntax: SELECT LOWER(Name) AS LowerCaseName FROM Students;

4. UPPER - Convert a string to uppercase.


Syntax: SELECT UPPER(Name) AS UpperCaseName FROM Students;

5. SUBSTRING (or SUBSTR) - Extract a substring from a string.


Syntax: SELECT SUBSTRING(Name, 1, 3) AS FirstThreeChars FROM
Students;

6. TRIM - Remove leading and trailing spaces from a string.


Syntax: SELECT TRIM(' John ') AS TrimmedName;

7. CHAR_LENGTH - Return the length of a string (character count).


Syntax: SELECT CHAR_LENGTH(Name) AS CharCount FROM Students;

8. FORMAT - Format a string or number.


Syntax: SELECT FORMAT(12345.678, 2) AS FormattedNumber;
DBMS POE
7. Perform queries involving like , between , IN
1. LIKE - For pattern matching with wildcards.
• %: Matches zero or more characters.
• _: Matches exactly one character.
Example 1: Names starting with 'A'
Syntax: SELECT * FROM Students
WHERE Name LIKE 'A%';

Example 2: Names ending with 'n'


Syntax: SELECT * FROM Students
WHERE Name LIKE '%n';

Example 3: Names containing 'an'


Syntax: SELECT * FROM Students
WHERE Name LIKE '%an%';

Example 4: Names with exactly 4 characters


Syntax: SELECT * FROM Students
WHERE Name LIKE '____';

2. BETWEEN - To filter data within a range.


Example 1: Students aged between 18 and 25
Syntax: SELECT * FROM Students
WHERE Age BETWEEN 18 AND 25;

3. IN - To specify multiple values in a filter.


Example 1: Students in specific departments
Syntax: SELECT * FROM Students
WHERE Department IN ('CSE', 'ECE', 'IT');

Combined Query Example:


Find students aged between 18 and 25, whose names start with 'A', and are in 'CSE' or
'ECE'.
Syntax: SELECT * FROM Students
WHERE Age BETWEEN 18 AND 25
AND Name LIKE 'A%'
AND Department IN ('CSE', 'ECE');

You might also like