SQLSummary
SQL
select: select columns
select distinct: remove redundant data in columns (reduce the number of rows)
limit: limit the number of results to be displayed
where: limit conditions
like: select string with certain pattern (work with wildcard)
in: select from several values
eg
1
where column in (value1, value2,...)
between: select value/string/data between two values
eg
1
where column between value1 and value2
alias: set alias (used in select, where)
order by: show result in certain order
insert into: insert a row with certain columns
eg
1
2insert into table_name (column1, column2, column3,...)
values (value1, value2, value3,...)You do not need to name the columns if the row inserted contains them all.
update: update certain rows
eg
1
2
3update table_name
set column1=value1, column2=value2,...
where <conditoins>You have to set conditions when using
update
.
delete: delete certains rows
eg
1
2delete from table_name
where <conditions>;You have to set conditions when using
delete
.
join: join two several tables
eg
1
2
3
4select <columns>
from table1_name
join table2_name
on table1_name.key=table2.name.key;
union: union several table (with same number of columns, same type in each column and same order of columns)
eg
1
2
3select <columns> from table1_name
union <all>
select <columns> from table2_name;Use
all
to allow redundant data in the result ofunion
.
insert into select: copy data from a table and insert to another
eg
1
2
3insert into table2_name <columns>
select <columns>
from table1_name
create table: create a new table
eg
1
2
3
4
5
6create table table_name
(
column1 data_type(size) <constraint_name>,
column2 data_type(size) <constraint_name>,
...
)constraints: add constraint to give more rules in table
Table
not null unique primary key foreign key check default no null in this column each value in this column is nuique set primary key (not null & unique) set foreign key each value compromise certain condition set default value primary key: every table should have one and only one primary key.
eg
1
2
3
4
5
6
7create table table_name
(
column1 data_type(size),
column2 data_type(size),
...
primary key (column1)
)
foreign key: foreign key is the primary key of another table
check: check certain condition
eg
1
2
3
4
5
6
7create table table_name
(
column1 data_type(size),
column2 data_type(size),
...
check <condition>
)or
1
2
3
4
5
6
7create table table_name
(
column1 data_type(size),
column2 data_type(size),
...
constraint constraint_name check <conditions>
)
index: create index
eg
1
2create <unique> index index_name
on table_name (column1, column2,...)
group by: group the results; it should work with aggregate function.
eg
1
2
3select column1, aggregate_function(column_name)
from table_name
group by column1
having: aggregate function can not work with
where
and that is why we needhaving
.- the order of these commands is
where
->group by
->having
.
- the order of these commands is
exist: return true is the result exists