建议用 TXT 文档来写代码 然后粘贴至cmd命令直接运行
创建students库,表名为student,表包含以下字段:
id(主键) name(姓名) age(年龄) sex(性别) sc(综合积分) ;
insert student values(0,"小姐",1,24,188)
create table student(
id int auto_increment primary key not null,
name varchar(10) not null,
sex enum("男","女","中性"),
age int ,
jf int
)default charset="utf8";
3)(10分)向表里添加5条数据,自定义各个字段数据类型;
insert student values(0,"小刘",1,20,158);
insert student values(0,"小崔",2,21,159);
insert student values(0,"小钱",3,22,128);
insert student values(0,"小林",2,23,138);
insert student values(0,"小姐",1,24,188);
4)(10分)查询出所有学生的信息,并输出;
select * from student;
5)(10分)查询所有女生的信息,并输出
select * from student where sex=2;
6)(10分)查询所有男生且综合积分49分以下的信息,并输出
select * from student where sex=1 and jf=158;
7)(10分)给综合积分低于49分以下的同学,综合积分加上50,并查看信息
update student set jf=jf+50 where jf=158;
8)(10分)查询出所有姓张的同学的信息,并输出。
select * from student where name like "%小%";
9)(10分)删除ID是1的记录,如果没有相关记录则提示
delete from student where id=1;
10)(10分)将性别为女的记录年龄统一加2岁,并查看信息
update student set age=age+2 where sex=2;
11)(10分)查看所有同学的综合积分情况
select jf from student;
1. 使用sql语句创建数据库,名称为CustomDB
2. 创建数据表customer(客户)、deposite(存款)、bank(银行),表结构如下:(10分)
customer的表结构
| 属性名称 | 类型与长度 | 中文含义 | 备注 |
| -------- | ------------ | -------- | ---------- |
| c_id | char(6) | 客户标识 | 主键,非空 |
| name | varchar(30) | 客户姓名 | 非空 |
| location | Varchar(30) | 工作地点 | |
| salary | decimal(8,2) | 工资 | |
bank的表结构
| 属性名称 | 类型与长度 | 中文含义 | 备注 |
| --------- | ---------- | -------- | ---------- |
| b_id | char(5) | 银行标识 | 主键,非空 |
| bank_name | char(30) | 银行名次 | 非空 |
deposite的表结构
| 属性名称 | 类型与长度 | 中文含义 | 备注 |
| --------- | ------------ | ---------- | -------------------------- |
| d_id | int | 存款流水号 | 主键,非空,自增 |
| c_id | char(6) | 客户标识 | 外键,关联customer表的c_id |
| b_id | char(5) | 银行标识 | 外键,关联bank表的b_id |
| dep _date | date | 存入日期 | |
| amount | decimal(8,2) | 存款金额 | |
3、录入数据:
customer的数据如下
| c_id | name | location | salary |
| ------ | ---- | -------- | ------ |
| 101001 | 孙杨 | 广州 | 1234 |
| 101002 | 郭海 | 南京 | 3526 |
| 101003 | 卢江 | 苏州 | 6892 |
| 101004 | 郭惠 | 济南 | 3492 |
bank的数据如下:
| b_id | bank_name |
| ----- | --------- |
| B0001 | 工商银行 |
| B0002 | 建设银行 |
| B0003 | 中国银行 |
| B0004 | 农业银行 |
deposite的数据如下:
| d_id | c_id | b_id | dep_date | amount |
| ---- | ------ | ----- | ---------- | ------ |
| 1 | 101001 | B0001 | 2011-04-05 | 42526 |
| 2 | 101002 | B0003 | 2012-07-15 | 66500 |
| 3 | 101003 | B0002 | 2010-11-24 | 42366 |
| 4 | 101004 | B0004 | 2008-03-31 | 62362 |
| 5 | 101001 | B0003 | 2002-02-07 | 56346 |
| 6 | 101002 | B0001 | 2004-09-23 | 353626 |
| 7 | 101003 | B0004 | 2003-12-14 | 36236 |
| 8 | 101004 | B0002 | 2007-04-21 | 26267 |
| 9 | 101001 | B0002 | 2011-02-11 | 435456 |
| 10 | 101002 | B0004 | 2012-05-13 | 234626 |
| 11 | 101003 | B0003 | 2001-01-24 | 26243 |
| 12 | 101004 | B0001 | 2009-08-23 | 45671 |
4、修改数据(sql语句实现):
1.将数据表deposite中孙杨的存款金额加10000
2.将数据表deposite中所属账户为工商银行并且存入日期为2011-04-05的人员的存款金额加1000
3.将数据表deposite中郭海的银行标识改为建设银行
5、查询数据(sql语句实现):
1.查询孙杨的存款信息(显示信息:客户ID,客户姓名,银行名称,存款金额)
2.查询日期为2011-04-05这一天进行过存款的客户ID,客户姓名,银行名称,存款金额
3.查询郭海在工商银行的存款信息(显示信息:客户ID,客户姓名,银行标识,银行名称,存款日期,存款金额)
4.查询工商银行存款大于等于一万的客户姓名(使用表链接和子查询两种方式实现)
5.查询在农业银行存款前五名的客户存款信息(显示信息:客户姓名,银行名称,存款金额)
6.查询姓“卢”的客户的存款信息(显示信息:客户姓名,银行名称,存款金额)
1. 使用sql语句创建数据库,名称为CustomDB
答:`create database if not exists CustomDB;`
2. 创建表,代码如下:
答:
```
/*创建顾客表*/
create table if not exists customer(
c_id char(6) primary key,
name varchar(30)not null,
location varchar(30),
salary decimal(8,2)
);
/*插入数据*/
insert into customer
values('101001','孙杨','广州',1234),
('101002','郭海','南京',3526),
('101003','卢江','苏州',6892),
('101004','郭惠','济南',3492);
/*创建银行表*/
create table if not exists bank(
b_id char(5) primary key,
bank_name char(30) not null
);
insert into bank
values('B0001','工商银行'),
('B0002','建设银行'),
('B0003','中国银行');
insert into bank
values('B0004','农业银行');
/*创建存款表(注意外键的代码使用)*/
create table if not exists deposite(
d_id int(10) auto_increment primary key,
c_id char(6),
b_id char(5),
dep_date date,
amount decimal(8,2),
constraint FK_c_id foreign key(c_id) references customer(c_id)
);
CREATE TABLE `deposit` (
`d_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`c_id` varchar(6) ,
`b_id` varchar(5) ,
`dep_date` date ,
`dep_type` enum('1','3','5') ,
`amount` decimal(8,2) ,
FOREIGN KEY (`c_id`) REFERENCES `customer` (`c_id`),
FOREIGN KEY (`b_id`) REFERENCES `bank` (`b_id`)
);
create table deposite(
d_id int not null auto_increment primary key,
c_id varchar(6),
b_id varchar(5),
dep_date date,
dep_type enum('1','3','5'),
amount decimal(8,2),
foreign key(c_id) references customer(c_id),
foreign key(b_id) references bank(b_id)
);
insert into deposite
values(1,'101001','B0001','2011-04-05',1,42526),
(2,'101002','B0003','2012-07-15',1,66500),
(3,'101003','B0002','2010-11-24',2,42366),
(4,'101004','B0004','2008-03-31',3,62362),
(5,'101001','B0003','2002-02-07',3,56346),
(6,'101002','B0001','2004-09-23',1,353626),
(7,'101003','B0004','2003-12-14',2,36236),
(8,'101004','B0002','2007-04-21',1,26267),
(9,'101001','B0002','2011-02-11',1,435456),
(10,'101002','B0004','2012-05-13',2,234626),
(11,'101003','B0003','2001-01-24',3,26243),
(12,'101004','B0001','2009-08-23',2,45671);
```
1. 将数据表deposite中孙杨的存款金额加10000
```
update deposite set amount =amount+10000
where c_id in(select c_id from customer where name='孙杨');
```
1. 将数据表deposite中所属账户为工商银行并且存入日期为2011-04-05的人员的存款金额加1000
```
update deposite set amount=amount+1000
where dep_date='2011-04-05' and b_id in(select b_id from bank where bank_name='工商银行');
```
1. 将数据表deposite中郭海的银行标识改为建设银行
```
update deposite set b_id=(select b_id from bank where bank_name='建设银行')
where c_id in(select c_id from customer where name='郭海');
```
1. 查询孙杨的存款信息(显示信息:客户ID,客户姓名,银行名称,存款金额)
```
## 内连接查询
select customer.c_id,customer.name,bank.bank_name,deposite.amount
from deposite inner join customer on customer.c_id=deposite.c_id
inner join bank on bank.b_id=deposite.b_id
and customer.name='孙杨';
##表连接查询
select customer.name,customer.c_id,bank.bank_name,deposite.amount
from customer,bank,deposite
where customer.c_id= deposite.c_id
and bank.b_id= deposite.b_id and customer.name='孙杨';
```
1. 查询日期为2011-04-05这一天进行过存款的客户ID,客户姓名,银行名称,存款金额
```
方法(1):内连接查询:
select customer.c_id,customer.name,bank.bank_name,deposite.amount
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join bank on bank.b_id=deposite.b_id
and deposite.dep_date='2011-04-05';
方法(2):表连接查询:
select customer.name,customer.c_id,bank.bank_name,deposite.amount
from deposite,customer,bank
where deposite.dep_date='2011-04-05'
and customer.c_id= deposite.c_id
and bank.b_id= deposite.b_id;
```
1. 查询郭海在工商银行的存款信息(显示信息:客户ID,客户姓名,银行标识,银行名称,存款日期,存款金额)
```
方法(1):内连接查询:
select customer.c_id,customer.name,bank.b_id ,bank.bank_name,
deposite.amount,deposite.dep_date
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join bank on bank.b_id=deposite.b_id
and customer.name='郭海' and bank.bank_name='建设银行';
方法(2):表连接查询:
select customer.c_id,customer.name,bank.b_id ,bank.bank_name,
deposite.amount,deposite.dep_date
from deposite,bank,customer
where customer.c_id= deposite.c_id
and bank.b_id=deposite.b_id
and customer.name='郭海' and bank.bank_name='建设银行';
```
1. 查询工商银行存款大于等于一万的客户姓名(使用表链接和子查询两种方式实现)
```
方法(1):子查询:
select customer.name FROM customer WHERE c_id in (
select c_id from deposite WHERE amount in
(select amount from deposite WHERE amount>1000 and b_id IN
(select b_id from bank where bank_name='工商银行')));
方法(2):表连接查询:
select customer.name
from deposite,bank,customer
where customer.c_id= deposite.c_id
and bank.b_id=deposite.b_id
where amount>10000 AND bank_name='工商银行';
方法(3):内连接查询:
select customer.name
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join bank on bank.b_id=deposite.b_id
where amount>10000 AND bank_name='工商银行';
```
1. 查询在农业银行存款前五名的客户存款信息(显示信息:客户姓名,银行名称,存款金额)
```
方法(1):内连接查询:
select customer.name,bank.bank_name,deposite.amount
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join bank on bank.b_id=deposite.b_id
where bank_name='农业银行' ORDER BY amount DESC LIMIT 5;
方法(2):表连接查询:
select customer.name,bank.bank_name,deposite.amount
from deposite,bank,customer
where customer.c_id= deposite.c_id
and bank.b_id=deposite.b_id
and bank_name='农业银行' ORDER BY amount DESC LIMIT 5;
```
1. .查询姓“卢”的客户的存款信息(显示信息:客户姓名,银行名称,存款金额)
```
方法(1):内连接查询:
select customer.name,bank.bank_name,deposite.amount
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join bank on bank.b_id=deposite.b_id where name LIKE '卢%';
方法(2):表连接查询:
select customer.name,bank.bank_name,deposite.amount
from deposite,bank,customer
where customer.c_id= deposite.c_id
and bank.b_id=deposite.b_id AND name LIKE '卢%';
```
1. 创建存储过程update_salary,更新customer表的salary属性,将salary低于5000的客户的salary变为原来的2倍.
```
create procedure update_salary()
update customer set salary = salary * 2
where salary < 5000;
```