1.设计一个学生表,字段包含(学生ID,姓名[name],年龄[age],班级[class],性别[sex])
create table student (id int,name varchar(20),age int(10),class varchar(20),sex char(1));
2.修改表,添加列 爱好
alter table student add column (love varchar(20));
3.查询出班里的平均年龄
select avg(age) from student ;
4.查询姓名里包含 “伟” 的同学
select * from student where name like '%伟%';
LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用是在一个字符型字段列中检索包含对应子串的。
5.按年龄大小 进行降序排序
select * from student order by age desc;
6.查询年龄在18-22之间的同学
select * from student where age>18 and age<22;
select * from student where age betwen 18 and 22;
7.将学生ID字段名更改为sno
alter table student change id sno int not null
8.加载数据到数据表的方式。请列举,并举例
使用create table 语句来指定表的结构
mysql->create table pet(name varchar(20),owner varchar(20),species varchar(20),sex char(1),birth date,death date)
例如 记录文本位于 D:\java\opensource\mysql\pet.txt
mysql> load data local infile 'D:/java/opensource/mysql/pet.txt' into table pet;如果您在Windows的编辑器中创建了一个使用\r\n 作为行终止符的文件(多条语句的时候),则应该使用此语句:
mysql> LOAD DATA LOCAL INFILE 'D:/java/opensource/mysql/pet.txt' INTO TABLE pet
-> LINES TERMINATED BY '\r\n';
9.什么是左连接、自然连接、右连接 ,以及有什么区别
左连接:select * from student a left join attendance b on a.sno = b.sno;
内连接:select a.*,b.* from student a inner join attendance b on a.sno=b.sno;
右连接:select * from student a right join attendance b on a.sno = b.sno;
左连接:左边有的,右边没有的为null
右连接:左边没有的,右边有的为null
内连接:显示左边右边共有的
10.按年龄进行分组,并查出都是同一个年龄的人数大于 5的年龄
select age from student group by age having count(age)>5;