Databases are operating in pretty much every private sector and public sector organization on the planet. Database schemas serve as a foundation for enterprises that are highly data-driven. A simple example is shown visually in the image below. This is an example of a database at a university where the schema would represent the structure of the university database, outlining tables for Students, Teachers, Courses, Enrollments, and Grades, while establishing relationships between these entities to manage information related to the university’s academic activities.
Each table has a Primary Key (PK) field which uniquely identifies the information in a row of the database. In addition, some tables have Foreign Keys (FK) fields that establish a relationship between tables via linking to the Primary Key of the related table.
You can see a couple of the relationships and links between the different tables in the image below. For example, there is an FK relationship that links the Enrollments table to the Students table showing which student is enrolled in a specific course. Another FK relationship connects the Courses table to the Teachers table where the TeacherId column in the Courses table references the unique TeacherId column in the Teachers table. This will ensure which teacher is responsible for teaching a particular course.
You can see we have data for Teachers, Students, Courses with their respective Ids in their own tables. Creating such database schemas helps simplify maintenance, avoids data redundancy and ensures data consistency as we don't have to update the same information multiple times in different places, which reduces the inconsistencies and errors in the database.
As data is changed, for instance, when a teacher moves to a new address or a student upgrades his subjects, modifications are made only in their respective master tables as the information is managed centrally in these tables
Database schemas provide a framework how data is stored, accessed and manipulated within a Database Management System (DBMS). They are used by a wide range of database professionals including database users, database administrators, and database analysts to manage databases effectively and help to derive useful insights about the data.
Database schemas enable six primary benefits in database management systems:
Creating a database schema involves defining and designing the structure, tables, columns, relationships, and constraints within a database. Always remember the exact syntax and methods for creating a database schema might differ based on the database management system you are using.
Here's a general guide on how to create a database schema:
CREATE TABLE Products (
ProductId INT PRIMARY KEY,
ProductName VARCHAR(50) UNIQUE,
StockedUpDate DATE
);
Example (adding a foreign key in the "SubProducts" table referring to the "Products" table).
A database schema is the logical representation of an entire database that defines how data is organized, stored, and accessed within that database. It outlines the logical and structural design of a database, defining the tables, their attributes (columns), data types, constraints, relationships and keys.
The schema establishes relationships between tables, specifying how different tables are related to each other through keys, such as primary keys and foreign keys. These relationships maintain data integrity and ensure that data is consistent across related tables.
The schema also includes constraints that enforce rules and restrictions on data. These constraints can include primary key constraints, unique constraints, foreign key constraints, check constraints, etc. They help in maintaining data accuracy and consistency.
Primary keys ensure uniqueness, foreign keys establish relationships, unique constraints prevent duplicate entries, not null constraints ensure mandatory values, etc.
The schema structure guides users in performing operations like querying, inserting, updating and deleting data.
In a DBMS, a schema encompasses the conceptual, logical and physical structure of the database, providing a comprehensive framework for organizing and managing data effectively. It provides a system that defines how data is organized, how tables are structured, the relationships between tables, and the constraints applied to maintain data integrity.
Conceptual schema:
At this level, it defines the entities, their attributes, and the relationships between them without specifying how these will
be stored physically.
Logical schema:
The logical schema defines the logical structure of the entire database, including tables, views, relationships, constraints, and security rules without focusing on the physical storage details.
Physical schema:
The physical schema defines how data is stored physically on storage devices such as hard drives, solid-state drives, or memory. It includes details about data blocks, indexes, tablespaces, filegroups, etc. This level of schema is more concerned with the physical storage and performance aspects of the database.
For example, in a simple Relational Database Management System (RDBMS), the physical schema would define details such as how tables are stored on disk, the file structure, indexing methods, etc. The logical schema would define the structure of tables, their columns, data types, primary and foreign keys, relationships between tables, and other constraints that define the database's logical framework.
In SQL Server, you can find information about the schemas in a database by querying system views or system tables provided by the SQL Server Management Studio (SSMS). Below are some ways to find information about schemas:
The sys.objects view and sys.schemas view from the sys schema provide information about database objects and schemas respectively.
SELECT s.name AS SchemaName
FROM sys.objects o
JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE o.name = 'TableName' AND o.type = 'U'
Here, you need to replace 'TableName' with the table you are interested in and 'U' stands for User-defined Table
SQL Server also supports information schema views that provide information about database objects, including schemas. The INFORMATION_SCHEMA.SCHEMATA view contains schema information.
The INFORMATION_SCHEMA views provide metadata information about the database objects. To find the schema of a table:
SELECT TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TableName';
Replace 'TableName' with the name of the table you want to know the schema for.
You can explore the Object Explorer using SSMS.
Connect to your SQL Server instance in SSMS.
Expand the Databases folder.
Expand the specific database you are interested in.
Expand the Security folder, and then the Schemas folder.
This will display a list of schemas available in the database.
You can also retrieve more specific details about schemas, such as the tables or objects associated with a particular schema, using queries that join different system views. For instance, if you want to find all tables within a specific schema named 'dbo':
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_TYPE = 'BASE TABLE';
Replace 'dbo' with the name of the schema you want to query.
Using these queries, you can explore and retrieve information about schemas within a SQL Server database, view the objects associated with each schema, and gain insights into the database's structure.