SQL Server操作實例
阿新 • • 發佈:2017-07-10
tween bsp 交流 targe inf 余額 長春 建數據庫 foreign
創建數據庫
create database accountInfo/*創建賬戶信息數據庫*/
創建數據表
1 /*定義主碼、外碼、和人數、余額的取值範圍。*/ 2 /*創建儲蓄所表*/ 3 create table bank( 4 bank_ID int primary key identity(10001,1),/*從10001開始,每次增加1*/ 5 bank_name nvarchar(20), /*儲蓄所名稱,*/ 6 bank_address nvarchar(40), 7 bank_peopleNum int check(bank_peopleNum between1 and 30), 8 bank_city nvarchar(10) 9 ) 10 11 /*創建賬戶表*/ 12 /*帳戶(編號,姓名,余額,建立日期,儲蓄所編號)*/ 13 create table account( 14 account_id int primary key identity(1,1), 15 account_name nvarchar(15), 16 account_balance int check(account_balance>= 0 ), 17 account_found_date char(8),18 bank_ID int foreign key references bank(bank_id) 19 ) 20 21 /*創建借貸表*/ 22 /*借貸(帳戶,借貸類型,金額,日期)*/ 23 create table borrow( 24 borrow_id int primary key identity(1,1), 25 account_ID int foreign key references account(account_ID), 26 type_borrow nvarchar(10), 27 money_sum int check(money_sum>=0), 28 date_borrow char(8) 29 )
插入數據
1 --插入bank數據 2 insert into bank values(‘長春南湖路‘,‘朝陽區‘,10,‘長春市‘) 3 insert into bank values(‘西安大路‘,‘朝陽區‘,13,‘長春市‘) 4 insert into bank values(‘幸福二路‘,‘南關區‘,11,‘長春市‘) 5 insert into bank values(‘幸福三路‘,‘南關區‘,12,‘長春市‘) 6 insert into bank values(‘桃林街‘,‘城關區‘,18,‘靈寶市‘) 7 8 --插入account數據 9 insert into account values(‘趙大‘,10000,20170510,10001) 10 insert into account values(‘錢二‘,5000,20160120,10001) 11 insert into account values(‘張三‘,50000,20161120,10001) 12 insert into account values(‘李四‘,50000,20161120,10005) 13 insert into account values(‘孫五‘,500000,20161110,10005) 14 15 --插入borrow數據 16 insert into borrow values(1,‘短期‘,1000,20170516) 17 insert into borrow values(2,‘長期‘,3000,20170416) 18 insert into borrow values(3,‘長期‘,200000,20170510)
連接查詢
--內連接 select account_name,account_balance,bank_name from account , bank where account.bank_ID=bank.bank_id and bank_name=‘長春南湖路‘ --左外連接 select bank.bank_ID,count(account_id)‘賬戶個數‘,sum(account_balance)‘余額總數‘ from bank left outer join account on(bank.bank_ID=account.bank_ID) group by bank.bank_ID
嵌套查詢
select account_name,account_balance from account where bank_ID in(select bank_ID from bank where bank_name=‘長春南湖路‘)
select account_name from account where account_balance>(select MAX(account_balance) from account where account_name=‘長春南湖路‘)
select* from account as A,Bank as B where A.bank_ID=B.bank_ID and B.bank_city in (select bank_city from bank group by bank_city having count(bank_ID)=(select min(Nm) from(select bank_city,count(bank_ID) from bank group by bank_city )as _bank(Cy,Nm) ) );
select account_id from account where account_id not in (select id from( select account_ID from borrow group by account_ID ) as v(id)--,Nm))
組合查詢
select account_id from account except (select account_ID from borrow )
創建及使用視圖
1 create view view1 as 2 select account.bank_ID, 3 sum(account_balance)‘balanceSum‘ 4 from bank,account 5 where bank.bank_ID=account.bank_ID 6 group by account.bank_ID 7 8 select v1.bank_ID vid 9 from(select max(v.balanceSum)as balanceSum from view1 v) 10 su,view1 v1 11 where v1.balanceSum=su.balanceSum
存儲過程
--根據上面基本表的信息定義一個存儲過程,完成下面功能: --入口參數:儲蓄所編號 --1 顯示儲蓄所信息 --2 如果沒有帳戶,刪除該儲蓄所記錄 --3 如果帳戶余額總額低於100萬元,開一個新帳戶 alter PROCEDURE pr_bank(@bank_id int) as BEGIN select *from bank where bank_ID = @bank_id if (select count(*) from account where bank_id=@bank_id)=0 delete from bank where bank_ID=@bank_id if (select sum(account_balance) from account where bank_id=@bank_id group by bank_id)<1000000 insert into account(bank_id) values(@bank_id) END exec pr_bank 10005
作者:耑新新,發布於 博客園
轉載請註明出處,歡迎郵件交流:[email protected]
SQL Server操作實例