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
users
table.
SELECT * FROM users;
- select single field - retrieve the
name
column from theusers
table
SELECT name FROM users;
- multiple fields - retrieve the
id
&name
columns from theusers
table
SELECT id, name FROM users;
- select distinct fields - retrieves only distinct values (non-duplicates) of
city
from theusers
table
SELECT DISTINCT city FROM users;
"CREATE TABLE" Statement - creating a table
- create a new table
users
withid
as number,name
as text andcity
as 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
people
table tousers
ALTER TABLE people RENAME TO users;
- rename the
handle
column tousername
inusers
table
ALTER TABLE users RENAME COLUMN handle TO username;
- add a
text
column namedpassword
inusers
table
ALTER TABLE users ADD COLUMN password TEXT;
- drop the
city
column fromusers
table
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 KEY
UNIQUE
NOT NULL
If you found this article helpful, I would be grateful for your support.
"Buy me some paneer curry"