数据库语言对大小写不敏感,用分号分隔每条数据库语言
SQL开发,在创建SQL表时要确定每个列要存储的数据类型,每一列都需要一个列名和确定的数据类型
常见数据类型自行查询
一些重要的SQL命令
select column_name,column_name from table_name;
select * from table_name;
选取结果被储存在一个结果表中,称为结果集
select distinct column_name,column_name from table_name;
select column_name,column_name
from table_name
where column_name operator value;
在where子句中可以使用=,<>,<,>,>=,<=,between,like#搜索某种模式,in#指定针对某个列的多个可能值
select * from custormers
where country = 'Germany'
and (city = 'Berlin' or City = 'Beijing')
select column_name,column_name
from table_name
order by column_name,column_name ASC|DESC
insert into table_name
values(value1,value2,value3,...)
insert into table_name(column1,column2,column3,...)
values(value1,value2,value3,...)
#会形成新行,但只在指定列插入数据,custormid列会自动更新
update table_name
set column1=value1,column2=value2,...
where some_column=some_value
#通过where来限制更新行,缺失where语句会对整列都进行修改
delete from table_name
where some_column = some_value
delete from table_name;
or
delete * from table_name;
#删除表中所有数据
#SQL Server / MS Access 语法
SELECT TOP number|percent column_name(s)
FROM table_name;
#MySQL 和 Oracle 中的 SQL SELECT TOP 是等价的
MySQL 语法
select column_name(s)
from table name
limit number;
#Oracle语法
select column_name(s)
from table_name
where rownum <= number;
select column_name(s)
from table_name
where column_name like pattern;
通配符 | 描述 |
---|---|
% | 替代一个字符 |
_ | centered |
[charlist] | 字符列中的任何单一字符 |
[^charlist] or [!charlist] | 不在字符列中的任何单一字符 |
# %运用
SELECT * FROM Customers
WHERE City LIKE '%es%';
#_运用
SELECT * FROM Customers
WHERE City LIKE '_erlin';
# SQL[charlist]运用 选取以a、b、c开头的所有
SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
#选取 City 不以 "b"、"s" 或 "p" 开始的所有客户
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
select column_name(s)
from table_name
where colunmn_name in (value1,value2,...);
select column_name(s)
from table_name
where column_name in(select column from table_w3c where ...);
select column_name(s)
from table_name
where colunmn_name between value1 and value2;
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND CategoryID NOT IN (1,2,3);
select table1_name.column_name(s),table2_name.column_name(s)
from table1_name
inner join table2_name
on table1_name.column = table2_name.column #基于表之间的共同字段相连接
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
#默认选取不同值,如果要允许重复值,使用union all
SELECT *
INTO newtable [IN externaldb] #外部数据库
FROM table1;
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';
两者的区别在于select into复制后创建一个新表,而insert select into复制后添加到一个已有的表格中