1.專案介紹:
– 建立銀行資料庫
– 建立資料表customer(客戶)、bank(銀行)、deposit(存款)
– 向表中插入測試資料
– 對資料進行增刪改等操作
2.建立庫:
1.檢視資料庫是否啟動。
2.mysql -uroot -p # 連線本地資料庫
3.\s # 檢視資料服務的狀態
4.建立資料庫並設定字符集:
create database bank charset=utf8;
5.檢視建立的資料庫:
show databases;
3.建立表:
建立客戶表:
create table customer(
c_id char(6) primary key not null,
name varchar(30) not null,
location varchar(30)
, salary decimal(8,2));
建立銀行表:
create table banks(
b_id char(5) primary key not null,
bank_name varchar(30) not null
); 建立存款表:
create table deposite(
d_id int(10) auto_increment primary key not null,
c_id char(6)
, b_id char(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 banks(b_id)
);
4.插入資料:
insert customer values
('101001'
,'孫楊'
,'廣州'
,'1234'),
('101002'
,'郭海'
,'南京'
,'3526'),
('101003'
,'盧江'
,'蘇州'
,'6892'),
('101004'
,'郭慧'
,'濟南'
,'3492'),
('101006'
,'同學1'
,'北京'
,'12000'),
('101007'
,'同學2'
,'北京'
,'13000'),
('101008'
,'同學3'
,'北京'
,'14000'),
('101009'
,'同學4'
,'北京'
,'13500'),
('101010'
,'同學5'
,'北京'
,'13000'),
('101005'
,'徐老師'
,'北京'
,'85000');
insert banks values
('b0001'
,'工商銀行'),
('b0002'
,'建設銀行'),
('b0003'
,'中國銀行'),
('b0004'
,'農業銀行');
insert deposite values
(0,'101001'
,'b0001'
,'2011-04-05'
,'3'
,'42526'),
(0,'101002'
,'b0003'
,'2012-07-15'
,'5'
,'66500'),
(0,'101003'
,'b0002'
,'2010-11-24'
,'1'
,'42366'),
(0,'101004'
,'b0004'
,'2008-03-31'
,'1'
,'62362'),
(0,'101005'
,'b0003'
,'2002-02-07'
,'3'
,'56346'),
(0,'101006'
,'b0001'
,'2004-09-23'
,'3'
,'353626'),
(0,'101007'
,'b0004'
,'2003-12-14'
,'5'
,'36236'),
(0,'101008'
,'b0002'
,'2007-04-21'
,'5'
,'26267'),
(0,'101005'
,'b0002'
,'2011-02-11'
,'1'
,'435456'),
(0,'101006'
,'b0004'
,'2012-05-13'
,'1'
,'234626'),
(0,'101003'
,'b0003'
,'2001-01-24'
,'5'
,'26243'),
(0,'101004'
,'b0001'
,'2009-08-23'
,'3'
,'45671'
);
5.實戰練習
1)在bank中插入一條新記錄b0005,交通銀行
insert into banks(b_id,bank_name) values(
'b0005'
,'交通銀行');
2)查詢出每名帳戶的銀行存款金額(排序)
select
*from deposite order by amount;
3)給自己的帳戶多存入10000元
-- update deposite set amount=amount+10000 where c_id='101005'
; update deposite set amount=amount+10000
where c_id in(
select c_id from customer where name='徐老師');
4)檢視'徐老師'帳戶的金額:
select c.c_id,c.name, b.b_id,b.bank_name,d.amount, d.dep_type,d.dep_date
from deposite as d
inner join customer as c on c.c_id =d.c_id
inner join banks as b on b.b_id = d.b_id
and c.name='徐老師'
; 5)刪除'同學1'的存款記錄,並檢視結果。
delete from deposite where deposite.c_id
in(select customer.c_id from customer where customer.name='同學1');
select c.c_id,c.name, b.b_id,b.bank_name,d.amount, d.dep_type,d.dep_date
from deposite as d
inner join customer as c on c.c_id =d.c_id
inner join banks as b on b.b_id = d.b_id
and c.name='同學1'
; 6) 查詢郭海在建設銀行的存款資訊(顯示資訊:客戶id,客戶姓名,銀行標識,銀行名稱,存款日期,存款金額)
方法(1):內連線查詢:
select customer.c_id,customer.name,banks.b_id ,banks.bank_name,
deposite.amount,deposite.dep_date
from deposite
inner join customer on customer.c_id= deposite.c_id
inner join banks on banks.b_id=deposite.b_id
and customer.name='郭海' and banks.bank_name='中國銀行'
;
方法(2):表連線查詢:
select customer.c_id,customer.name,banks.b_id ,banks.bank_name,
deposite.amount,deposite.dep_date
from deposite,banks,customer
where customer.c_id= deposite.c_id
and banks.b_id=deposite.b_id
and customer.name='郭海' and banks.bank_name='中國銀行'
;
資料庫SQL實戰
無emp no birth date first name last name gender hire date 10008 1958 02 19 saniya kalloufi m1994 09 15 示例1無 無 select from employeesorder byhire datedes...
資料庫SQL實戰
找出所有員工當前 to date 9999 01 01 具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示 create table salaries emp no int 11 not null,salary int 11 not null,from date date not ...
資料庫SQL實戰
獲取當前 to date 9999 01 01 薪水第二多的員工的emp no以及其對應的薪水salary create table salaries emp no int 11 not null,salary int 11 not null,from date date not null,to d...