Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
CREATE
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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.

  1. 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.

  1. 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 TypeDescriptionExampleUse Case
Numeric Types
INTInteger data typeQuantity INTRepresenting counts, identifiers, or indices
BIGINTLarger integer typePopulation BIGINTHandling large numbers, like population counts
SMALLINTSmaller integer typeRank SMALLINTLimited range integer values, e.g., rank
DECIMAL(p, s)Decimal numbers with precision and scalePrice DECIMAL(10, 2)Monetary values with exact precision
Character String Types
VARCHAR(n)Variable-length character stringsName VARCHAR(50)Storing variable-length text
CHAR(n)Fixed-length character stringsCode CHAR(10)Fixed-length codes or identifiers
TEXTVariable-length character strings for large text dataDescription TEXTStoring large text or document content
Date and Time Types
DATEDate without a time componentBirthDate DATEBirthdates, event dates
TIMETime without a date componentEventTime TIMERecording time of events
DATETIME or TIMESTAMPDate and time togetherCreatedAt TIMESTAMPTimestamps for record creation or updates
YEARYear in a four-digit formatBirthYear YEARStoring 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.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible