Creating databases is one of the foundational tasks in working with SQL. Databases store and organize data in a structured format, and understanding how to create and manage them is crucial for database management and development. In this chapter, we will cover the basics of creating databases, along with advanced concepts, examples, and best practices.
A database is a structured collection of data that can be easily accessed, managed, and updated. SQL (Structured Query Language) is the most widely used language to manage databases. When you work with SQL, you interact with databases to store, retrieve, and manipulate data.
Databases consist of tables, and each table holds data in rows and columns. The process of creating a database involves defining its structure, parameters, and organization.
A database is made up of several components, including:
The first step in working with a SQL database is creating one. The CREATE DATABASE
statement is used to create a new database.
CREATE DATABASE database_name;
database_name
: The name of the database you want to create. It should be unique within the SQL instance.
CREATE DATABASE EmployeeDB;
In this example, we are creating a new database called EmployeeDB
. Once created, you can begin adding tables, data, and other elements to this database.
When creating a database, you can specify additional options to control its behavior, such as the location of the database files and size parameters.
CREATE DATABASE EmployeeDB
ON PRIMARY
(NAME = EmployeeData,
FILENAME = 'C:\SQLData\EmployeeDB.mdf',
SIZE = 10MB,
MAXSIZE = 100MB,
FILEGROWTH = 5MB);
ON PRIMARY
: Specifies the primary filegroup for the database.NAME
: Defines the logical name of the database file.FILENAME
: Indicates the physical file location.SIZE
: Initial size of the database file.MAXSIZE
: Maximum size the database file can grow to.FILEGROWTH
: How much the file should grow when it runs out of space.A schema is a collection of database objects (tables, views, stored procedures, etc.) associated with a specific user or role. It is a way to logically group objects in a database.
CREATE SCHEMA HumanResources;
In this example, we are creating a schema named HumanResources
. Schemas help organize database objects and manage permissions.
After creating a database, the next step is to create tables within it. Tables hold the actual data in rows and columns.
USE EmployeeDB;
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
USE EmployeeDB
: Selects the EmployeeDB
database to work within.CREATE TABLE Employees
: Creates a table named Employees
.EmployeeID
, FirstName
, LastName
, Department
, and Salary
.The Employees
table is successfully created within the EmployeeDB
database.
Constraints are rules applied to columns in a table to ensure data integrity. Common constraints include:
NULL
values.
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50) NOT NULL,
Budget DECIMAL(10, 2) CHECK (Budget > 0)
);
DepartmentID
is the primary key.DepartmentName
cannot be NULL
.Budget
must be greater than 0 due to the CHECK
constraint.You can view a list of all databases on the server using the following query:
SELECT name
FROM sys.databases;
+-----------------+
| name |
+-----------------+
| master |
| tempdb |
| model |
| msdb |
| EmployeeDB |
+-----------------+
To rename a database, use the ALTER DATABASE
command:
ALTER DATABASE EmployeeDB MODIFY NAME = NewEmployeeDB;
You can delete (drop) a database with the DROP DATABASE
command:
DROP DATABASE NewEmployeeDB;
Be cautious when dropping a database, as it permanently deletes all the data and objects within it.
Backing up databases is crucial for preventing data loss in case of system failures or corruption. You can create a backup and restore it as needed.
BACKUP DATABASE EmployeeDB TO DISK = 'C:\SQLBackup\EmployeeDB.bak';
RESTORE DATABASE EmployeeDB FROM DISK = 'C:\SQLBackup\EmployeeDB.bak';
BACKUP
command saves a copy of the database to a file.RESTORE
command restores the database from the backup file.Creating databases in SQL is an essential skill for working with data. From basic creation to advanced configurations, this chapter has covered everything you need to know to create and manage databases effectively. We've explored how to define schemas, create tables, apply constraints, and manage database backups. Happy coding !❤️