經常使用sql語句

添加一列 
alter table 表名 add 列名 類型; 
添加一行 
insert into 表名 values ('','',''); 


刪除一行 
delete from 表名 where 條件; 
刪除一列 
ALTER TABLE 表名 DROP COLUMN 列名; 


alter table student 
drop constraint pk_student  
--去掉這個表的主鍵 

alter table student 
add constraint pk_student primary key clustered (studentid, seq) 
--從新設置表的主鍵 


修改字段的值 
update 表名 set 字段='xxxx' where 字段='yyy' 

根據一張表建立另外一張表(也能夠在原表上加必定的條件) 
select * into a_s_unit from  s_unit weher id in(1,2,3,4) ;
 

oracle: 

create table  a_s_unit as select * from s_unit weher id in(1,2,3,4) ;
 



使用rename關鍵字來實現字段名的修改:alter table 表名 rename column舊的字段名 to 新的字段名名; 
使用modify關鍵字來實現對數據類型的修改:alter table 表名 modify 字段名 數據類型; 


oralce 修改有數據的列的類型 

1,創建臨時列 
alter table S_UNIT_INCOME add income_fee_bak decimal(18,6); 

2.把修改的列的數據導入到臨時列 
update s_unit_income set income_fee_bak=income_fee; 

3.把列的數據所有修改成空 
update s_unit_income set income_fee=''; 

4.修改列的類型 
alter table S_UNIT_INCOME modify income_fee decimal(18,6); 

5.把臨時列的數據導入到修改的列 
update s_unit_income set income_fee=income_fee_bak; 

6.刪除臨時列 
alter table S_UNIT_INCOME drop column income_fee_bak; 


大字段修改 

1,創建臨時列 
alter table s_project add iintro_bak varchar(4000); 

2.把修改的列的數據導入到臨時列 
update s_project set iintro_bak=intro; 

3.刪除列 
ALTER TABLE s_project DROP COLUMN intro; 

4.把臨時列命名爲刪除的列 
ALTER TABLE s_project RENAME COLUMN iintro_bak TO intro; 









增長主鍵約束 
alter table S_PROJECT add constraint PK__S_PROJECT primary key(ID); 

oracle修改數據庫密碼 
alter user 數據庫名 identified by 密碼; 



獲取當前時間 
cast((Datename(year,GetDate())+'-'+Datename(month,GetDate())+'-'+Datename(day,GetDate())) as datetime) 



查表 
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='base table' 



存儲過程 

--------------------------------------------------- 
declare @unitId varchar(32),@honorId varchar(32) 
declare project_cursor CURSOR FOR SELECT ID FROM s_honor where unit_id is null  and id in(select honor_id from s_honor_author where author_type='1') 
OPEN project_cursor 
FETCH NEXT FROM project_cursor INTO @honorId 
WHILE @@FETCH_STATUS=0 
BEGIN 
declare member_cursor CURSOR for select author_unit_id from s_honor_author where honor_id = @honorId and author_type='1' order by order_id 
OPEN member_cursor 
FETCH NEXT FROM member_cursor INTO @unitId 
update s_honor set unit_id=@unitId where id=@honorId 
CLOSE member_cursor 
DEALLOCATE member_cursor 
print ' honorId :'+@honorId +' unitId:'+@unitId 
FETCH NEXT FROM project_cursor 
INTO @honorId 
END 
CLOSE project_cursor 
DEALLOCATE project_cursor 


select ','+name from s_perosn for xml path(''), web

相關文章
相關標籤/搜索