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

  • Post author:
  • Post category:其他


在这里插入图片描述

(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 = ‘女’;

Code Name College

101 李二 数学

102 张三 数学

103 李四 计算机

104 王五 计算机

105 薛六 软件

106 赵七 计算机



建student表

create table Student (
	Code int primary key auto_increment,
	Name varchar(20),
	College varchar(20)
);
alter table student auto_increment = 101;

insert into student (name, college) 
value('李二', '数学');
insert into student (name, college) 
value('张三', '数学');
insert into student (name, college) 
value('李四', '计算机');
insert into student (name, college) 
value('王五', '计算机');
insert into student (name, college) 
value('薛六', '软件');
insert into student (name, college) 
value('赵七', '计算机');



建scores表

create table scores (
	SID int,
	stuCode int,
	Course varchar(20),
	Score int
);

insert into scores value (1,101,'线性代数',68);
insert into scores value (2,103,'离散数学',78);
insert into scores value (3,101,'高等数学',55);
insert into scores value (4,104,'离散数学',98);
insert into scores value (5,102,'离散数学',45);
insert into scores value (6,105,'离散数学',59);
insert into scores value (7,103,'高等数学',83);
insert into scores value (8,101,'离散数学',60);
insert into scores value (9,106,'高等数学',68);



版权声明:本文为weixin_43801418原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。