SQL大讨论---有这方面兴趣爱好和问题的请进!

========Title========
sql语句大讨论---有这方面兴趣爱好和问题的请进!
========Content========
跨服务器的SQL语句如何书写
SELECT      * 
FROM            OPENDATASOURCE( 
                'SQLOLEDB', 
                'Data  Source=服务名;User  ID=用户;Password=密码' 
                ).库名.dbo.表名 

--------------------------------------------------------------- 

select  *  into  本地库名..表名  from  OPENDATASOURCE( 
                'SQLOLEDB', 
                'Data  Source=远程ip;User  ID=sa;Password=密码' 
                ).库名.dbo.表名 

insert  本地库名..表名  from  OPENDATASOURCE( 
                'SQLOLEDB', 
                'Data  Source=远程ip;User  ID=sa;Password=密码' 
                ).库名.dbo.表名 

或使用联结服务器: 
EXEC  sp_addlinkedserver   
    '别名',   
    '',   
    'MSDASQL', 
    NULL, 
    NULL, 
    'DRIVER={SQL  Server};SERVER=远程名;UID=用户;PWD=密码;' 
GO 
然后你就可以如下: 
select  *  from  别名.库名.dbo.表名 
insert  库名.dbo.表名  select  *  from  别名..库名.dbo.表名 
select  *  into  库名.dbo.新表名  from  别名..库名.dbo.表名 
go

sql语句输入相应表名就可以查到表的字段名,对应好数据库 查询是否存在该表语句
select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '相应表名')
查询是否存在该表语句
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tb_cost]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tb_cost]
GO
创表语句

CREATE TABLE [dbo].[tb_cost] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[inputtime] [datetime] NOT NULL ,
[pushcount] [int] NOT NULL ,
[revertcount] [int] NOT NULL ,
[revertrate] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[unitprice] [float] NOT NULL ,
[cost] [float] NOT NULL ,
[income] [float] NOT NULL ,
[rate] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[pushinfor] [varchar] (8000) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO


建索引脚本:
create clustered index 索引名 on 表名(表.字段)

在表增加一个字段,例如fa字段

alter table 表名 add fa int not null default 0

选择前面 m-n 条数据的语句

例如选择前面第 5-10(m=10,n=5) 条记录的sql语句如下:

sql="select top 6 * from table where (id not in (select top 4 id from table order by id desc)) order by id desc"

其中6,4 是这样得来的

sql="select top m-n+1 * from table where (id not  in (select top n-1 id from table))



有a,b,c 3表公有字段为ID怎么连?

在SQL SERVER中,上述查询建议采用下面的形式:
SELECT
aa.name,bb.name,cc.name
FROM
a AS aa INNER JOIN b AS bb ON aa.id=bb.id
INNER JOIN c AS cc ON aa.id=cc.id

另一种形式也是合法的,如下:
SELECT aa.name,bb.name,cc.name
FROM a as aa,b as bb ,c as cc
WHERE aa.id=bb.id and aa.id=cc.id
1.增加主键
   alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN);
   指定表空间
   alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN) using index tablespace TABLE_SPACE_NAME;
  2.增加外键
   alter table TABLE_NAME add constraint FK_NAME foreign key (TABLE_COLUMN) references KEY_TABLE_NAME;
  3.使主键或外键失效、生效
   alter table TABLE_NAME disable(enable) constraint KEY_NAME;
  4、查看各种约束
   select constraint_name,table_name,constraint_type,status from user_constraints;
   select constraint_name, constraint_type,search_condition, r_constraint_name from user_constraints where table_name = upper('&table_name')
   select c.constraint_name,c.constraint_type,cc.column_name
   from user_constraints c,user_cons_columns cc
   where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')
   and c.owner = cc.owner and c.constraint_name = cc.constraint_name
   order by cc.position;
  5、删除主键或外键
   alter table TABLE_NAME drop constraint KEY_NAME;
  6、建外键
   单字段时:create table 表名 (col1 char(8),
   cno char(4) REFERENCE course);
   多个字段时,在最后加上 Foreign Key (字段名) REFERENCE 表名(字段)
   连带删除选项 (on delete cascade
   当指定时,如果父表中的记录被删除,则依赖于父表的记录也被删除
   REFERENCE 表名() on delete cascade;
  7、删除带约束的表
   Drop table 表名 cascade constraints;
  8:索引管理
  <1>.creating function-based indexes
  sql> create index summit.item_quantity on summit.item(quantity-quantity_shipped);
  <2>.create a B-tree index
  sql> create [unique] index index_name on table_name(column,.. asc/desc) tablespace
  sql> tablespace_name [pctfree integer] [initrans integer] [maxtrans integer]
  sql> [logging | nologging] [nosort] storage(initial 200k next 200k pctincrease 0
  sql> maxextents 50);
  <3>.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows
  <4>.creating reverse key indexes
  sql> create unique index xay_id on xay(a) reverse pctfree 30 storage(initial 200k
  sql> next 200k pctincrease 0 maxextents 50) tablespace indx;
  <5>.create bitmap index
  sql> create bitmap index xay_id on xay(a) pctfree 30 storage( initial 200k next 200k
  sql> pctincrease 0 maxextents 50) tablespace indx;
  <6>.change storage parameter of index
  sql> alter index xay_id storage (next 400k maxextents 100);
  7.allocating index space
  sql> alter index xay_id allocate extent(size 200k datafile 'c:/oracle/index.dbf');
  <8>.alter index xay_id deallocate unused;
  <9>、查看索引
   SQL>select index_name,index_type,table_name from user_indexes order by table_name;
  <10>、查看索引被索引的字段
   SQL>select * from user_ind_columns where index_name=upper('&index_name');
  11、创建序列
   select * from user_sequences;
   create sequence SEQ_NAME start with 1000
   maxvalue 1000 increment by 1;
   alter sequence SEQ_NAME minvalue 50 maxvalue 100;
  12、删除重复行
   update a set aa=null where aa is not null;
  
   delete from a where rowid!=
   (select max(rowid) from a b where a.aa=b.aa);
  13、删除同其他表相同的行
   delete from a where exits
   (select 'X' from b where b.no=a.no);
   或
   delete from a where no in (select no from b);
  14、查询从多少行到多少行的记录(可以用在web开发中的分页显示)
   select * from ( select rownum row_id,b.* from (select a.* from sys_oper a) b )
   where row_id between 15 and 20
  15、对公共授予访问权
   grant select on 表名 to public;
   create public synonym 同义词名 for 表名;
  16、填加注释
   comment on table 表名 is '注释';
   comment on column 表名.列名 is '注释';
  17、分布式数据库,创建数据库链路
   create [public] database link LINKNAME
   [connect to USERNAME identified by PASSWORD]
   [using 'CONNECT_STRING']
   可以在服务器端,也可以在客户端建立,但必须注意,两台服务器之间
   数据库必须可以互访,必须各有各自的别名数据库
  18、查看数据库链路
   select * from all_db_links;
   select * from user_db_links;
   查询 select * from TABLENAME@DBLNKNAME;
   创建远程数据库同义词
   create synonym for TABLENAME@DBLNKNAME;
   操纵远程数据库记录
   insert into TABLENAME@DBLNKNAME (a,b) values (va,vb);
   update TABLENAME@DBLNKNAME set a='this';
   delete from TABLENAME@DBLNKNAME;
   怎样执行远程的内嵌过程
   begin
   otherdbpro@to_html(参数);
   end;
  

未完待续........支持的顶 !!!1
最后编辑2007-01-25 10:00:03