Example in SQL
1. Create Tables
CREATE TABLE Department (
DeptID INT PRIMARY KEY, -- Primary key constraint
DeptName VARCHAR(50) NOT NULL -- Not Null constraint
);
CREATE TABLE Employee (
EmpID INT PRIMARY KEY, -- Primary key constraint
EmpName VARCHAR(100) NOT NULL, -- Not Null constraint
Salary DECIMAL(10, 2) CHECK (Salary > 0), -- Check constraint
DeptID INT, -- Foreign key
FOREIGN KEY (DeptID) REFERENCES Department(DeptID) -- Foreign key
constraint
);
Insert values
INSERT INTO Department (DeptID, DeptName) VALUES (1, 'Human
Resources');
INSERT INTO Department (DeptID, DeptName) VALUES (2, 'Engineering');
INSERT INTO Department (DeptID, DeptName) VALUES (3, 'Marketing');
INSERT INTO Department (DeptID, DeptName) VALUES (4, 'Finance');
Display data
SELECT * FROM Department;
INSERT INTO Employee (EmpID, EmpName, Salary, DeptID) VALUES (1,
'Alice Johnson', 55000.00, 101), (2, 'Bob Smith', 60000.00, 102), (3,
'Charlie Brown', 70000.00, 101), (4, 'Diana Prince', 65000.00, NULL);
SELECT * FROM Employee
2. Alter Table
Add a new column:
ALTER TABLE Employee
ADD Email VARCHAR(100);
Modify an existing column:
ALTER TABLE Employee
MODIFY Email VARCHAR(150);
3. Drop Table
DROP TABLE Employee;
DROP TABLE Department;
4. Enforce Constraints
Primary Key
ALTER TABLE Employee
ADD CONSTRAINT PK_Employee PRIMARY KEY (EmpID);
Foreign Key
ALTER TABLE Employee
ADD CONSTRAINT FK_Employee_Department FOREIGN KEY (DeptID)
REFERENCES Department(DeptID);
Check
ALTER TABLE Employee
ADD CONSTRAINT CHK_Salary CHECK (Salary >= 5000);
Not Null
ALTER TABLE Employee
MODIFY EmpName VARCHAR(100) NOT NULL;
5. Create a View
Create a view to display employees with a salary greater than a specified
amount:
CREATE VIEW HighEarningEmployees AS
SELECT EmpID, EmpName, Salary, DeptName
FROM Employee
JOIN Department ON Employee.DeptID = Department.DeptID
WHERE Salary > 10000;
Explanation
Primary Key: Ensures unique identification for each row.
Foreign Key: Establishes a relationship between two tables.
Check: Ensures a column meets a condition.
Not Null: Prevents null values in a column.
Views: Provide a simplified representation of complex queries and do not
store data themselves.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Function to perform BFS on the graph
void BFS(int startNode, const vector<vector<int>>& adjList) {
vector<bool> visited(adjList.size(), false); // Track visited nodes
queue<int> q; // Queue for BFS traversal
// Start from the given node
visited[startNode] = true;
q.push(startNode);
cout << "BFS Traversal: ";
while (!q.empty()) {
int current = q.front();
q.pop();
cout << current << " ";
// Visit all neighbors of the current node
for (int neighbor : adjList[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
cout << endl;
int main() {
// Define a graph using an adjacency list
int nodes = 5; // Number of nodes
vector<vector<int>> adjList(nodes);
// Example graph edges
adjList[0] = {1, 2};
adjList[1] = {0, 3, 4};
adjList[2] = {0};
adjList[3] = {1};
adjList[4] = {1};
// Perform BFS starting from node 0
BFS(0, adjList);
return 0;