数据库基础作业:数据库查询及约束【练习】

在这里插入图片描述

(1). 写出sql语句,查询所有年龄大于20岁的员工(2分)

(2). 写出sql语句,查询所有年龄小于25岁的女性员工(3分)

(3). 写出sql语句,统计男女员工各有多少名(3分)

(4). 写出sql语句,按照年龄倒序获取员工信息(3分)

(5). 写出sql语句,获取员工中哪个姓名具有重名现象(3分)

(6). 写出sql语句,查询所有姓张的员工(3分)

(7). 写出sql语句,查询住址为北京的前3条记录(3分)

(8). 写出sql语句,查询员工总数(3分)

(9). 写出sql语句,向表中插入一条记录(2分)

(10).写出sql语句,修改员工张四的住址为南京(2分)

(11).写出sql语句,删除年龄大于24岁的女员工(2分)

cmd/Java连接数据库是在这个步骤之后才能连接成功的,所以我们到这步之后才算是进入了MYSQL数据库
在这里插入图片描述

基本操作:

cd :change directory的缩写。数据库里面有很多表
show databases;
show tables;
desc employee;
 
drop database hzyc98;
drop table employee;

create table employee (Name char(30) not null,Sex char(8),Age int, Address char(30));

【约束条件】
创建表:约束(默认值、非空、独一无二)
PRI:主键primary(一个表只有一个主键,主键是赋给表头的某个列(Field)的)
primary key
not null
unique
default 

添加列表

insert into employee (Name,Sex,Age , Address ) value('张三', '女',19,'北京' );
insert into employee (Name,Sex,Age , Address ) value('李四', '男',20,'上海' );
insert into employee (Name,Sex,Age , Address ) value('王五', '女',25,'广州' );
insert into employee (Name,Sex,Age , Address ) value('王五', '男',22,'北京' );
insert into employee (Name,Sex,Age , Address ) value('薛六', '女',20,'北京' );
insert into employee (Name,Sex,Age , Address ) value('赵七', '男',28,'上海' );
insert into employee (Name,Sex,Age , Address ) value('张四', '女',23,'北京' );

答案仅供参考:

注意这里有个update不能加from,易错

1. 写出sql语句,查询所有年龄大于20岁的员工(2分)
Select * from employee where age >20;2. 写出sql语句,查询所有年龄小于25岁的女性员工(3分)
Select * from employee where age <25 and sex = ‘女’;3. 写出sql语句,统计男女员工各有多少名(3分)
Select sex,count(*) from employee group by sex;4. 写出sql语句,按照年龄倒序获取员工信息(3分)
Select * from employee order by age desc;5. 写出sql语句,获取员工中哪个姓名具有重名现象(3分)
Select name from employee group by name count(name)>1;6. 写出sql语句,查询所有姓张的员工(3分)
//模糊查询like
Select * from employee where name like ‘张%;7. 写出sql语句,查询住址为北京的前3条记录(3分)
select * from employee where address = '北京'order by address asc limit 0,3;8. 写出sql语句,查询员工总数(3分)
Select count(name) from employee;9. 写出sql语句,向表中插入一条记录(2分)
Insert into employee (Name ,Sex ,  Age , Address) value ('张四', '女',23,'北京');10.写出sql语句,修改员工张四的住址为南京(2分)
Update employee set address = ‘南京’ where name = ‘张四’;11.写出sql语句,删除年龄大于24岁的女员工(2分)
Delete from employee where age > 24 and sex = ‘女’;

猜你喜欢

转载自blog.csdn.net/weixin_43801418/article/details/110761248