Notes
Notes
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
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.
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.
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 >.
1. INNER JOIN
2. SELF JOIN
3. CROSS JOIN
4. OUTER JOIN
The following visual representation explains how INNER JOIN returns the matching
records from table1 and table2:
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
ADVERTISEMENT
ADVERTISEMENT
Let us first create two tables "Student" and "Fee" using the following statement:
Next, we will insert some records into these tables using the below statements:
Table: Student
Table: Fee
We can demonstrate the INNER JOIN using the following command:
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.
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.
Example
ADVERTISEMENT
ADVERTISEMENT
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
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.
ADVERTISEMENT
ADVERTISEMENT
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:
ADVERTISEMENT
ADVERTISEMENT
This output shows that the unmatched row's values are replaced with NULLs in the
respective columns.
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:
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.