Learning SQL
Using SQL
Structured Query Language (SQL) is a declarative programming language used for accessing data storied in relational databases. It appears in or is a core part of many common software packages like MySQL, PostgreSQL, Microsoft SQL Server, and Oracle Database.
While the standard actions of SELECT, INSERT, UPDATE, and DELETE help with interacting with existing data, there are also ways to add, change, or remove tables themselves. These are CREATE TABLE, ALTER TABLE, and DROP TABLE.
CREATE TABLE
To add a new table to any existing ones, use the keywords CREATE TABLE.
Table Name: People | |
ID | Name |
1 | Dan |
2 | Chad |
Using the CREATE TABLE keywords require the name of the table.
CREATE TABLE Companies
Like with working with the keyword INSERT, CREATE TABLE needs to know the columns to add. These are included within parentheses and separated by commas.
CREATE TABLE Companies (
ID INT,
Name TEXT
)
Table Name: Companies | |
ID | Name |
Datatypes
Each different implementation of SQL has their own types of data and requirements when creating new tables. Most support the type INT (integers) and TEXT (letters and numbers) or some equivalent. These are required when creating tables to let SQL know what type of data will be stored within those columns.
Many SQL implementations also require some notification if data within the column can be empty or not. Often the keywords NULL (can be empty) or NOT NULL (cannot be empty) will also be used after the data type of the column but before the comma between column names.
CREATE TABLE Companies (
ID INT NOT NULL,
Name TEXT NOT NULL
)