Lab Two
Data Control Language
The subset of SQL that allows a database administrator to manage user privileges and used to control access and
permissions.
GRANT used to provide specific privileges to users or roles
GRANT SELECT, INSERT ON employees TO 'userl';
REVOKE: This command removes previously granted privileges
REVOKE INSERT ON employees FROM 'userl';
select*from stud
--before creating login right click on server click on properties
--click on security-click on sql server instead of windows authentication ,that mean configure the server's
authentication settings before creating the login.
Login Controls authentication (who can enter SQL Server) and exists at the server level (master
DB).
--creat at server level user/login login loginl,login login2 with passward 1234
create login loginl with password ='1234'
create login login2 with password ='1234'
--user created at database level
After creating a login, you must create a user inside a database
so that the login can access that database.
Allows access to a specific database.
Controls what objects the person can access tables.
--create user to login loginl
--create user to login loginl
create user user1 for login loginl
create user user2 for login login2
A role is a group of users with shared permissions. Instead of assigning permissions one-by-one to
each user, you grant them to a role and add users to that role
--creat role =grouop roleadd each user to the role
create role stud_group
SP_ADDROLEMEMBER 'STUD_GROUP','USERl'
SP_ADDROLEMEMBER 'STUD_GROUP','USER2'
---grant permission, after create group role login to database engine➔ login in to server and connect server for
each user by login name with password.
after that: give privilege by master server/computer server for each user
grant select, insert, update on stud to userl with grant option
---on userl server check by new qury use bd name
---write each privilege like…
use universty
select * from stud
UPDATE stud
SET stud_ex = 'f'
WHERE stud_id = 'maur/1141/15';
If you give permission user2 by user1. WITH GRANT OPTION = you can give it to
others,
run on loginone/server one
grant select,update(sfnam,slname) on stud to user2
---------revoke user
revoke grant option for select on stud from userl cascade
but without cascade SQL Server will not remove permissions user1 already gave to other users.
TCL (Transaction Control Language)
used to manage transactions in a database. It ensures data integrity and consistency by controlling how changes are
committed or rolled back within a database.
TCL commands are essential for maintaining the reliability of database operations, especially in multi-user
environments.
COMMIT
Saves all changes made during the current transaction permanently to the database.
syntax: begin transaction
Syntax: commit transaction;
e.g select * from munn
begin transaction
--UPDATE munn
--SET name = 'mes'
--WHERE id = 1;
commit transaction;
ROLLBACK
Undoes all changes made during the current transaction, reverting the database to its previous state
Syntax: rollback transaction
select * from munn
begin transaction
--delete from munn
rollback transaction;
/////
Function in database
A Database Function is a routine that accepts parameters, performs an action to perform
operations on data or database objects and returns the result of that action as a value.
Depending on the Function, the return value can be a single value.
Aggregate function in a database is a function that performs a calculation on a set of
values (multiple rows) and returns a single summarized value.
Operates on multiple rows in a table.
Example: COUNT () Function to computing total number of students record.
Query: SELECT COUNT (*) AS NumStudents FROM Students;
Scalar function is a user-defined function written in SQL and it returns a single value
each time it is invoked. SQL scalar functions contain the source code for the user-
defined function in the user-defined function definition.
Operate on single values and return a single value.
CREATE FUNCTION getSquare (@num INT)
RETURNS INT
AS
BEGIN
RETURN @num * @num;
END
;
GO
Call the function
SELECT [Link](4) --AS SquareResult;
SELECT [Link](6)--AS SquareResult;
CREATE FUNCTION GetAge (@BirthDate DATE)
RETURNS INT
AS
BEGIN
RETURN DATEDIFF(YEAR, @BirthDate, GETDATE());
END;
go
SELECT [Link]('2000-05-15'); -- Output: 25 (as of 2025)
create function calgrade(@mark int) returns
char
as begin
declare @grade varchar(6) if
@mar 0
set @grade='A+' if
@mar 5 set
@grade='A'
else if @mark 0 set
@grade='A-' else if
@mark>75 set
@grade='B+' else if
@mark set
@grade='B' else if
@mark 5 set
@grade='B-' else if
@mark 0 set
@grade='C+' else if
@mark 5 set
@grade='C' else if
@mark 0 set
@grade='D' else if
@mark 0 set
@grade='Fx' else
set @grade='F'
return @grade end
go
select stud_fname,[Link](85) as Grade from stud