0% found this document useful (0 votes)
9 views

Notes

Uploaded by

TRUMIL NASIT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Notes

Uploaded by

TRUMIL NASIT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data model Schema and Instance

o The data which is stored in the database at a particular moment of time is called an
instance of the database.
o The overall design of a database is called schema.
o A database schema is the skeleton structure of the database. It represents the logical
view of the entire database.
o A schema contains schema objects like table, foreign key, primary key, views,
columns, data types, stored procedure, etc.
o A database schema can be represented by using the visual diagram. That diagram
shows the database objects and relationship with each other.
o A database schema is designed by the database designers to help programmers whose
software will interact with the database. The process of database creation is called
data modeling.

A schema diagram can display only some aspects of a schema like the name of record type,
data type, and constraints. Other aspects can't be specified through the schema diagram. For
example, the given figure neither show the data type of each data item nor the relationship
among various files.

In the database, actual data changes quite frequently. For example, in the given figure, the
database changes whenever we add a new grade or add a student. The data at a particular
moment of time is called the instance of the database.
Data Independence

o Data independence can be explained using the three-schema architecture.


o Data independence refers characteristic of being able to modify the schema at one
level of the database system without altering the schema at the next higher level.

There are two types of data independence:

1. Logical Data Independence


o Logical data independence refers characteristic of being able to change the conceptual
schema without having to change the external schema.
o Logical data independence is used to separate the external level from the conceptual
view.
o If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.
o Logical data independence occurs at the user interface level.
2. Physical Data Independence
o Physical data independence can be defined as the capacity to change the internal
schema without having to change the conceptual schema.
o If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.
o Physical data independence is used to separate conceptual levels from the internal
levels.
o Physical data independence occurs at the logical interface level.

DBMS Architecture 1-level, 2-Level, 3-Level



A Database stores a lot of critical information to access data quickly and securely. Hence it is
important to select the correct architecture for efficient data management. DBMS
Architecture helps users to get their requests done while connecting to the database. We
choose database architecture depending on several factors like the size of the database,
number of users, and relationships between the users. There are two types of database models
that we generally use, logical model and physical model. Several types of architecture are
there in the database which we will deal with in the next section.
Types of DBMS Architecture
There are several types of DBMS Architecture that we use according to the usage
requirements. Types of DBMS Architecture are discussed here.
 1-Tier Architecture
 2-Tier Architecture
 3-Tier Architecture
1-Tier Architecture
In 1-Tier Architecture the database is directly available to the user, the user can directly sit on
the DBMS and use it that is, the client, server, and Database are all present on the same
machine. For Example: to learn SQL we set up an SQL server and the database on the local
system. This enables us to directly interact with the relational database and execute
operations. The industry won’t use this architecture they logically go for 2-tier and 3-tier
Architecture.

DBMS 1-Tier Architecture

Advantages of 1-Tier Architecture


Below mentioned are the advantages of 1-Tier Architecture.
 Simple Architecture: 1-Tier Architecture is the most simple architecture to set up, as
only a single machine is required to maintain it.
 Cost-Effective: No additional hardware is required for implementing 1-Tier Architecture,
which makes it cost-effective.
 Easy to Implement: 1-Tier Architecture can be easily deployed, and hence it is mostly
used in small projects.
2-Tier Architecture
The 2-tier architecture is similar to a basic client-server model. The application at the client
end directly communicates with the database on the server side. APIs like ODBC and JDBC
are used for this interaction. The server side is responsible for providing query processing and
transaction management functionalities. On the client side, the user interfaces and application
programs are run. The application on the client side establishes a connection with the server
side to communicate with the DBMS.
An advantage of this type is that maintenance and understanding are easier, and compatible
with existing systems. However, this model gives poor performance when there are a large
number of users.

DBMS 2-Tier Architecture

Advantages of 2-Tier Architecture


 Easy to Access: 2-Tier Architecture makes easy access to the database, which makes fast
retrieval.
 Scalable: We can scale the database easily, by adding clients or upgrading hardware.
 Low Cost: 2-Tier Architecture is cheaper than 3-Tier Architecture and Multi-Tier
Architecture.
 Easy Deployment: 2-Tier Architecture is easier to deploy than 3-Tier Architecture.
 Simple: 2-Tier Architecture is easily understandable as well as simple because of only
two components.
3-Tier Architecture
In 3-Tier Architecture, there is another layer between the client and the server. The client
does not directly communicate with the server. Instead, it interacts with an application server
which further communicates with the database system and then the query processing and
transaction management takes place. This intermediate layer acts as a medium for the
exchange of partially processed data between the server and the client. This type of
architecture is used in the case of large web applications.
DBMS 3-Tier Architecture

Advantages of 3-Tier Architecture


 Enhanced scalability: Scalability is enhanced due to the distributed deployment of
application servers. Now, individual connections need not be made between the client and
server.
 Data Integrity: 3-Tier Architecture maintains Data Integrity. Since there is a middle
layer between the client and the server, data corruption can be avoided/removed.
 Security: 3-Tier Architecture Improves Security. This type of model prevents direct
interaction of the client with the server thereby reducing access to unauthorized data.
Disadvantages of 3-Tier Architecture
 More Complex: 3-Tier Architecture is more complex in comparison to 2-Tier
Architecture. Communication Points are also doubled in 3-Tier Architecture.
 Difficult to Interact: It becomes difficult for this sort of interaction to take place due to
the presence of middle layers.
For more, you can refer to the Advantages and Disadvantages of 3-Tier Architecture in
DBMS.
Conclusion
When it comes to choosing a DBMS architecture, it all comes down to how complex and
scalable the system is. The 3-level structure has the best features and is perfect for modern,
big database systems.
__

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:

ADVERTISEMENT

ADVERTISEMENT

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:

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

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

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

1. SELECT columns
2. FROM table1
3. INNER JOIN table2 ON condition1
4. INNER JOIN table3 ON condition2

INNER JOIN Example

ADVERTISEMENT

ADVERTISEMENT

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

1. CREATE TABLE Student (


2. id int PRIMARY KEY IDENTITY,
3. admission_no varchar(45) NOT NULL,
4. first_name varchar(45) NOT NULL,
5. last_name varchar(45) NOT NULL,
6. age int,
7. city varchar(25) NOT NULL
8. );
9.
10. CREATE TABLE Fee (
11. admission_no varchar(45) NOT NULL,
12. course varchar(45) NOT NULL,
13. amount_paid int,
14. );

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

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


2. VALUES (3354,'Luisa', 'Evans', 13, 'Texas'),
3. (2135, 'Paul', 'Ward', 15, 'Alaska'),
4. (4321, 'Peter', 'Bennett', 14, 'California'),
5. (4213,'Carlos', 'Patterson', 17, 'New York'),
6. (5112, 'Rose', 'Huges', 16, 'Florida'),
7. (6113, 'Marielia', 'Simmons', 15, 'Arizona'),
8. (7555,'Antonio', 'Butler', 14, 'New York'),
9. (8345, 'Diego', 'Cox', 13, 'California');
10.
11.
12. INSERT INTO Fee (admission_no, course, amount_paid)
13. VALUES (3354,'Java', 20000),
14. (7555, 'Android', 22000),
15. (4321, 'Python', 18000),
16. (8345,'SQL', 15000),
17. (5112, 'Machine Learning', 30000);

Execute the SELECT statement to verify the records:

Table: Student

Table: Fee
We can demonstrate the INNER JOIN using the following command:

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


e.amount_paid
2. FROM Student
3. INNER JOIN Fee
4. ON Student.admission_no = Fee.admission_no;

This command gives the below result:

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

In this example, we have used the admission_no column as a join condition to get
the data from both tables. Depending on this table, we can see the information of
the students who have paid their fee.

SELF JOIN
A table is joined to itself using the SELF JOIN. It means that each table row is
combined with itself and with every other table row. The SELF JOIN can be thought
of as a JOIN of two copies of the same tables. We can do this with the help of table
name aliases to assign a specific name to each table's instance. The table aliases
enable us to use the table's temporary name that we are going to use in the query.
It's a useful way to extract hierarchical data and comparing rows inside a single table.

SELF JOIN Syntax

ADVERTISEMENT
ADVERTISEMENT

The following expression illustrates the syntax of SELF JOIN in SQL Server. It works
the same as the syntax of joining two different tables. Here, we use aliases names for
tables because both the table name are the same.

1. SELECT T1.col_name, T2.col_name...


2. FROM table1 T1, table1 T2
3. WHERE join_condition;

Example

ADVERTISEMENT

ADVERTISEMENT

We can demonstrate the SELF JOIN using the following command:

1. SELECT S1.first_name, S2.last_name, S2.city


2. FROM Student S1, Student S2
3. WHERE S1.id <> S2.iD AND S1.city = S2.city
4. ORDER BY S2.city;

This command gives the below result:

In this example, we have used the id and city column as a join condition to get the
data from both tables.

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

ADVERTISEMENT

ADVERTISEMENT

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

1. SELECT column_lists
2. FROM table1
3. CROSS JOIN table2;

Example

We can demonstrate the CROSS JOIN using the following command:

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


e.amount_paid
2. FROM Student
3. CROSS JOIN Fee
4. WHERE Student.admission_no = Fee.admission_no;

This command gives the below result:

ADVERTISEMENT
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.

ADVERTISEMENT

ADVERTISEMENT

The below visual representation illustrates the LEFT OUTER JOIN:

LEFT OUTER JOIN Syntax

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

1. SELECT column_lists
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON table1.column = table2.column;

Example
We can demonstrate the LEFT OUTER JOIN using the following command:

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


e.amount_paid
2. FROM Student
3. LEFT OUTER JOIN Fee
4. ON Student.admission_no = Fee.admission_no;

This command gives the below result:

ADVERTISEMENT

ADVERTISEMENT

This output shows that the unmatched row's values are replaced with NULLs in the
respective columns.

RIGHT OUTER JOIN


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

The below visual representation illustrates the RIGHT OUTER JOIN:

RIGHT OUTER JOIN Syntax


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

1. SELECT column_lists
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON table1.column = table2.column;

Example

The following example explains how to use the RIGHT OUTER JOIN to get records
from both tables:

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


e.amount_paid
2. FROM Student
3. RIGHT OUTER JOIN Fee
4. ON Student.admission_no = Fee.admission_no;

This command gives the below result:

In this output, we can see that no column has NULL values because all rows in the
Fee table are available in the Student table based on the specified condition.

You might also like