SQL Basics
most of it will be based on the sqlite database.
general:
- query language used to interact with relation dbs
- it's not case sensitive.
"SELECT" Statement - retrieve data
- select all fields - retrieve all the columns and rows from the
userstable.
SELECT * FROM users;
- select single field - retrieve the
namecolumn from theuserstable
SELECT name FROM users;
- multiple fields - retrieve the
id&namecolumns from theuserstable
SELECT id, name FROM users;
- select distinct fields - retrieves only distinct values (non-duplicates) of
cityfrom theuserstable
SELECT DISTINCT city FROM users;
"CREATE TABLE" Statement - creating a table
- create a new table
userswithidas number,nameas text andcityas text as fields.
CREATE TABLE users(
id INTEGER,
name TEXT,
city TEXT
);
"ALTER TABLE" - altering the table i.e, making changes to a table without deleting any data
- rename the
peopletable tousers
ALTER TABLE people RENAME TO users;
- rename the
handlecolumn tousernameinuserstable
ALTER TABLE users RENAME COLUMN handle TO username;
- add a
textcolumn namedpasswordinuserstable
ALTER TABLE users ADD COLUMN password TEXT;
- drop the
citycolumn fromuserstable
ALTER TABLE users DROP COLUMN city;
NULL
In SQL, a cell with a NULL value indicates that the value is missing.
Constraints
It enforces some specific behaviour. eg:
PRIMARY KEYUNIQUENOT NULL