Loading lessons...
CREATE TABLE
CREATE TABLE
The CREATE TABLE statement creates a new table and defines its columns and their data types.
Syntax
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
City varchar(255)
);
- Each column has a name and a data type.
intstores whole numbers.varchar(255)stores text up to 255 characters.
Create a table from another table
CREATE TABLE NewTable AS
SELECT * FROM OldTable;
Copies both the structure and the data.
Common data types
| Type | Stores |
|---|---|
| int | whole numbers |
| decimal(p,s) | exact decimal numbers |
| varchar(n) | variable-length text |
| char(n) | fixed-length text |
| date | date (YYYY-MM-DD) |
| datetime | date and time |
| boolean | true / false |
TL;DR
- CREATE TABLE defines columns and types.
- Each column needs a name and a data type.
- varchar(255) is the common text type.
- int stores whole numbers, date stores dates.