Unit 4 - Rdbms Notes
Unit 4 - Rdbms Notes
1. Database Engine:
1. Core component for storing, processing, and securing
data.
2. Supports relational data, XML data, and spatial data.
3. Provides advanced features like full-text search, data
compression, and encryption.
2. Analysis Services:
Use Cases:
Early Versions:
SQL Server 6.5 and 7.0: These early versions laid the
foundation for SQL Server's capabilities, introducing features
like stored procedures, triggers, and distributed
transactions.
Modern Era:
Installation Steps:-
Run the Setup File:
Installation Center:
Feature Selection:
Instance Configuration:
Server Configuration:
1. Installation:
1. Configuration:
Post-Installation:
Query Editor:
Object Explorer:
Data Exploration:
Database Administration:
Security:
1. Offers robust security features, including user
authentication, role-based access control, and
encryption.
2. Protects sensitive data with strong security measures.
Full-Text Search:
Replication:
LocalDB:
Mainstream Support:
1. This phase typically lasts for five years after the initial
product release.
2. During this period, Microsoft provides:
1. Regular security updates and patches.
2. Bug fixes and performance improvements.
3. Technical support through various channels.
4. New features and enhancements.
Extended Support:
End of Support:
DDL Commands :
In this section, We will cover the following DDL commands as
follows.
1. Create
2. Alter
3. truncate
4. drop
5. Rename
Command-1 :
CREATE :
This command is used to create a new table in SQL. The user
has to give information like table name, column names, and their
datatypes.
Syntax –
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);
Example –
We need to create a table for storing Student information of a
particular College. Create syntax would be as below.
CREATE TABLE Student_info
(
College_Id number(2),
College_name varchar(30),
Branch varchar(10)
);
Command-2 :
ALTER :
This command is used to add, delete or change columns in the
existing table. The user needs to know the existing table name
and can do add, delete or modify tasks easily.
Syntax –
Syntax to add a column to an existing table.
ALTER TABLE table_name
ADD column_name datatype;
Example –
In our Student_info table, we want to add a new column for CGPA.
The syntax would be as below as follows.
ALTER TABLE Student_info
ADD CGPA number;
Command-3 :
TRUNCATE :
This command is used to remove all rows from the table, but the
structure of the table still exists.
Syntax –
Syntax to remove an existing table.
TRUNCATE TABLE table_name;
Example –
The College Authority wants to remove the details of all students
for new batches but wants to keep the table structure. The
command they can use is as follows.
TRUNCATE TABLE Student_info;
Command-4 :
DROP :
This command is used to remove an existing table along with its
structure from the Database.
Syntax –
Syntax to drop an existing table.
DROP TABLE table_name;
Example –
If the College Authority wants to change their Database by
deleting the Student_info Table.
DROP TABLE Student_info;
Command -5
RENAME:
It is possible to change name of table with or without data in it
using simple RENAME command.
We can rename any table object at any point of time.
Syntax –
RENAME TABLE <Table Name> To <New_Table_Name>;
Example:-
If you want to change the name of the table from Employee to
Emp we can use rename command as
RENAME TABLE Employee To EMP;
DML Commands in SQL
DML is an abbreviation of Data Manipulation Language.
INSERT
UPDATE
DELETE
SELECT
SELECT Command
INSERT Command
Syntax:
INSERT INTO <table_name> ('column_name1' <datatype>,
'column_name2' <datatype>)
VALUES ('value1', 'value2');
Example :
INSERT INTO students ('stu_id' int, 'stu_name' varchar(20), 'city'
varchar(20))
VALUES ('1', 'Nirmit', 'Gorakhpur');
UPDATE Command
DELETE Command
DDL Commands
GRANT Command
Syntax
Here,
Privilege_name
areSELECT,UPDATE,DELETE,INSERT,ALTER,ALL.
objectname is table name
user is the name of the user to whom we grant privileges
Example
REVOKE Command
Here,
Privilege_name
areSELECT,UPDATE,DELETE,INSERT,ALTER,ALL.
objectname is table name
user is the name of the user to whom we grant privileges
Example
TCL Commands
TCL includes the following commands:
1. COMMIT
Syntax:
COMMIT;
2. ROLLBACK
Syntax:
ROLLBACK;
3. SAVEPOINT
Syntax:
SAVEPOINT savepoint_name;
Data Constraints
Constraints in SQL
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
1. NOT NULL –
If we specify a field in a table to be NOT NULL. Then the field will
never accept null value. That is, you will be not allowed to insert a
new row in the table without specifying any value to this field.
For example, the below query creates a table Student with the
fields ID and NAME as NOT NULL. That is, we are bound to
specify values for these two fields every time we wish to insert a
new row.
2. UNIQUE –
This constraint helps to uniquely identify each row in the table. i.e.
for a particular column, all the rows should have unique values.
We can have more than one UNIQUE columns in a table.
For example, the below query creates a table Student where the
field ID is specified as UNIQUE. i.e, no two students can have the
same ID.
3. PRIMARY KEY –
Primary Key is a field which uniquely identifies each row in the
table. If a field in a table as primary key, then the field will not be
able to contain NULL values as well as all the rows should have
unique values for this field. So, in other words we can say that
this is combination of NOT NULL and UNIQUE constraints.
A table can have only one field as primary key. Below query will
create a table named Student and specifies the field ID as
primary key.
4. FOREIGN KEY –
Foreign Key is a field in a table which uniquely identifies each row
of a another table. That is, this field points to primary key of
another table. This usually creates a kind of link between the
tables.
Consider the two tables as shown below:
Orders
O_ID ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Customers
C_ID NAME ADDRESS
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly that the field C_ID in Orders table is the
primary key in Customers table, i.e. it uniquely identifies each row
in the Customers table. Therefore, it is a Foreign Key in Orders
table.
Syntax:
5. CHECK –
Using the CHECK constraint we can specify a condition for a field,
which should be satisfied at the time of entering values for this
field.
For example, the below query creates a table Student and
specifies the condition for the field AGE as (AGE >= 18 ). That is,
the user will not be allowed to enter any record in the table with
AGE < 18.
6. DEFAULT –
This constraint is used to provide a default value for the fields.
That is, if at the time of entering new records in the table if the
user does not specify any value for these fields then the default
value will be assigned to them.
For example, the below query will create a table named Student
and specify the default value for the field AGE as 18.
Improved Performance:
o Precompilation: SQL Server compiles the stored
procedure once, and the compiled code is stored in the
database. This eliminates the need for repeated
parsing and compilation, leading to faster execution.
o Reduced Network Traffic: By executing complex logic
on the server side, you can reduce the amount of data
transferred between the client and the server.
Enhanced Security:
Reusability:
Modularity:
Syntax
Two important syntaxes for using stored procedures in SQL are:
Parameter
SQL
CREATE PROCEDURE GetCustomersASBEGIN
SELECT CustomerID, CustomerName, City
FROM Customers;END;
SQL
EXEC GetCustomers;
SQL
CREATE PROCEDURE GetCustomersByCity
@City NVARCHAR(50)ASBEGIN
SELECT CustomerID, CustomerName
FROM Customers
WHERE City = @City;END;
SQL
EXEC GetCustomersByCity @City = 'New York';
Functions
A Function is similar to a stored procedure, but it returns a specific
value. Functions can be scalar-valued or table-valued.
Types of Functions:
Scalar-Valued Functions:
Table-Valued Functions:
Benefits of Functions:
Syntax:
Scalar-Valued Function:
SQL
CREATE FUNCTION FunctionName
(
@Parameter1 DataType,
@Parameter2 DataType,
...
)RETURNS DataTypeASBEGIN
-- SQL statements to calculate the return value
RETURN ValueEND
Table-Valued Function:
SQL
CREATE FUNCTION FunctionName
(
@Parameter1 DataType,
@Parameter2 DataType,
...
)RETURNS TABLEASRETURN
(
SELECT Column1, Column2, ...
FROM TableName
WHERE ...
)
SQL
CREATE FUNCTION CalculateDiscount
(@Price DECIMAL(10,2), @DiscountPercentage
INT)RETURNS DECIMAL(10,2)ASBEGIN
DECLARE @DiscountedPrice DECIMAL(10,2)
SET @DiscountedPrice = @Price - (@Price *
@DiscountPercentage / 100)
RETURN @DiscountedPriceEND
SQL
CREATE FUNCTION GetTopCustomers
(@TopN INT)RETURNS TABLEASRETURN
(
SELECT TOP(@TopN) CustomerID, CustomerName,
TotalPurchases
FROM Customers
ORDER BY TotalPurchases DESC
)
May or may not returns a value to the Returns a value to the calling part of the
calling part of program. program.
Does not specify the datatype of the Necessarily specifies the datatype of the
value if it is going to return after a value which it is going to return after a
calling made to it. calling made to it.
Cannot be called from the function Can be called from the procedure block
block of code. of code.