VTP Asp - Net (16-30)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

VOCATIONAL TRAINING PROGRAM

FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Class-.net Topic - Course Content Date-10/06/2024

Lecture 16 :
A login is a simple credential for accessing SQL Server. For example, you provide your username
and password when logging on to Windows or even your e-mail account. This username and
password builds up the credentials. Therefore, credentials are simply a username and a
password.

SQL Server allows four types of logins −

• A login based on Windows credentials.


• A login specific to SQL Server.
• A login mapped to a certificate.
• A login mapped to asymmetric key.

In this tutorial, we are interested in logins based on Windows Credentials and logins specific to
SQL Server.

Logins based on Windows credentials allow you to log in to SQL Server using a Windows
username and password. If you need to create your own credentials (username and password,)
you can create a login specific to SQL Server.

To create, alter, or remove a SQL Server login, you can take one of two approaches −

• Using SQL Server Management Studio.


• Using T-SQL statements.

Following methods are used to create Login −

First Method – Using SQL Server Management Studio

Step 1 − After connecting to SQL Server Instance, expand logins folder as shown in the following
snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − Right-click on Logins, then click Newlogin and the following screen will open.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 3 − Fill the Login name, Password and Confirm password columns as shown in the above
screen and then click OK.

Login will be created as shown in the following image.

Second Method – Using T-SQL Script


Create login yourloginname with password='yourpassword'

To create login name with TestLogin and password ‘P@ssword’ run below the following query.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 17 :

Database is a collection of objects such as table, view, stored procedure, function, trigger, etc.

In MS SQL Server, two types of databases are available.

•System databases
•User Databases
System Databases

System databases are created automatically when we install MS SQL Server. Following is a list
of system databases −

•Master
•Model
•MSDB
•Tempdb
•Resource (Introduced in 2005 version)
•Distribution (It’s for Replication feature only)
User Databases

User databases are created by users (Administrators, developers, and testers who have access
to create databases).

Following methods are used to create user database.

Method 1 – Using T-SQL Script or Restore Database

Following is the basic syntax for creating database in MS SQL Server.

Create database <yourdatabasename>

OR

Restore Database <Your database name> from disk = '<Backup file location + file name>
Example

To create database called ‘Testdb’, run the following query.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Create database Testdb

OR

Restore database Testdb from disk = 'D:\Backup\Testdb_full_backup.bak'

Note − D:\backup is location of backup file and Testdb_full_backup.bak is the backup file name

Method 2 – Using SQL Server Management Studio

Connect to SQL Server instance and right-click on the databases folder. Click on new database
and the following screen will appear.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Enter the database name field with your database name (example: to create database with the
name ‘Testdb’) and click OK. Testdb database will be created as shown in the following
snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 18:

Method 1 – Using SQL Server Management Studio


Example

To run a query to select backup history on database called ‘msdb’, select the msdb database as
shown in the following snapshot.

Method 2 – Using T-SQL Script


Use <your database name>
Example

To run your query to select backup history on database called ‘msdb’, select the msdb database
by executing the following query.

Exec use msdb

The query will open msdb database. You can execute the following query to select backup
history.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Select * from backupset

To remove your database from MS SQL Server, use drop database command. Following two
methods can be used for this purpose.

Method 1 – Using T-SQL Script

Following is the basic syntax for removing database from MS SQL Server.

Drop database <your database name>


Example

To remove database name ‘Testdb’, run the following query.

Drop database Testdb


Method 2 – Using MS SQL Server Management Studio

Connect to SQL Server and right-click the database you want to remove. Click delete command
and the following screen will appear.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Click OK to remove the database (in this example, the name is Testdb as shown in the above
screen) from MS SQL Server.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Backup is a copy of data/database, etc. Backing up MS SQL Server database is essential for
protecting data. MS SQL Server backups are mainly three types − Full or Database, Differential
or Incremental, and Transactional Log or Log.

Backup database can be done using either of the following two methods.

Method 1 – Using T-SQL


Full Type
Backup database <Your database name> to disk = '<Backup file location + file name>'
Differential Type
Backup database <Your database name> to
disk = '<Backup file location + file name>' with differential
Log Type
Backup log <Your database name> to disk = '<Backup file location + file name>'
Example

The following command is used for full backup database called 'TestDB' to the location 'D:\'
with backup file name 'TestDB_Full.bak'

Backup database TestDB to disk = 'D:\TestDB_Full.bak'

The following command is used for differential backup database called 'TestDB' to the location
'D:\' with backup file name 'TestDB_diff.bak'

Backup database TestDB to disk = 'D:\TestDB_diff.bak' with differential

The following command is used for Log backup database called 'TestDB' to the location 'D:\'
with backup file name 'TestDB_log.trn'

Backup log TestDB to disk = 'D:\TestDB_log.trn'


Method 2 – Using SSMS (SQL SERVER Management Studio)

Step 1 − Connect to database instance named 'TESTINSTANCE' and expand databases folder as
shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − Right-click on 'TestDB' database and select tasks. Click Backup and the following screen
will appear.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 3 − Select backup type (Full\diff\log) and make sure to check destination path which is
where the backup file will be created. Select options at the top left corner to see the following
screen.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 4 − Click OK to create 'TestDB' database full backup as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 19:

Restoring is the process of copying data from a backup and applying logged transactions to the
data. Restore is what you do with backups. Take the backup file and turn it back into a
database.

The Restore database option can be done using either of the following two methods.

Method 1 – T-SQL
Syntax
Restore database <Your database name> from disk = '<Backup file location + file name>'
Example

The following command is used to restore database called 'TestDB' with backup file name
'TestDB_Full.bak' which is available in 'D:\' location if you are overwriting the existed database.

Restore database TestDB from disk = ' D:\TestDB_Full.bak' with replace

If you are creating a new database with this restore command and there is no similar path of
data, log files in target server, then use move option like the following command.

Make sure the D:\Data path exists as used in the following command for data and log files.

RESTORE DATABASE TestDB FROM DISK = 'D:\ TestDB_Full.bak' WITH MOVE 'TestDB' TO
'D:\Data\TestDB.mdf', MOVE 'TestDB_Log' TO 'D:\Data\TestDB_Log.ldf'
Method 2 – SSMS (SQL SERVER Management Studio)

Step 1 − Connect to database instance named 'TESTINSTANCE' and right-click on databases


folder. Click Restore database as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − Select device radio button and click on ellipse to select the backup file as shown in the
following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 3 − Click OK and the following screen pops up.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 4 − Select Files option which is on the top left corner as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 5 − Select Options which is on the top left corner and click OK to restore 'TestDB' database
as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Method 1 – Using T-SQL


Syntax
Create user <username> for login <loginname>
Example

To create user name 'TestUser' with mapping to Login name 'TestLogin' in TestDB database, run
the following query.

create user TestUser for login TestLogin

Where 'TestLogin' is the login name which was created as part of the Login creation

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Method 2 – Using SSMS (SQL Server Management Studio)

Note − First we have to create Login with any name before creating a user account.

Let’s use Login name called 'TestLogin'.

Step 1 − Connect SQL Server and expand databases folder. Then expand database called
'TestDB' where we are going to create the user account and expand the security folder. Right-
click on users and click on the new user to see the following screen.

Step 2 − Enter 'TestUser' in the user name field and click on ellipse to select the Login name
called 'TestLogin' as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 3 − Click OK to display login name. Again click OK to create 'TestUser' user as shown in the
following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lectures 20:

MS SQL Server - Services

MS SQL Server provides the following two services which is mandatory for databases creation
and maintenance. Other add-on services available for different purposes are also listed.

• SQL Server
• SQL Server Agent
Other Services
• SQL Server Browser
• SQL Server Full Text Search
• SQL Server Integration Services
• SQL Server Reporting Services
• SQL Server Analysis Services

The above services can be availed using the following method.

Start Services

To start any of the services, either of the following two methods can be used.

Method 1 – Services.msc

Step 1 − Go to Run, type services.msc and click OK. The following screen appears.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − To start service, right-click on service, click Start button. Services will start as shown in
the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Method 2 – SQL Server Configuration Manager

Step 1 − Open configuration manager using the following process.

Start → All Programs → MS SQL Server 2012 → Configuration Tools → SQL Server configuration
manager.

Step 2 − Select the service name, right-click and click on start option. Services will start as
shown in the following snapshot.

Stop Services

To stop any of the services, either of the following three methods can be used.

Method 1 - Services.msc

Step 1 − Go to Run, type services.msc and click OK. The following screen appears.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − To stop services, right-click on service and click Stop. The selected service will be
stopped as shown in the following snapshot.

Method 2 – SQL Server Configuration Manager

Step 1 − Open configuration manager using the following process.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Start → All Programs → MS SQL Server 2012 → Configuration Tools → SQL Server configuration
manager.

Step 2 − Select the service name, right-click and click Stop option. The selected service will be
stopped as shown in the following snapshot.

Method 3 – SSMS (SQL Server Management Studio)

Step 1 − Connect to the instance as shown in the following snapshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 2 − Right-click on instance name and click Stop option. The following screen appears.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 3 − Click Yes button and the following screen will open.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Step 4 − Click Yes option on the above screen to stop SQL Server agent service. The services will
be stopped as shown in the following screenshot.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 21:

SQL Server data type is an attribute that specifies types of data of any object. Each column,
variable and expression has related data type in SQL Server. These data types can be used while
creating tables. You can choose a particular data type for a table column based on your
requirement.

SQL Server offers seven categories including other category of data types for use.

Exact Numeric Types


Type From To

bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807

int -2,147,483,648 2,147,483,647

smallint -32,768 32,767

tinyint 0 255

bit 0 1

decimal -10^38 +1 10^38 –1

numeric -10^38 +1 10^38 –1

money -922,337,203,685,477.5808 +922,337,203,685,477.5807

smallmoney -214,748.3648 +214,748.3647

Numeric and decimal are Fixed precision and scale data types and are functionally equivalent.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Approximate Numeric Types
Type From To

Float -1.79E + 308 1.79E + 308

Real -3.40E + 38 3.40E + 38

Date and Time Types


Type From To

datetime(3.33 milliseconds Jan 1, 1753 Dec 31, 9999


accuracy)

Jan 1, 1900 Jun 6, 2079


smalldatetime(1 minute accuracy)

date(1 day accuracy. Introduced in Jan 1, 0001 Dec 31, 9999


SQL Server 2008)

datetimeoffset(100 nanoseconds
Jan 1, 0001 Dec 31, 9999
accuracy. Introduced in SQL Server
2008)

datetime2(100 nanoseconds
Jan 1, 0001 Dec 31, 9999
accuracy. Introduced in SQL Server
2008)

time(100 nanoseconds accuracy. 00:00:00.0000000 23:59:59.9999999


Introduced in SQL Server 2008)
Character Strings
Sr.No Type & Description

char
1
Fixed-length non-Unicode character data with a maximum length of
8,000 characters.

2
varchar

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Variable-length non-Unicode data with a maximum of 8,000


characters.

Varchar (max)
3
Variable-length non-Unicode data with a maximum length of 231
characters (Introduced in SQL Server 2005).

text
4
Variable-length non-Unicode data with a maximum length of
2,147,483,647 characters
Unicode Character Strings
Sr.No Type & Description

nchar
1
Fixed-length Unicode data with a maximum length of 4,000
characters.

nvarchar
2
Variable-length Unicode data with a maximum length of 4,000
characters.

Nvarchar (max)
3
Variable-length Unicode data with a maximum length of
230 characters (Introduced in SQL Server 2005).

ntext
4
Variable-length Unicode data with a maximum length of
1,073,741,823 characters.
Binary Strings
Sr.No Type & Description

1 binary
Fixed-length binary data with a maximum length of 8,000 bytes.

2 varbinary
Variable-length binary data with a maximum length of 8,000 bytes.

3
varbinary(max)

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Variable-length binary data with a maximum length of 231 bytes


(Introduced in SQL Server 2005).

image
4
Variable-length binary data with a maximum length of 2,147,483,647
bytes.
Other Data Types
• sql_variant − Stores values of various SQL Server-supported data types, except text,
ntext, and timestamp.
• timestamp − Stores a database-wide unique number that gets updated every time a row
gets updated.
• uniqueidentifier − Stores a globally unique identifier (GUID).
• xml − Stores XML data. You can store XML instances in a column or a variable
(Introduced in SQL Server 2005).
• cursor − A reference to a cursor.
• table − Stores a result set for later processing.
• hierarchyid − A variable length, system data type used to represent position in a
hierarchy (Introduced in SQL Server 2008).

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 22:

Creating a basic table involves naming the table and defining its columns and each column's
data type.

The SQL Server CREATE TABLE statement is used to create a new table.

Syntax

Following is the basic syntax of CREATE TABLE statement −

CREATE TABLE table_name(


column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns ));

CREATE TABLE is the keyword telling the database system what you want to do. In this case,
you want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement. Then in brackets comes the list defining each column in the table and what
sort of data type it is. The syntax becomes clearer to understand with the following example.

A copy of an existing table can be created using a combination of the CREATE TABLE statement
and the SELECT statement. You can check complete details at Create Table Using another Table.

Example

In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL are the
constraints showing that these fields cannot be NULL while creating records in this table −

CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
PRIMARY KEY (ID));

You can verify if your table has been created successfully by looking at the message displayed
by the SQL server, otherwise you can use the following command −

exec sp_columns CUSTOMERS

The above command produces the following output.

TABLE_QUALIFIER TABLE_OWNER TABLE_NAME COLUMN_NAME DATA_TYPE


TYPE_NAME
PRECISION LENGTH SCALE RADIX NULLABLE REMARKS COLUMN_DEF SQL_DATA_TYPE
SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION IS_NULLABLE
SS_DATA_TYPE

TestDB dbo CUSTOMERS ID 4 int 10 4 0 10 0


NULL NULL 4 NULL NULL 1 NO 56

TestDB dbo CUSTOMERS NAME 12 varchar 20 20 NULL NULL 0


NULL NULL 12 NULL 20 2 NO 39

TestDB dbo CUSTOMERS AGE 4 int 10 4 0 10 0


NULL NULL 4 NULL NULL 3 NO 56

TestDB dbo CUSTOMERS ADDRESS 1 char 25 25 NULL NULL 1


NULL NULL 1 NULL 25 4 YES 39

TestDB dbo CUSTOMERS SALARY 3 decimal 18 20 2 10 1


NULL NULL 3 NULL NULL 5 YES 106

You can now see that CUSTOMERS table is available in your database which you can use to
store required information related to customers.

Kickstart Your Career

Get certified by completing the course

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 23:

The SQL Server INSERT INTO statement is used to add new rows of data to a table in the
database.

Syntax

Following are the two basic syntaxes of INSERT INTO statement.

INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]


VALUES (value1, value2, value3,...valueN);

Where column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.

You need not specify the column(s) name in the SQL query if you are adding values for all the
columns of the table. But make sure the order of the values is in the same order as the columns
in the table. Following is the SQL INSERT INTO syntax −

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

Example

Following statements will create six records in CUSTOMERS table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (6, 'Komal', 22, 'MP', 4500.00 );

Syntax

You can create a record in CUSTOMERS table using second syntax as follows −

INSERT INTO CUSTOMERS VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );

All the above statements will produce the following records in CUSTOMERS table −

ID NAME AGE ADDRESS SALARY


1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
Populate One Table Using Another Table

You can populate data into a table through SELECT statement over another table provided
another table has a set of fields, which are required to populate first table. Following is the
syntax −

INSERT INTO first_table_name


SELECT column1, column2, ...columnN
FROM second_table_name
[WHERE condition];

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 24:

SQL Server JOINS

In real life, we store our data in multiple logical tables that are linked together by a common key
value in relational databases like SQL Server, Oracle, MySQL, and others. As a result, we
constantly need to get data from two or more tables into the desired output based on some
conditions. We can quickly achieve this type of data in SQL Server using the SQL JOIN clause. This
article gives a complete overview of JOIN and its different types with an example.

The join clause allows us to retrieve data from two or more related tables into a meaningful
result set. We can join the table using a SELECT statement and a join condition. It indicates how
SQL Server can use data from one table to select rows from another table. In general, tables are
related to each other using foreign key constraints.

In a JOIN query, a condition indicates how two tables are related:

o Choose columns from each table that should be used in the join. A join condition indicates
a foreign key from one table and its corresponding key in the other table.
o Specify the logical operator to compare values from the columns like =, <, or >.

Types of JOINS in SQL Server

SQL Server mainly supports four types of JOINS, and each join type defines how two tables are
related in a query. The following are types of join supports in SQL Server:

Backward Skip 10sPlay VideoForward Skip 10s

1. INNER JOIN
2. SELF JOIN
3. CROSS JOIN
4. OUTER JOIN

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Let us discuss each of these joins in detail.

INNER JOIN

This JOIN returns all records from multiple tables that satisfy the specified join condition. It is the
simple and most popular form of join and assumes as a default join. If we omit the INNER
keyword with the JOIN query, we will get the same output.

The following visual representation explains how INNER JOIN returns the matching records
from table1 and table2:

INNER JOIN Syntax

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

The following syntax illustrates the use of INNER JOIN in SQL Server:

SELECT columns
FROM table1
INNER JOIN table2 ON condition1
INNER JOIN table3 ON condition2

INNER JOIN Example

Let us first create two tables "Student" and "Fee" using the following statement:

CREATE TABLE Student (


id int PRIMARY KEY IDENTITY,
admission_no varchar(45) NOT NULL,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
age int,
city varchar(25) NOT NULL
);

CREATE TABLE Fee (


admission_no varchar(45) NOT NULL,
course varchar(45) NOT NULL,
amount_paid int,
);

Next, we will insert some records into these tables using the below statements:

INSERT INTO Student (admission_no, first_name, last_name, age, city)


VALUES (3354,'Luisa', 'Evans', 13, 'Texas'),
(2135, 'Paul', 'Ward', 15, 'Alaska'),
(4321, 'Peter', 'Bennett', 14, 'California'),
(4213,'Carlos', 'Patterson', 17, 'New York'),
(5112, 'Rose', 'Huges', 16, 'Florida'),

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
(6113, 'Marielia', 'Simmons', 15, 'Arizona'),
(7555,'Antonio', 'Butler', 14, 'New York'),
(8345, 'Diego', 'Cox', 13, 'California');

INSERT INTO Fee (admission_no, course, amount_paid)


VALUES (3354,'Java', 20000),
(7555, 'Android', 22000),
(4321, 'Python', 18000),
(8345,'SQL', 15000),
(5112, 'Machine Learning', 30000);

Execute the SELECT statement to verify the records:

Table: Student

Table: Fee

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

We can demonstrate the INNER JOIN using the following command:

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.


amount_paid
FROM Student
INNER JOIN Fee
ON Student.admission_no = Fee.admission_no;

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 25:

CROSS JOIN

CROSS JOIN in SQL Server combines all of the possibilities of two or more tables and returns a
result that includes every row from all contributing tables. It's also known as CARTESIAN
JOIN because it produces the Cartesian product of all linked tables. The Cartesian product
represents all rows present in the first table multiplied by all rows present in the second table.

The below visual representation illustrates the CROSS JOIN. It will give all the records
from table1 and table2 where each row is the combination of rows of both tables:

CROSS JOIN Syntax

The following syntax illustrates the use of CROSS JOIN in SQL Server:

SELECT column_lists
FROM table1
CROSS JOIN table2;

Example

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
We can demonstrate the CROSS JOIN using the following command:

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.


amount_paid
FROM Student
CROSS JOIN Fee
WHERE Student.admission_no = Fee.admission_no;

This command gives the below result:

OUTER JOIN

OUTER JOIN in SQL Server returns all records from both tables that satisfy the join condition. In
other words, this join will not return only the matching record but also return all unmatched rows
from one or both tables.

We can categories the OUTER JOIN further into three types:

o LEFT OUTER JOIN


o RIGHT OUTER JOIN
o FULL OUTER JOIN

LEFT OUTER JOIN

The LEFT OUTER JOIN retrieves all the records from the left table and matching rows from the
right table. It will return NULL when no matching record is found in the right side table. Since
OUTER is an optional keyword, it is also known as LEFT JOIN.

The below visual representation illustrates the LEFT OUTER JOIN:

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

LEFT OUTER JOIN Syntax

The following syntax illustrates the use of LEFT OUTER JOIN in SQL Server:

SELECT column_lists
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 26:

What are the Benefits of using a Stored Procedure in SQL?

Stored procedures provide some crucial benefits, which are:

• Reusable: As mentioned, multiple users and applications can easily use and reuse
stored procedures by merely calling it.

• Easy to modify: You can quickly change the statements in a stored procedure as and
when you want to, with the help of the ALTER TABLE command.

• Security: Stored procedures allow you to enhance the security of an application or a


database by restricting the users from direct access to the table.

• Low network traffic: The server only passes the procedure name instead of the whole
query, reducing network traffic.

• Increases performance: Upon the first use, a plan for the stored procedure is created
and stored in the buffer pool for quick execution for the next time.

How to Create a Simple Stored Procedure in SQL?

Creating a stored procedure in SQL is as easy as it can get. The syntax of SQL stored procedure
is:

CREATE or REPLACE PROCEDURE name(parameters)

AS

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
variables;

BEGIN;

//statements;

END;

In the syntax mentioned above, the only thing to note here are the parameters, which can be
the following three types:

• IN: It is the default parameter that will receive input value from the program

• OUT: It will send output value to the program

• IN OUT: It is the combination of both IN and OUT. Thus, it receives from, as well as
sends a value to the program

Note: You will work with and look at examples for different parameters in this article.

You will use the syntax to create a simple stored procedure in SQL. But before that, create two
tables using the CREATE TABLE command that you will use throughout the article. You will also
insert some values in them using the INSERT INTO command.

CREATE TABLE Car(

CarID INT,

CarName VARCHAR(100)

);

INSERT INTO Car VALUES (101,'Mercedes-Benz');

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
INSERT INTO Car VALUES (201,'BMW');

INSERT INTO Car VALUES (301,'Ferrari');

INSERT INTO Car VALUES (401,'Lamborghini');

INSERT INTO Car VALUES (501,'Porsche');

SELECT * FROM Car;

Output:

Now create the second table named CarDescription.

CREATE TABLE CarDescription(

CarID INT,

CarDescription VARCHAR(800)

);

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
INSERT INTO CarDescription VALUES (101,'Luxury vehicle from the German automotive');

INSERT INTO CarDescription VALUES (201,'Luxury motorcycle from the German automotive');

INSERT INTO CarDescription VALUES (301,'Luxury sports car from the Italian manufacturer');

INSERT INTO CarDescription VALUES (401,'Luxury SUV from the Italian automotive');

INSERT INTO CarDescription VALUES (501,'High-performance sports car from the German
manufacturer');

SELECT * FROM CarDescription;

Output:

Now that you have created both the tables, start creating the stored procedure in SQL with the
syntax mentioned earlier. For the simple procedure, you will have to use the JOIN keyword to
join both the tables, and output a new one with CarID, CarName, and CarDescription.

CREATE PROCEDURE GetCarDesc

AS

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
BEGIN

SET NOCOUNT ON

SELECT C.CarID,C.CarName,CD.CarDescription FROM

Car C

INNER JOIN CarDescription CD ON C.CarID=CD.CarID

END

This will create the stored procedure, and you will see the “command(s) executed successfully”
message in Microsoft SQL Server Management Studio. Now, since you have created the
procedure, it's time to execute it. The syntax to execute the procedure is:

EXEC procedure_name

Let’s execute the procedure we have created.

EXEC GetCarDesc;

Output:

As you can see in the output, the stored procedure executed the Join statement and gave the
desired result.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 27:
OOP Features

Here are the key features of OOP:

• Object Oriented Programming (OOP) is a programming model where programs are


organized around objects and data rather than action and logic.
• OOP allows decomposing a problem into many entities called objects and then
building data and functions around these objects.
• A class is the core of any modern object-oriented programming language such as
C#.
• In OOP languages, creating a class for representing data is mandatory.
• A class is a blueprint of an object that contains variables for storing data and
functions to perform operations on the data.
• A class will not occupy any memory space; hence, it is only a logical representation
of data.

To create a class, you use the keyword "class" followed by the class name:

class Employee
{

}
C#

Copy

Object
1. The software is divided into several small units called objects. The data and
functions are built around these objects.
2. An object's data can be accessed only by the functions associated with that object.
3. The functions of one object can access the functions of another object.

Objects are the basic run-time entities of an object-oriented system. They may represent a
person, a place, or any item the program must handle.

• "An object is a software bundle of related variables and methods."


• "An object is an instance of a class."

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

A class will not occupy any memory space. Hence to work with the data represented by the
class, you must create a variable for the class, which is called an object.

• When an object is created using the new operator, memory is allocated for the
class in a heap, the object is called an instance, and its starting address will be
stored in the object in stack memory.
• When an object is created without the new operator, memory will not be allocated
in a heap; in other words, an instance will not be created, and the object in the
stack will contain the value null.
• When an object contains null, it is impossible to access the class members using
that object.
class Employee
{

}
C#

Copy

Syntax to create an object of class Employee:

Employee objEmp = new Employee();


C#

Copy

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
OOPs Concepts

The key concepts of OOPs are

1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Abstraction

Abstraction is "To represent the essential feature without representing the background details."

• Abstraction lets you focus on what the object does instead of how it does it.
• Abstraction provides a generalized view of your classes or objects by providing
relevant information.
• Abstraction is the process of hiding the working style of an object and showing the
information about an object understandably.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 28:
Real-world Example of Abstraction

Suppose you have an object Mobile Phone.

Suppose you have three mobile phones as in the following:

• Nokia 1400 (Features: Calling, SMS)


• Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
• BlackBerry (Features: Calling, SMS, FM Radio, MP3, Camera, Video Recording,
Reading E-mails)

Abstract information (necessary and common information) for the "Mobile Phone" object is
that it calls any number and can send an SMS.

So, for a mobile phone object, you will have the abstract class as in the following,

abstract class MobilePhone {


public void Calling();
public void SendSMS();
}
public class Nokia1400: MobilePhone {}
public class Nokia2700: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
}
public class BlackBerry: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}
C#

Copy

Abstraction means putting all the necessary variables and methods in a class.

Example

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
If somebody in your college tells you to fill in an application form, you will provide your details,
like name, address, date of birth, which semester, the percentage you have, etcetera.

If some doctor gives you an application, you will provide the details, like name, address, date of
birth, blood group, height, and weight.

See in the preceding example what is in common?

Age, name, and address, so you can create a class that consists of the common data. That is
called an abstract class.

That class is not complete, and other classes can inherit it.

Encapsulation

Wrapping up a data member and a method together into a single unit (in other words, class) is
called Encapsulation. Encapsulation is like enclosing in a capsule. That is, enclosing the related
operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book, etcetera. It means this is
the property of encapsulating members and functions.

class Bag {
book;
pen;
ReadBook();
}
C#

Copy

• Encapsulation means hiding the internal details of an object, in other words, how
an object does something.
• Encapsulation prevents clients from seeing its inside view, where the behavior of
the abstraction is implemented.
• Encapsulation is a technique used to protect the information in an object from
another object.
• Hide the data for security, such as making the variables private, and expose the
property to access the private data that will be public.

So, when you access the property, you can validate the data and set it.

Example 1
class Demo {

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
private int _mark;
public int Mark {
get {
return _mark;
}
set {
if (_mark > 0) _mark = value;
else _mark = 0;
}
}
}
C#

Copy

Real-world Example of Encapsulation

Let's use as an example Mobile Phones and Mobile Phone Manufacturers.

Suppose you are a Mobile Phone Manufacturer and have designed and developed a Mobile
Phone design (a class). Now by using machinery, you are manufacturing Mobile Phones
(objects) for selling; when you sell your Mobile Phone, the user only learns how to use the
Mobile Phone but not how the Mobile Phone works.

This means that you are creating the class with functions and by with objects (capsules), of
which you are making available the functionality of your class by that object and without
interference in the original class.

Example 2
TV operation

It is encapsulated with a cover, and we can operate it with a remote, and there is no need to
open the TV to change the channel. Everything is private except the remote, so anyone can
access the remote to operate and change the things on the TV.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 29:
Inheritance

When a class includes a property of another class, it is known as inheritance. Inheritance is a


process of object reusability.

For example, a child includes the properties of its parents.

public class ParentClass {


public ParentClass() {
Console.WriteLine("Parent Constructor.");
}
public void print() {
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass: ParentClass {
public ChildClass() {
Console.WriteLine("Child Constructor.");
}
public static void Main() {
ChildClass child = new ChildClass();
child.print();
}
}
C#

Copy

Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
Polymorphism
Polymorphism means one name, many forms. One function behaves in different forms. In other
words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism


Example 1

A teacher behaves with his students.

A teacher behaves with their seniors.

Here the teacher is an object, but the attitude is different in different situations.

Example 2

A person behaves like a son in a house at the same time that the person behaves like an
employee in an office.

Example 3

Your mobile phone, one name but many forms:

• As phone
• As camera
• As mp3 player
• As radio

The easier way to understand abstraction and encapsulation is as follows.

Real-world Example
Use an example of a Mobile Phone

You have a Mobile Phone; you can dial a number using keypad buttons. You don't even know
how these are working internally. This is called abstraction. You only have the information that
is necessary to dial a number. But the not internal working of the mobile. But how does the
Mobile Phone work internally? How are the keypad buttons connected to the internal circuit?
That is called Encapsulation.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR

Lecture 30:

Polymorphism means “Many Forms”. In polymorphism, poly means “Many,” and morph means
“Forms.” polymorphism is one of the main pillars in Object Oriented Programming. It allows you
to create multiple methods with the same name but different signatures in the same class. The
same name methods can also be in derived classes.

There are two types of polymorphism,

1. Method Overloading
2. Method Overriding

In this article, I will explain the method overloading and method overriding concept in C#.
Furthermore, I will try to demonstrate step-by-step differences between these.

Method Overloading

Method Overloading is a type of polymorphism. It has several names like “Compile Time
Polymorphism” or “Static Polymorphism,” and sometimes it is called “Early Binding”.

Method Overloading means creating multiple methods in a class with the same names but
different signatures (Parameters). It permits a class, struct, or interface to declare multiple
methods with the same name with unique signatures.

The compiler automatically calls the required method to check the number of parameters and
their type passed into that method.

using System;
namespace DemoCsharp
{
class Program
{
public int Add(int num1, int num2)
{
return (num1 + num2);
}
public int Add(int num1, int num2, int num3)
{
return (num1 + num2 + num3);
}
public float Add(float num1, float num2)

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
{
return (num1 + num2);
}
public string Add(string value1, string value2)
{
return (value1 + " " + value2);
}
static void Main(string[] args)
{
Program objProgram = new Program();
Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2));
Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8));
Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f));
Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello",
"world"));
Console.ReadLine();
}
}
}
C#

Copy

In the above example, you can see four methods with the same name, but the type of
parameters or several parameters is different. So when you call Add(4,5), the compiler
automatically calls the method, which has two integer parameters. On the other hand, when
you call Add(“hello”,”world”), the compiler calls the method, which has two string parameters.
So basically, in method overloading compiler checks, which method should be called at the
compilation time.

Note: Changing the method's return type does not overload the method. You cannot create a
method overloaded to vary only by return type.
Method Overriding

Method Overriding is a type of polymorphism. It has several names like “Run Time
Polymorphism” or “Dynamic Polymorphism,” and sometimes it is called “Late Binding”.

Method Overriding means having two methods with the same name and same signatures
[parameters]; one should be in the base class, and another method should be in a derived class
[child class]. You can override the functionality of a base class method to create the same name
method with the same signature in a derived class. You can achieve method overriding using
inheritance. Virtual and Override keywords are used to achieve method overriding.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
using System;
namespace DemoCsharp
{
class BaseClass
{
public virtual int Add(int num1, int num2)
{
return (num1 + num2);
}
}
class ChildClass: BaseClass
{
public override int Add(int num1, int num2)
{
if (num1 <= 0 || num2 <= 0)
{
Console.WriteLine("Values could not be less than zero or equals to zero");
Console.WriteLine("Enter First value : ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter First value : ");
num2 = Convert.ToInt32(Console.ReadLine());
}
return (num1 + num2);
}
}
class Program
{
static void Main(string[] args)
{
BaseClass baseClassObj;
baseClassObj = new BaseClass();
Console.WriteLine("Base class method Add :" + baseClassObj.Add(-3, 8));
baseClassObj = new ChildClass();
Console.WriteLine("Child class method Add :" + baseClassObj.Add(-2, 2));
Console.ReadLine();
}
}
}
C#

Copy

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com
VOCATIONAL TRAINING PROGRAM
FUNDED BY
PANKI THERMAL EXTENSION PROJECT (PTEP), KANPUR
ORGANISED BY
INDIAN INSTITUTE OF TECHNOLOGY (IIT), KANPUR
In the above example, I have created two same-name methods in the BaseClass and the
ChildClass. When you call the BaseClass Add method with less than zero value as parameters,

then it adds successfully. But when you call the ChildClass Add method with less than zero
value, it checks for a negative value. If the passing values are negative, it asks for a new value.

So, here it is clear that we can modify the base class methods in derived classes.

Points to be remembered,

1. The method cannot be private.


2. Only abstract or virtual methods can be overridden.
3. Which method should be called is decided at run time.

Website- http://vtpit.co.in/ Contact- +91-8318572800 E-Mail-


vtpit2023@gmail.com

You might also like