1:查询数据库dbName有多少张表
select count(*) from information_schema.tables where table_schema='dbName’;
如果是SQL Server数据库的话:
select name from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 //查询所有表名
select count(name) from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 //查询一共有多少张表
2:根据某个字段合并表
select id,name,sum(operate) from test group by name;
执行结果:
3: 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
select name from table group by name having min(fenshu)>80
4:删除除了自动编号不同, 其他都相同的学生冗余信息
delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by 学号, 姓名, 课程编号, 课程名称, 分数)
学生表 如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
5:如何截断表
截断表的意思是:(1)将表中数据全部清空(2)重新添加数据的话,id还从1开始。相当于重建表。
truncate user; 截断用户表
如果表中有外键的话,只不能执行truncate了。可以采用下面的语句:
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE user;
SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=0;的意思是取消外键。
SET FOREIGN_KEY_CHECKS=1;的意思是恢复外键。
6:查看数据库字段的属性(Collation字符编码)
show full columns from user;
7:查看字符集的长度
show character set
8:left join和inner join