To create a new database, you can use the CREATE DATABASE statement.
CREATE DATABASE database_name;
Example:
CREATE DATABASE CompanyDB;
Before creating tables or performing any operations on a database, you need to select the database you want to work with. This is done using the USE statement.
USE database_name;
Example:
USE CompanyDB;
To create a new table in a database, use the CREATE TABLE statement. You must define the columns, their data types, and any constraints (like PRIMARY KEY, NOT NULL, etc.).
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
column3 datatype constraints,
...
);
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
HireDate DATE,
DepartmentID INT
);
In this example: