Lesson 28 +10 XP

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.
  • int stores 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

TypeStores
intwhole numbers
decimal(p,s)exact decimal numbers
varchar(n)variable-length text
char(n)fixed-length text
datedate (YYYY-MM-DD)
datetimedate and time
booleantrue / 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.