SQL语句入门
http://www.cnblogs.com/scorpioying/archive/2013/02/26/2933717.html
这篇文章里面的命令是mySQL的命令行,,部分命令如show databases等,在vb内不知如何执行。
create相关
show databases; 显示出现有的数据库 use database_x; 现在要使用数据库database_xshow tables;显示所有的表 (注意:最后有个s)show columns from coffee; 查看表结构
show create table coffee; 查看表信息,包括建表语句create table coffee (id int(5) not null,coffee_name varchar(25)); 创建一张表,包含id和coffee_name两个字段alter table student rename ss; 改变表名alter table ss drop mark; 删除表ss中mark列
alter table coffee add taste varchar(10); 增加新的一列
select语句
select * from coffee where id="1"; 查询出id=1的所有信息 select coffee_name from coffee where id="2"; 查询出id=2的coffee name
select * from club where id between "1" and "3"; 查询出id=1到3的条目
select * from club where mark not between 48 and 50; 查询mark不在48与50之间的club
select * from club where id="1" or id="3"; 查询出id=1和id=3这两个条目 select * from club where mark>50; 查询出mark大于50的club
select * from club where id in("1","3","4"); 查询id=1,3,4的条目 select * from club where id not in("1","3","4");
select * from club where name like "M%r"; 通配符%表示任意长度的字符(可以是0,汉字为两个字符) select * from club where name like "M_r"; _表示单个字符
select * from club where id in("1","3","4") and mark>50; 多重查询
select * from club order by mark desc; 按照mark降序排列(desc:降序,usc:升序)
数量查询相关
select count(*) from club; 查询club中有多少条记录 select count(distinct mark) from club; 不同分数的有多少条记录 select sum(mark) from club; 积分总和 select avg(mark) from club; 平均积分 select max(mark) from club; 最高积分 select min(mark) from club; 最低积分
update语句
update club set mark=mark-8 where id="1"; id=1的俱乐部积分罚8分
delete语句
delete from club where id="001"; 删除id=001的俱乐部信息
insert语句
insert into coffee (id,coffee_name,taste,rank)values ("1","BlueMountain","well","5");插入数据
insert into coffee (id,coffee_name,taste,rank) values("1","BlueMountain","well","5"), ("2","Blue","well","5"); 插入多个数据
注意
1、引号都可以用单引号,在程序中方便一些。
2、access执行insert into 会出现莫名的语法错误,有一个原因是数据库的字段名和其关键字重了(万恶的错误!)解决方式是 [] :
insert into accesstable ([order]) values (‘will be ok’) 博客园有人总结过这类错误,请搜索之。