0% completed
CREATE
In SQL, the CREATE
statement is used to create various database objects, i.e., tables, indexes, or the database itself.
Here's a detailed explanation of the CREATE
statement and the objects you can create.
-
CREATE Database
We can create a new database with a CREATE statement. Let's see how.
CREATE DATABASE database_name;
This simple command will create a database. You can specify any name for your database.
-
CREATE Table
A table is a fundamental database object that stores and organizes data in a structured format. After creating a database, you first need to create a table so that you can act on it.
Here's how you can create a table in the database
CREATE TABLE Student ( StudentID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), RollNumber INT, );
You might be wondering what INT or VARCHAR means. Let's first go deeply into these fundamentals to ensure you have a firm grasp.
Data Types in SQL
SQL offers several data types to facilitate the specification of the kind of data to be stored in the table columns. For example, The age must be entered as a number, and the name must be entered as a string.
MySQL has three main data types: string, numeric, and date and time.
Data Type | Description | Example | Use Case |
---|---|---|---|
Numeric Types | |||
INT | Integer data type | Quantity INT | Representing counts, identifiers, or indices |
BIGINT | Larger integer type | Population BIGINT | Handling large numbers, like population counts |
SMALLINT | Smaller integer type | Rank SMALLINT | Limited range integer values, e.g., rank |
DECIMAL(p, s) | Decimal numbers with precision and scale | Price DECIMAL(10, 2) | Monetary values with exact precision |
Character String Types | |||
VARCHAR(n) | Variable-length character strings | Name VARCHAR(50) | Storing variable-length text |
CHAR(n) | Fixed-length character strings | Code CHAR(10) | Fixed-length codes or identifiers |
TEXT | Variable-length character strings for large text data | Description TEXT | Storing large text or document content |
Date and Time Types | |||
DATE | Date without a time component | BirthDate DATE | Birthdates, event dates |
TIME | Time without a date component | EventTime TIME | Recording time of events |
DATETIME or TIMESTAMP | Date and time together | CreatedAt TIMESTAMP | Timestamps for record creation or updates |
YEAR | Year in a four-digit format | BirthYear YEAR | Storing years, e.g., year of birth |
The (n)
notation in the CHAR and VARCHAR types represents the maximum length of the string.
So, as we go through the concepts in detail, now you know that VARCHAR or INT is just the type of data that needs to be stored in a specific column in the student table.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible