sql - Serbipunk/notes GitHub Wiki
primary key
ALTER TABLE website ADD PRIMARY KEY (id);
one or multiple (composite primary key) cols in table, must be unique and cannot be null
foreign key
linked to another table's primary key.
create table with foreign key:
CREATE TABLE `order`(
oid INT NOT NULL AUTO_INCREMENT,
uid INT references user(id),
PRIMARY KEY (oid)
);
delete foreign keys
ALTER TABLE `order`
DROP FOREIGN KEY;
sql select constraint
http://c.biancheng.net/sql/constraint.html
order by
SELECT column_list FROM table_name [WHERE condition] [ORDER BY col1, col2, ...] [ASC | DESC]
group by
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
if column name with the same name of GROUP BY item, the next value will be merged into a new result.
SELECT country, TRUNCATE(SUM(uv), 2) AS total FROM website
GROUP BY country;
distinct
duplicated select condition logs will be merged into one log
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
http://c.biancheng.net/sql/distinct.html
sqlite
insert
insert into books_book (title, subtitle, author, isbn) values ("search for morden china 2", "1500 to 1990 history", "Jonathan Spence", "1234567");