Notes
Chapter 3: Database Structure

1. Tables, Rows, and Columns

In Microsoft SQL Server, a database is organized into tables, which are used to store structured data. Let's break down the components:

Tables: Tables are the fundamental building blocks of a database. They represent entities such as customers, products, or orders. Each table consists of rows and columns.

Rows: Rows, also known as records, represent individual data entries within a table. For example, in a "Customers" table, each row might represent a unique customer with various attributes like name, address, and contact information.

Columns: Columns, also known as fields, define the attributes or properties of the data stored in a table. In the "Customers" table, columns might include "CustomerID," "FirstName," "LastName," and "Email."

Example :

creating a "Customers" table in SQL Server
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),

2. Primary Keys and Foreign Keys

Primary Keys:

● A Primary Key is a unique identifier for each row in a table.

● It ensures that each row has a unique value, which helps maintain data integrity.

● Commonly used for columns like "CustomerID" or "ProductID."

Example :

Adding a primary key constraint to an existing table
ALTER TABLE Customers
ADD PRIMARY KEY (CustomerID);
Foreign Keys:

● A Foreign Key establishes a link between two tables, creating relationships.

● It enforces referential integrity, ensuring that values in one table correspond to values in another.

● Often used to relate tables like "Orders" and "Customers" using the "CustomerID" column.

Example :

Adding a foreign key constraint to an existing table
ALTER TABLE Orders
ADD CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);

3. Indexes

Indexes are database structures used to improve the speed of data retrieval operations. They work similarly to book indexes, allowing the database engine to quickly locate the required data. Common types of indexes include:

Clustered Index: : Determines the physical order of data rows in a table. A table can have only one clustered index.

Example :

Creating a clustered index
CREATE CLUSTERED INDEX IX_ProductID
ON Products(ProductID); REFERENCES Customers(CustomerID);

Non-Clustered Index: Provides a separate structure for fast data retrieval. A table can have multiple non-clustered indexes.

Example :

Creating a non-clustered index
CREATE NONCLUSTERED INDEX IX_LastName
ON Customers(LastName);

Indexes significantly enhance query performance, especially for large datasets.

4. Schema

A Schema in SQL Server is a container for organizing database objects like tables, views, and procedures. It helps in managing database structures more effectively by providing a logical grouping.

Example :

Creating a schema
CREATE SCHEMA Sales;

Example :

Creating a table within a schema
CREATE TABLE Sales.Orders (
OrderID INT PRIMARY KEY,
CustomerID INT, OrderDate DATE );

5. Real-world Database Structures

E-commerce Database Structure:

In an e-commerce system using SQL Server, you might have tables like "Customers," "Products," "Orders," and "OrderDetails." The "Orders" table could have a foreign key referencing the "Customers" table, and "OrderDetails" could relate to both "Orders" and "Products."

Human Resources Database Structure:

In a human resources system, you could have tables such as "Employees," "Departments," and "Salaries." The "Employees" table might have a foreign key linking to the "Departments" table, and "Salaries" could be associated with "Employees."

6. Key Takeaways

● SQL Server databases consist of tables, rows, and columns to organize and store data.

● Primary keys ensure uniqueness within a table, while foreign keys establish relationships between tables.

● Indexes are crucial for improving data retrieval performance.

● Schemas provide a logical way to group and manage database objects.

7. Exercise Questions

● Create a "Products" table with columns for "ProductID," "ProductName," and "Price." Set "ProductID" as the primary key.

● Design a database structure for a university, including tables for "Students," "Courses," and "Enrollments." Define appropriate primary and foreign keys.

● Create a non-clustered index on the "LastName" column of the "Employees" table. Explain the difference between a primary key and a foreign key with examples.

● Suppose you have a "Books" table and a "Authors" table. Create a foreign key relationship between them, assuming each book has one author.

● How can indexes improve the performance of SQL queries? Provide a real-world scenario. Create a schema called "HR" and move the "Employees" and "Departments" tables into this schema.

● Discuss when it is beneficial to use schemas in database design.

Understanding Databases

A database is a structured collection of data organized for efficient storage, retrieval, and management. In the context of Microsoft SQL Server, a database is a container that holds tables, views, indexes, and other objects for organizing and accessing data. Databases are at the heart of data management in SQL Server.

Read More
Data Modeling and Normalization

Data modeling is the process of defining the structure and relationships of data within a database. It helps ensure that data is organized efficiently and accurately, allowing for easy retrieval and manipulation. Data modeling typically involves creating entity-relationship diagrams (ERDs) and defining tables, columns, and relationships.

Read More