SQL 語句

--創建數據
create database student
on
(--主要文件
name='student_data',
filename='d:\database\student_data.mdf',
size=1mb,
maxsize=3mb,
filegrowth=1
)
log on--邏輯日誌文件
(
name=student_log,
filename='d:\database\student_log.ldf',
size=1,
maxsize=2,
filegrowth=10%
)
--修改數據庫
alter database student
modify file
(
name='student_data',
maxsize=5
)
alter database student
add file
(--添加次要文件
name='student_data2',
filename='d:\database\student_data2.ndf',
size=2,
maxsize=3,
filegrowth=1
)
--刪除數據庫
drop database student
create database aa
drop database aa,student

--配置
exec sp_dboption 'pubs','read only','true'
exec sp_dboption 'pubs','read only','false'
--使用T-SQL創建數據庫
create database accp
on primary
(name=accp_data,
filename='d:\備課資料\轉換課程\sql\accp.mdf',
size=2Mb,
maxsize=3Mb,
filegrowth=10%
)--主要數據文件(必需有,只有一個)
,
(
name=accp_data1,
filename='d:\備課資料\轉換課程\sql\accp.ndf',
size=2Mb,
maxsize=3Mb,
filegrowth=10%
)--次要數據文件(可以沒有,也可以有多個)
log on
(
name=accp_log,
filename='d:\備課資料\轉換課程\sql\accp.ldf',
size=1Mb,
maxsize=2Mb,
filegrowth=10%
)--數據日誌文件(必需有,至少一個)

--使用T-SQL刪除數據庫
drop datebase accp

--使用系統存儲過程配置數據庫選項
--將數據庫pubs設置爲只讀
exec sp_dboption 'pubs','read only',true
--將數據庫pubs設置爲自動週期性收縮
exec sp_dboption 'pubs','autoshrink',true
--將數據庫pubs設置爲只有一個用戶
exec sp_dboption 'pubs','single user'

--使用DBCC命令收縮指定的數據庫,並預留一定的空間
dbcc shrinkdatabase(pubs,10) --聲明decimal類型變量,第一個參數爲精確的位數,第二個爲精確的小數位
declare @var1 numeric(6,3),@var2 decimal(6,5),@var3 decimal(6,4)
set @var1=123.78456;
set @var2=1.2345678;
set @var3=12.345678;
select '@var1'=@var1,'@var2'=@var2,'@var3'=@var3

--創建用戶自定義數據類型
exec sp_addtype Phone,'varchar(11)',null


--創建表
create table book
(
book_id varchar(12) not null,
book_name varchar(20) not null,
book_publish varchar(20),
book_type varchar(10)not null
)
create table borrow
(
borrow_id int primary key identity(1,1),
book_id varchar(12) not null,
reader_id int not null ,
borrow_time datetime not null,
return_time datetime not null
)
create table reader
(
reader_id varchar(10) primary key,
reader_name varchar(20) not null,
reader_age tinyint not null,
reader_address varchar(50) not null
)
--修改表結構,爲表添加主鍵約束
alter table book
add constraint pk_book1 primary key(book_id)
--修改表結構,爲表添加唯一約束
alter table book
add constraint pk_book2 unique(book_id)

--修改表結構,爲表添加非空和默認約束
alter table book
add book_qty int not null default '1'

--修改表結構,爲表添加檢查約束
alter table book
add constraint pk_book3 check(book_qty>0)

--爲表中列添加標識列約束,只能在創建列時添加
create table book_user
(
bookid int identity(1000,2) primary key,
bookname varchar(12)
)
--查找表中的記錄
select * from book_user
--修改表中已有列的數據類型
alter table borrow
alter column reader_id varchar(10) not null


select * from reader
--刪除表
drop table book_user
--爲表實施外鍵約束
alter table borrow
add constraint fk_reader
foreign key(reader_id) references reader(reader_id)


alter table borrow
add constraint fk_book
foreign key(book_id) references book(book_id)
--刪除約束
alter table book
drop constraint pk_book2
--添加用戶數據類型
exec sp_addtype A_name,'varchar(20)','NULL'
--刪除用戶數據類型
exec sp_droptype A_name
--創建表
create table student
(
s_id varchar(20) primary key,--創建主鍵
s_name varchar(20) not null,
sex char(2) not null,
age tinyint not null,
phone varchar(20) not null,
address varchar(50)
)
--創建唯一和標識列約束,只能在字段創建時添加
create table aa
(
a_id int identity(1,1),
a_name varchar(10)
)
drop table aa
--添加主鍵約束
alter table aa
add constraint pk_aa
primary key(a_id)
--添加唯一約束
alter table aa
add constraint uq_name
unique(a_name)
--刪除列
alter table aa
drop column a_id
--添加字段,增加標識列約束
alter table aa
add aa_id int identity(1,3)
--唯一標識
create table bb
(
b_id uniqueidentifier default newid(),
b_name varchar(10)
)
--創建成績表
drop table 成績
--創建外鍵約束
create table 成績
(
編號 int identity(1,1) primary key,
學號 varchar(20) not null,
-- foreign key references student(s_id),--外鍵約束
課程 varchar(20) default 'vb' ,
成績 int not null default 60
)
--添加外鍵約束
alter table 成績
add constraint fk_學號
foreign key(學號)
references student(s_id)
--添加檢查約束
alter table student
add constraint ck_age
check (age>1)
--刪除約束
alter table student
drop constraint ck_student
alter table student
drop column age
create table book
(
book_id varchar(12) primary key,
book_name varchar(12) not null,
book_publish varchar(20) not null
)
create table reader
(
reader_id varchar(12) primary key,
reader_name varchar(20) not null,
reader_age int not null
)
create table borrow
(
borrow_id int identity(1000,1),
book_id varchar(12) foreign key(book_id) references book(book_id),
reader_id varchar(12) foreign key(reader_id) references reader(reader_id),
borrow_time datetime default getdate(),
return_time varchar(15) not null
)


--向book表中添加記錄
insert book values
('book0001','sql2000','chubanshe')
insert book values
('book0002','sql2000','chubanshe')

select * from book
--向reader表添加記錄
insert reader values('reader0001','rose',20)
insert reader values('reader0002','jhon',30)

--向從表中添加記錄
insert into borrow(book_id,reader_id,return_time)
values
('book0002','reader0002','未還')


delete from borrow where borrow_time='2003.4.8'
insert borrow(reader_id,book_id,borrow_time,return_time)
select reader_id,book_id,btime,rtime
from table1 where btime>'2003.4.9'

select * from borrow
delete from borrow where book_id='book0002'
select * from book
--內部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a inner join borrow as b
on a.book_id=b.book_id
--左外部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a left outer join borrow as b
on a.book_id=b.book_id
--右外部聯接(引用完整性)
select a.book_id,a.book_name,b.borrow_time
from book as a right outer join borrow as b
on a.book_id=b.book_id
--右外部連接
select btime,b.book_id,b.reader_id
from table1 a right outer join borrow b
on a.book_id=b.book_id

select * from borrow
select * from table1

--更新記錄
update table1
set book_id='book0001',reader_id='reader0002'
where book_id='book0002'
select * from table1

update table1
set rtime='未還'

select * from table1
delete from table1 where rtime='未還'
--刪除表中的數據
truncate table borrow
truncate table book

delete from book
select * from book
--關係運算符like
select * from authors
where au_lname like 'w%'
--%
--[]
--[^]
--_
--向表中添加記錄
insert into book values('book0001','vb','人民出版社','圖書',20)
insert into reader values('reader0001','rose',20,'road no.1')

alter table borrow
alter column return_time varchar(12) not null

insert into borrow(book_id,reader_id,borrow_time,return_time)
values('book0001','reader0001','2004-3-17','未還')


--創建表brw
create table brw
(
book_id varchar(12) not null,
reader_id varchar(20) not null,
borrow_time datetime not null,
return_time varchar(10) not null
)
--向brw表中添加記錄
insert into brw(book_id,reader_id,borrow_time,return_time)
values('book0001','reader0001','2004-3-17','未還')

--將brw表中的記錄添加到borrow表中
insert into borrow
select book_id,reader_id,borrow_time,return_time from brw

select * from borrow
--根據條件改更表的記錄
update book
set book_name='sql'
from book as a right outer join borrow as b on a.book_id =b.book_id
where a.book_id='book0001'
--刪除表中的所有記錄
delete from borrow
--刪除表中滿足條件的記錄
delete from borrow where book_id="book0001"
--創建數據庫
create database student
on
(
name='student_data',
filename='d:\stu\student_data.mdf',
size=2,
maxsize=4,
filegrowth=1
)
log on
(
name='student_log',
filename='d:\stu\student_log.ldf',
size=1,
maxsize=2,
filegrowth=1
)


--學生信息表
create table student
(
s_id varchar(20) primary key,--主鍵
s_name varchar(20) not null,--非空
sex varchar(4) not null default '男' check(sex='男' or sex='女'),--非空默認檢查
phone varchar(20) not null default 'no phone'
)
--學生成績表
create table chj
(
ch_id int identity(1,1) primary key,
s_id varchar(20) not null,
course varchar(20) not null,
chj int check(chj>=0 and chj<=100)
)
--添加外鍵
alter table chj
add constraint fk_s_id
foreign key(s_id)
references student(s_id)
--借閱信息表
create table borrowmessage
(
borrow_index int identity(1,1) primary key,
bookindex varchar(20) not null,
readerindex varchar(20) not null,
borrow_time datetime not null check(borrow_time<=getdate()),--字段級檢查
return_time datetime not null
)
--添加記錄級檢查約束(或表級)
alter table borrowmessage
add constraint ck_time
check(borrow_time <=return_time)
--刪除列
alter table borrowmessage
drop column borrow_time
--刪除約束
alter table borrowmessage
drop constraint ck_time
--添加列
alter table borrowmessage
add borrow_time datetime
not null default getdate()
--添加數據

insert into student
values('0404s1a2004','dd','男','130000000')
--查詢數據
select * from student2
select * from chj
insert chj
values('0404s1a2004','lgc',80)
--基於現有表中的數據,向目的表追加數據
insert student2
select s_id,s_name,sex,phone
from student
where s_id='0404s1a2010'
--修改數據
select * from student2
update student2
set sex='女',s_name='李四'
where s_id='0404s1a2002'
--多個表數據的更新
--內部連接
select s_name,course,chj
from student s inner join chj as c--別名
on s.s_id=c.s_id
--左外部連接
select s_name,course,chj
from student s left outer join chj as c--別名
on s.s_id=c.s_id
--右外部連接(引用完整)
select s_name,course,chj
from student s right outer join chj as c--別名
on s.s_id=c.s_id

select * from chj
--右外部連接(無約束)
select s_name,course,chj
from chj s right outer join student2 as c--別名
on s.s_id=c.s_id
--
create table emp
(
emp_id varchar(10) primary key,
emp_name varchar(10),
leader varchar(10)
)
select * from emp
insert emp
values('5','ee','')
--內部連接
select a.emp_name '員工',b.emp_name as '經理姓名'
from emp a inner join emp b
on a.leader=b.emp_id

delete from emp
where emp_id='1'

truncate table student
select * from student
truncate table chj
delete student
create table book_user
(
Buser_id char(4) primary key,
Buser_name char(10)not null
)
create table user_pa
(
Buser_id char(4) not null,
Buser_pass char(4) not null

)
drop table book_user
insert into book_user values('2','bb')
select * from book_user

truncate table book_user

alter table user_pa
add constraint fk_user foreign key(Buser_id) references book_user(Buser_id)
insert into user_pa values('1','aa')
delete from book_user
select stor_id,ord_num from sales

select stor_id from stores

select a.stor_id,a.ord_num,b.stor_name
from sales as a,stores as b
where a.stor_id=b.stor_id

select stor_id,qty from sales
order by qty desc,stor_id

create table borrow
(
borrow_id int identity(1000,1) primary key,
borrow_name varchar(20) not null
)
insert into borrow(borrow_name) values('sql')

select identity(int,1000,2) as brw_id
into brw
from borrow

select * from brw

alter table brw
add brw_name varchar(10)

insert into brw(brw_name) values('vb')

update brw
set brw_name='sql'
where brw_name is null
select * from brw

select top 10 percent * from sales order by qty desc

select count(*) from sales

--左外部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a left outer join borrow as b
on a.book_id=b.book_id
--左外部聯接
select count(*) as 'count_J'
from book as a left outer join borrow as b
on a.book_id=b.book_id
--左外部聯接
select count(b.borrow_time)
from book as a left outer join borrow as b
on a.book_id=b.book_id
select * from sales

select stor_id,avg(qty) as 'sum of qty '
from sales
where ord_num='6871'
group by stor_id
having avg(qty)=20
and stor_id='8042'

select avg(qty),stor_id from sales
--where avg(qty)>20
select * from sales
where title_id like '%8%'
--between and
select * from sales
where qty between 20 and 100 and qty<>20 and qty<>100

--in
select * from sales
where qty in (20,30,50)

--or
select * from sales
where qty=20 or qty=30 or qty=50

--time
declare @u_time as varchar(20)
SET @U_time=datepart(mi,getdate())
select '時間'=@u_time

select au_lname +'::'+au_fname as 'name'
from authors
select au_id,au_lname,au_fname,phone,state
from authors
where not state='ca'
order by au_lname desc

--order by
select stor_id,qty from sales
order by qty,stor_id
--查詢中的常量
select au_lname+'.'+au_fname '姓名'
from authors
--表的別名
select a.au_lname 姓,au_fname
from authors as a
--生成表查詢
select au_lname,au_fname
into authors_copy--新表名
from authors--現有的表名
where state='ca'

select identity(int,1,2) as '標識'
into au
from authors_copy
select * from au

--top
select top 1 percent au_lname,au_fname
from authors
where state='ca'

select top 3 * from sales order by qty desc

select distinct qty from sales
--group by
select top 1 stor_id,min(ord_num) as 最小值,avg(qty) 平均數量
from sales
group by all stor_id
--having avg(qty)>100
order by avg(qty) desc


select stor_id,ord_num,qty
from sales
group by stor_id,ord_num,qty


select stor_id,sum(qty)
from sales
where stor_id=7066
group by stor_id

select stor_id,sum(qty)
from sales
group by stor_id
having stor_id=7066
--like
select au_lname,au_fname
from authors
where au_lname like '_e%e_'
and au_fname like '[^a,e,i,o,u]%'

數據類型 字節大小 範圍 說明
整數類型 Bigint 8
Int 4 -2147483648—2147483647
Smallint 2 -32768--32767
Tinyint 1 0-255
Bit 0或1
小數類型 Decimal 與精度有關 -10^38-1—10^38-1 Decimal(12)
或Decimal(12,4)
Numeric 與精度有關 -10^38-1—10^38-1 同上
近似數值類型 Float 8 -1.79E-308—1.79E+308
Real 4 -3.40E-38—3.40E38
貨幣類型 Money 8 非常大
Smallmoney 4 -214748.3648—214748.3648
日期時間 Datetime 8 1753-1-1---9999-12-3
Smalldatetime 4 1900-1-1---2079-6-6
字符類型 Char <=8000個字符(定長)
Varchar <=8000個字符(不定長)
Text <=2G個字符(不定長)
Nchar <=4000個字符(定長)
Nvarchar <=4000個字符(不定長)
Ntext <=1G個字符(不定長)
二進制類型 Binary <=8000字節(定長)
Varbinary <=8000字節(不定長)
Image <=2^31-1字節(不定長) 聲音、圖像等








select stor_id,ord_num from sales

select stor_id from stores

select a.stor_id,a.ord_num,b.stor_name
from sales as a,stores as b
where a.stor_id=b.stor_id

select stor_id,qty from sales
order by qty desc,stor_id

create table borrow
(
borrow_id int identity(1000,1) primary key,
borrow_name varchar(20) not null
)
insert into borrow(borrow_name) values('sql')

select identity(int,1000,2) as brw_id
into brw
from borrow

select * from brw

alter table brw
add brw_name varchar(10)

insert into brw(brw_name) values('vb')

update brw
set brw_name='sql'
where brw_name is null
select * from brw

select top 10 percent * from sales order by qty desc

select count(*) from sales

--左外部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a left outer join borrow as b
on a.book_id=b.book_id
--左外部聯接
select count(*) as 'count_J'
from book as a left outer join borrow as b
on a.book_id=b.book_id
--左外部聯接
select count(b.borrow_time)
from book as a left outer join borrow as b
on a.book_id=b.book_id
select * from sales

select stor_id,avg(qty) as 'sum of qty '
from sales
where ord_num='6871'
group by stor_id
having avg(qty)=20
and stor_id='8042'

select avg(qty),stor_id from sales
--where avg(qty)>20
select * from sales
where title_id like '%8%'
--between and
select * from sales
where qty between 20 and 100 and qty<>20 and qty<>100

--in
select * from sales
where qty in (20,30,50)

--or
select * from sales
where qty=20 or qty=30 or qty=50

--time
declare @u_time as varchar(20)
SET @U_time=datepart(mi,getdate())
select '時間'=@u_time

select au_lname +'::'+au_fname as 'name'
from authors
--使用T-SQL創建數據庫
create datebase accp
on primary
(name=accp_mdf,
filename='d:\備課資料\轉換課程\sql\accp.mdf',
size=2M,
maxsize=3M,
filegrowth=10%
)--主要數據文件(必需有,只有一個)
(
name=accp_mdf,
filename='d:\備課資料\轉換課程\sql\accp.mdf',
size=2M,
maxsize=3M,
filegrowth=10%
)--次要數據文件(可以沒有,也可以有多個)
log on
(
name=accp_ldf,
filename='d:\備課資料\轉換課程\sql\accp.ldf',
size=1M,
maxsize=2M,
filegrowth=10%
)--數據日誌文件(必需有,至少一個)

--使用T-SQL刪除數據庫
drop datebase accp

--使用系統存儲過程配置數據庫選項
--將數據庫pubs設置爲只讀
exec sp_dboption 'pubs','read only',true
--將數據庫pubs設置爲自動週期性收縮
exec sp_dboption 'pubs','autoshrink',true
--將數據庫pubs設置爲只有一個用戶
exec sp_dboption 'pubs','single user'

--使用DBCC命令收縮指定的數據庫,並預留一定的空間
dbcc shrinkdatabase(pubs,10)
create table book_user
(
Buser_id char(4) primary key,
Buser_name char(10)not null
)
create table user_pa
(
Buser_id char(4) not null,
Buser_pass char(4) not null

)
drop table book_user
insert into book_user values('2','bb')
select * from book_user

truncate table book_user

alter table user_pa
add constraint fk_user foreign key(Buser_id) references book_user(Buser_id)
insert into user_pa values('1','aa')
delete from book_user
--關係運算符like
select * from authors
where au_lname like 'w%'
--%
--[]
--[^]
--_
--向表中添加記錄
insert into book values('book0001','vb','人民出版社','圖書',20)
insert into reader values('reader0001','rose',20,'road no.1')

alter table borrow
alter column return_time varchar(12) not null

insert into borrow(book_id,reader_id,borrow_time,return_time)
values('book0001','reader0001','2004-3-17','未還')


--創建表brw
create table brw
(
book_id varchar(12) not null,
reader_id varchar(20) not null,
borrow_time datetime not null,
return_time varchar(10) not null
)
--向brw表中添加記錄
insert into brw(book_id,reader_id,borrow_time,return_time)
values('book0001','reader0001','2004-3-17','未還')

--將brw表中的記錄添加到borrow表中
insert into borrow
select book_id,reader_id,borrow_time,return_time from brw

select * from borrow
--根據條件改更表的記錄
update book
set book_name='sql'
from book as a right outer join borrow as b on a.book_id =b.book_id
where a.book_id='book0001'
--刪除表中的所有記錄
delete from borrow
--刪除表中滿足條件的記錄
delete from borrow where book_id="book0001"
create table book
(
book_id varchar(12) primary key,
book_name varchar(12) not null,
book_publish varchar(20) not null
)
create table reader
(
reader_id varchar(12) primary key,
reader_name varchar(20) not null,
reader_age int not null
)
create table borrow
(
borrow_id int identity(1000,1),
book_id varchar(12) foreign key(book_id) references book(book_id),
reader_id varchar(12) foreign key(reader_id) references reader(reader_id),
borrow_time datetime default getdate(),
return_time varchar(15) not null
)


--向book表中添加記錄
insert book values
('book0001','sql2000','chubanshe')
insert book values
('book0002','sql2000','chubanshe')

select * from book
--向reader表添加記錄
insert reader values('reader0001','rose',20)
insert reader values('reader0002','jhon',30)

--向從表中添加記錄
insert into borrow(book_id,reader_id,return_time)
values
('book0002','reader0002','未還')


delete from borrow where borrow_time='2003.4.8'
insert borrow(reader_id,book_id,borrow_time,return_time)
select reader_id,book_id,btime,rtime
from table1 where btime>'2003.4.9'

select * from borrow
delete from borrow where book_id='book0002'
select * from book
--內部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a inner join borrow as b
on a.book_id=b.book_id
--左外部聯接
select a.book_id,a.book_name,b.borrow_time
from book as a left outer join borrow as b
on a.book_id=b.book_id
--右外部聯接(引用完整性)
select a.book_id,a.book_name,b.borrow_time
from book as a right outer join borrow as b
on a.book_id=b.book_id
--右外部連接
select btime,b.book_id,b.reader_id
from table1 a right outer join borrow b
on a.book_id=b.book_id

select * from borrow
select * from table1

--更新記錄
update table1
set book_id='book0001',reader_id='reader0002'
where book_id='book0002'
select * from table1

update table1
set rtime='未還'

select * from table1
delete from table1 where rtime='未還'
--刪除表中的數據
truncate table borrow
truncate table book

delete from book
select * from book
--聲明decimal類型變量,第一個參數爲精確的位數,第二個爲精確的小數位
declare @var1 numeric(6,3),@var2 decimal(6,5),@var3 decimal(6,4)
set @var1=123.78456;
set @var2=1.2345678;
set @var3=12.345678;
select '@var1'=@var1,'@var2'=@var2,'@var3'=@var3

--創建用戶自定義數據類型
exec sp_addtype Phone,'varchar(11)',null


--創建表
create table book
(
book_id varchar(12) not null,
book_name varchar(20) not null,
book_publish varchar(20),
book_type varchar(10)not null
)
create table borrow
(
borrow_id int primary key identity(1,1),
book_id varchar(12) not null,
reader_id int not null ,
borrow_time datetime not null,
return_time datetime not null
)
create table reader
(
reader_id varchar(10) primary key,
reader_name varchar(20) not null,
reader_age tinyint not null,
reader_address varchar(50) not null
)
--修改表結構,爲表添加主鍵約束
alter table book
add constraint pk_book1 primary key(book_id)
--修改表結構,爲表添加唯一約束
alter table book
add constraint pk_book2 unique(book_id)

--修改表結構,爲表添加非空和默認約束
alter table book
add book_qty int not null default '1'

--修改表結構,爲表添加檢查約束
alter table book
add constraint pk_book3 check(book_qty>0)

--爲表中列添加標識列約束,只能在創建列時添加
create table book_user
(
bookid int identity(1000,2) primary key,
bookname varchar(12)
)
--查找表中的記錄
select * from book_user
--修改表中已有列的數據類型
alter table borrow
alter column reader_id varchar(10) not null


select * from reader
--刪除表
drop table book_user
--爲表實施外鍵約束
alter table borrow
add constraint fk_reader
foreign key(reader_id) references reader(reader_id)


alter table borrow
add constraint fk_book
foreign key(book_id) references book(book_id)
--刪除約束
alter table book
drop constraint pk_book2

-- ==========================MySql ===========================
---遊標和存儲過程
/** add the Residential note Other category to all existing investor connections */
DROP PROCEDURE IF EXISTS insertInvestorCategories;
CREATE PROCEDURE insertInvestorCategories()
BEGIN
DECLARE investorId INT DEFAULT 0;
DECLARE done INT DEFAULT 0;

DECLARE cur CURSOR FOR
select ic.investor_connection_id
FROM investor_connection ic
where ic.investor_connection_id not in (select icInv.investor_connection_id from investor_connection_investment icInv);

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
REPEAT
FETCH cur into investorId;
IF NOT done THEN

insert into investor_connection_category (investor_connection_id, investment_category_id)
values (investorId, (select investment_category_id from investment_category where investment_category_key="LLI_PN_RES_LOAN_TYPE_OTHER"));

END IF;
UNTIL done END REPEAT;
CLOSE cur;
END;

call insertInvestorCategories();
DROP PROCEDURE IF EXISTS insertInvestorCategories;

-- ===================================
DROP PROCEDURE IF EXISTS switchCategoryLoanType;
CREATE PROCEDURE switchCategoryLoanType()
BEGIN
DECLARE categoryId INT DEFAULT 0;
DECLARE loanTypeId INT DEFAULT 0;
DECLARE investmentId INT DEFAULT 0;
DECLARE done INT DEFAULT 0;

DECLARE cur CURSOR FOR
select inv.investment_id,p.loan_type_id, inv.investment_category_id
listing_option_id
FROM promissory_note p inner join investment inv on p.investment_id = inv.investment_id;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;
REPEAT
FETCH cur into investmentId, loanTypeId, categoryId;
IF NOT done THEN

update promissory_note promnote
inner join investment invest on promnote.investment_id = invest.investment_id
set promnote.loan_type_id=
(select lli.lookup_list_item_id from lookup_list_item lli
where lli.list_key = (select cat.investment_category_key from investment_category cat
where cat.investment_category_id = categoryId)
)
where invest.investment_id=investmentId;

update investment invest
inner join promissory_note promnote on promnote.investment_id = invest.investment_id
set invest.investment_category_id=
(select cat.investment_category_id from investment_category cat
where cat.investment_category_key = (select lli.list_key from lookup_list_item lli
where lli.lookup_list_item_id = loanTypeId)
)
where invest.investment_id=investmentId;


END IF;
UNTIL done END REPEAT;
CLOSE cur;
END;

call switchCategoryLoanType();
DROP PROCEDURE IF EXISTS switchCategoryLoanType;
-- ================================
-- update table, first need remove the foreign key
set FOREIGN_KEY_CHECKS = 0;

set @oldID := (select sct2.sch_task_id from scheduler_tasks sct2
where sct2.sch_task_name = 'Item Status Notification');

set @maxID := (select max(sct1.sch_task_id) from scheduler_tasks sct1)+1;


update scheduler_tasks_config stc
set stc.sch_task_id = @maxID
where stc.sch_task_id = @oldID;

update scheduler_tasks sct
set sct.sch_task_id = @maxID
where sct.sch_task_name = 'Item Status Notification';

set FOREIGN_KEY_CHECKS = 1;

===添加列 和 註釋
alter table `item`
add is_delete INT(1) default 0 comment 'indicates if the item has been deleted from the user I-Page';
======================
-- 忘記密碼,重新設置
-- 跳過權限檢查啓動MySQL
c:\mysql\bin>mysqld -nt --skip-grant-tables

-- 重新打開一個命令窗口,進入c:\mysql\bin目錄,設置root的新密碼
c:\mysql\bin>mysqladmin -u root flush-privileges password "newpassword"
c:\mysql\bin>mysqladmin -u root -p shutdown

-------------------------------------------
-- oracle 循環插入5W條數據
declare
maxrecords constant int:=50000;
i int :=1;
begin
for i in 1..maxrecords loop
insert into bigdatatest(bigdatatest_id,title,data_type_code)
values(i+10,TO_CHAR('9999'+i),TO_CHAR('9999'+i));
end loop;
dbms_output.put_line(' 成功錄入數據! ');
commit;
end;

-------------------------------------------
oracle 數據導出:
1 將數據庫TEST完全導出,用戶名system 密碼manager 導出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
2 將數據庫中system用戶與sys用戶的表導出
exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
3 將數據庫中的表inner_notify、notify_staff_relat導出
exp aichannel/aichannel@TESTDB2 file= d:\data\newsmgnt.dmp tables=(inner_notify,notify_staff_relat)

4 將數據庫中的表table1中的字段filed1以"00"打頭的數據導出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"

上面是常用的導出,對於壓縮,既用winzip把dmp文件可以很好的壓縮。
也可以在上面命令後面 加上 compress=y 來實現。

數據的導入
1 將D:\daochu.dmp 中的數據導入 TEST數據庫中。
imp system/manager@TEST file=d:\daochu.dmp
imp aichannel/aichannel@HUST full=y file=file= d:\data\newsmgnt.dmp ignore=y
上面可能有點問題,因爲有的表已經存在,然後它就報錯,對該表就不進行導入。
在後面加上 ignore=y 就可以了。
2 將d:\daochu.dmp中的表table1 導入
imp system/manager@TEST file=d:\daochu.dmp tables=(table1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章