Database & Tables Introduction
A database is used to store and organize data, while tables are used to store data in rows and columns. In SQL Server, databases and tables form the foundation for storing and managing structured information.
What is a Database?
A database is a structured collection of information that allows data to be stored, managed and retrieved efficiently.
In SQL Server, a database can contain tables, views, stored procedures, functions and other database objects.
Example
A college application may have a database named CollegeDB that stores student, course and teacher information.
Database Structure
A SQL Server database can contain multiple database objects. A simple structure can be represented as follows:
Database
↓
Tables
↓
Rows & Columns
↓
Data
Tables are one of the most important objects inside a database because they store the actual data.
What is a Table?
A table is a database object used to store related information in the form of rows and columns.
Each column represents a specific type of information, while each row represents one complete record.
Rows and Columns
Consider a Students table:
| StudentId | StudentName | Age | Course |
|---|---|---|---|
| 1 | Rahul | 22 | Java |
| 2 | Priya | 21 | .NET |
| 3 | Amit | 23 | Python |
- Column: Defines the type of information stored.
- Row: Represents one complete record.
- Table: Collection of related records.
Database and Table Relationship
A database can contain multiple tables. Each table can store data related to a particular entity.
CollegeDB
│
├── Students
│
├── Courses
│
├── Teachers
│
└── Departments
For example, the Students table stores student information, while the Courses table stores course information.
Creating a Database
The CREATE DATABASE statement is used to create a new database in SQL Server.
CREATE DATABASE CollegeDB;
After executing this statement, SQL Server creates a new database named CollegeDB.
Creating a Table
The CREATE TABLE statement is used to create a table inside a database.
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Age INT,
Course VARCHAR(50)
);
This statement creates a table named Students with four columns.
Common Database & Table Operations
| Operation | SQL Statement | Purpose |
|---|---|---|
| Create Database | CREATE DATABASE | Create a new database. |
| Create Table | CREATE TABLE | Create a new table. |
| Alter Table | ALTER TABLE | Modify table structure. |
| Drop Table | DROP TABLE | Remove a table. |
Important
A database contains database objects, and tables are primarily used to store structured data in rows and columns. Choosing a proper table structure is important for managing data efficiently.
Summary
In this section, we learned what a database and table are, how databases and tables are organized, the difference between rows and columns, and the basic SQL statements used to create databases and tables.