0% found this document useful (0 votes)
2 views

DB Lab 2

The document outlines a series of SQL commands for managing a database and its tables, including creating, inserting, retrieving, updating, and deleting data. It also covers altering table structures, sorting, counting records, grouping data, and joining tables. Additionally, it provides syntax examples for each command to assist in practical implementation.

Uploaded by

arslancc450
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)
2 views

DB Lab 2

The document outlines a series of SQL commands for managing a database and its tables, including creating, inserting, retrieving, updating, and deleting data. It also covers altering table structures, sorting, counting records, grouping data, and joining tables. Additionally, it provides syntax examples for each command to assist in practical implementation.

Uploaded by

arslancc450
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/ 3

LAB #02

Lab Task:
• Create Table for Student data and perform Queries
1. Create a Database
CREATE DATABASE DatabaseName;
2. Switch to a Database
USE DatabaseName;
3. Create a Table
CREATE TABLE TableName (
Column1 DataType,
Column2 DataType,
Column3 DataType,
...
);
4. Insert Data into a Table
INSERT INTO TableName (Column1, Column2, Column3, ...)
VALUES (Value1, Value2, Value3, ...);
5. Retrieve All Data
SELECT * FROM TableName;
6. Retrieve Specific Columns
SELECT Column1, Column2 FROM TableName;
7. Filter Data
SELECT * FROM TableName
WHERE Condition;
8. Update Data
UPDATE TableName
SET Column1 = Value1, Column2 = Value2
WHERE Condition;
9. Delete Data
DELETE FROM TableName
WHERE Condition;
10. Add a Column
ALTER TABLE TableName
ADD ColumnName DataType;
11. Drop a Column
ALTER TABLE TableName
DROP COLUMN ColumnName;
12. Sort Data
SELECT * FROM TableName
ORDER BY ColumnName ASC/DESC;
13. Count Records
SELECT COUNT(*) FROM TableName;
14. Group Data
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName;
15. Create Another Table
CREATE TABLE TableName (
Column1 DataType PRIMARY KEY,
Column2 DataType
);
16. Join Tables
SELECT Table1.Column1, Table2.Column2
FROM Table1
JOIN Table2 ON Table1.CommonColumn = Table2.CommonColumn;
17. Delete a Table
DROP TABLE TableName;
18. Delete a Database
DROP DATABASE DatabaseName;

19. Changes in Table structure ALTER


TABLE - ADD Column
✓ Syntax
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE Staff
ADD Email varchar(255);
ALTER TABLE - DROP COLUMN
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE staff
DROP COLUMN
Email;

You might also like