扫码一下
查看教程更方便
sql 是由一系列特定的规则和标识符组成的,这些被称为语法。本教程通过列出所有基本 sql 语法,让你可以快速开始使用 sql。
所有 sql 语句都以任何特定的关键字开头,例如 select、insert、update、delete、alter、drop、create、use、show,并且所有语句都以分号 (;) 结尾。
需要注意的最重要的一点是 sql 不区分大小写,这意味着 select 和 select 在 sql 语句中具有相同的含义。而 mysql 在表名上有所不同。因此,如果你是在使用 mysql,那么需要给出数据库中存在的表名。
本教程中给出的所有示例都已在 mysql 服务器上进行了测试。
select column1, column2....columnn
from table_name;
select distinct column1, column2....columnn
from table_name;
select column1, column2....columnn
from table_name
where condition;
select column1, column2....columnn
from table_name
where condition-1 {and|or} condition-2;
select column1, column2....columnn
from table_name
where column_name in (val-1, val-2,...val-n);
select column1, column2....columnn
from table_name
where column_name between val-1 and val-2;
select column1, column2....columnn
from table_name
where column_name like { pattern };
select column1, column2....columnn
from table_name
where condition
order by column_name {asc|desc};
select sum(column_name)
from table_name
where condition
group by column_name;
select count(column_name)
from table_name
where condition;
select sum(column_name)
from table_name
where condition
group by column_name
having (arithematic function condition);
create table table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnn datatype,
primary key( one or more columns )
);
drop table table_name;
sql create index 语句
create unique index index_name
on table_name ( column1, column2,...columnn);
alter table table_name
drop index index_name;
desc table_name;
truncate table table_name;
alter table table_name {add|drop|modify} column_name {data_ype};
alter table table_name rename to new_table_name;
insert into table_name( column1, column2....columnn)
values ( value1, value2....valuen);
update table_name
set column1 = value1, column2 = value2....columnn=valuen
[ where condition ];
delete from table_name
where {condition};
create database database_name;
drop database database_name;
use database_name;
commit;
rollback;