-
SQL statement end with ';'
-
Keywords are case-insensitive,
select
andSELECT
are the sameBut it's better using captical case in general
-
String
orDate
should use single quote like 'abc' to conclude, exceptNumber
should write directly.
CREATE DATABASE <name of your database>;
CREATE DATABASE shop;
CREATE TABLE <table name>
(
<column name 1> <data type> <constraint>,
<column name 2> <data type> <constraint>,
<column name 3> <data type> <constraint>,
)
CREATE TABLE Product
(
product_id CHAR(4) NOT NULL,
product_name VARCHAR(100) NOT NULL,
product_type VARCHAR(32) NOT NULL,
sale_price INTEGER ,
purchase_price INTEGER ,
regist_date DATE ,
PRIMARY KEY (product_id)
);
Start with letters
, number
, _
only.
-
product_id ✅
-
product-id ❌
-
00product-id ❌
-
INTEGER
can't save small digits number. -
CHAR
static length:CHAR(8)
and when inputabc
it wll save asabc-----
(with 5 spaces) -
VARCHAR
flexible length and will saveabc
without spacesVARCHAR2
type is avaliable inOracle
but not recommend -
DATE
-
NOT NULL
-
PRIMARY KEY
-
DROP TABLE <table name>;
-
ALTER TABLE <table name> ADD COLUMN <column>
-
or add multipal column
ALTER TABLE <table name> ADD COLUMN (<column1>,<column2>,<column3>)
-
alter and delete column:
ALTER TABLE <table name> DROP COLUMN <column name>
-
insert data:
START TRANSACTION;
INSERT INTO tableName VALUES ();
...
COMMIT;
SELECT DISTINCT column_name from table_name;
Begin only in first column_name
SELECT column_name, ... FROM table_name WHERE condition;
Single line:
-- single line
Multiple line:
/*
Multi line
*/
-
+
-
-
-
*
-
/
any operation with NULL will equal to NULL
-
=
-
<>
unequal -
>=
-
>
-
<=
-
<
Note it that when compare between CHAR
:
- 1 < 10 < 11 < 2 < 222 < 3
-
SELECT xxx FROM xxx WHERE xxx = NULL;
❌ -
SELECT xxx FROM xxx WHERE xxx IS NULL;
✅ -
SELECT xxx FROM xxx WHERE xxx IS NOT NULL;
✅
NOT
SELECT xxx FROM xxx WHERE NOT sale_price >= 1000;
-- equal to
SELECT xxx FROM xxx WHERE sale_price < 1000;
AND
andOR
, similar to||
and&&
AND
will run prior toOR
, use()
to giving priority.
TRUE
andFALSE