ADVANCED DB Presentation
ADVANCED DB Presentation
1. Kareem Abdullraheem
2. Ahmed Sherif
3. Nour El-Deen Osama
Topics:
A Rule is a reusable constraint applied to multiple columns to enforce data validation. For
example, a rule might restrict the age of an employee to be between 18 and 65. Unlike
constraints, a rule can be reused on multiple tables.
Scenario:
Goal:
Name VARCHAR(50),
Age INT
);
Table Structure:
1 Alice 30
Expected Output:
1 Alice 30
Output:
1 Alice 30
sql
Copy code
This removes the rule from the Age column and deletes the rule.
A User in SQL Server is a person or system that has access to the database. Each user is
linked to a login and can be assigned a default schema.
Goal:
Explanation:
This creates a user JohnD for the JohnDLogin and sets the default schema as dbo.
FROM sys.database_principals
Name Default
Schema
JohnD dbo
Explanation:
This renames the user JohnD to JDoe.
FROM sys.database_principals
Output:
Name Default
Schema
JDoe dbo
Permissions control what users can do in the database. You can GRANT a user the ability to
SELECT, UPDATE, DELETE, or INSERT on a table. You can also REVOKE permissions to
take away access.
Goal:
GRANT SELECT
ON Employees
TO JohnD;
Explanation:
This allows JohnD to view the Employees table.
As JohnD, run:
Output:
EmployeeID Name Age
1 Alice 30
REVOKE SELECT
ON Employees
FROM JohnD;
Explanation:
This removes JohnD's ability to view the Employees table.
As JohnD, run:
Output:
4. Users to a Role
Concept:
Key Commands:
Scenario:
You have a database called SalesDB. You need to create a role called DataEditor that
allows users to SELECT, INSERT, and UPDATE data in the Sales.Orders table. You will
then add two users, Emily and David, to the role. Finally, you will view the role members,
remove David, and drop the role.
Gives SELECT, INSERT, and UPDATE permissions on the Sales.Orders table to the
DataEditor role.
DP2.name AS MemberName
ON DRM.role_principal_id = DP1.principal_id
ON DRM.member_principal_id = DP2.principal_id
Expected Output:
RoleName MemberName
DataEditor Emily
DataEditor David
Expected Output:
RoleName MemberName
DataEditor Emily
5. Triggers
Concept:
A Trigger is a special kind of stored procedure that runs automatically when certain events
(like INSERT, UPDATE, or DELETE) happen on a table. Triggers are useful for enforcing
rules, auditing changes, or automatically recording updates in other tables.
Key Commands:
Scenario:
You have an Employees table. Every time an employee's salary is updated, you need to log
the change in an EmployeeSalaryAudit table. The audit should store the EmployeeID, the
OldSalary, the NewSalary, and the Date of the change.
EmployeeID INT,
OldSalary DECIMAL(10,2),
NewSalary DECIMAL(10,2),
ChangeDate DATETIME
);
ON Employees
FOR UPDATE
AS
BEGIN
SELECT
i.EmployeeID,
d.Salary AS OldSalary,
i.Salary AS NewSalary,
GETDATE() AS ChangeDate
FROM
inserted i
JOIN
END;
Creates a trigger that runs automatically every time the salary of an employee is updated. It
records changes in the EmployeeSalaryAudit table.
UPDATE Employees
WHERE EmployeeID = 3;
Expected Output:
6. Functions
Concept:
A Function is a block of code that performs a calculation and returns a single result. Unlike
stored procedures, functions can be used in SQL queries like SELECT. Functions are useful
for reusability and encapsulating business logic.
Key Commands:
Scenario:
You want to create a function that takes an EmployeeID as input and returns the Full Name
of the employee by combining their FirstName and LastName. This function will be used to
display employee names in reports.
RETURNS VARCHAR(100)
AS
BEGIN
SELECT
FROM
Employees
WHERE
EmployeeID = @EmployeeID;
RETURN @FullName;
END;
Creates a function that takes EmployeeID as input and returns the Full Name of the
employee.
Expected Output:
FullName
Emily Johnson
SELECT
EmployeeID,
dbo.fn_GetEmployeeFullName(EmployeeID) AS FullName,
Salary
FROM
Employees;
This query lists all employees with their EmployeeID, Full Name, and Salary.
Expected Output: