刪掉索引腳本
1. sql server中 表中如何創建索引
if exists(select *from sys.objects where naem = 'newindex')
drop index newindex
create index
--===================================
竟然沒有懸賞...唉...
那算了吧
我還是都告訴你吧..
看個示例
自己琢磨去:
--==============================================
use master
go
if db_id(N'zhangxu')is not null
drop database zhangxu
go
create database zhangxu
sp_helpdb zhangxu
use zhangxu
go
IF EXISTS (SELECT *FROM SYS.OBJECTS WHERE NAME = N'WORKER')
DROP TABLE WORKER
GO
create table worker
(
w_id int identity (1000,1) not null,
w_name Nvarchar(10) unique,
w_age SMALLINT CONSTRAINT CK_W_AGE CHECK(w_age>20 and w_age<150),
w_pay money DEFAULT 0,
CONSTRAINT PK_W_ID PRIMARY KEY(W_ID)
)
SELECT *FROM WORKER--用查詢技術查看錶信息
sp_help worker--利用存儲過程查看錶信息
/*
創建簡單的非聚集索引
*/
USE ZHANGXU
GO
if exists(select name from sys.indexes where name = N'IX_ID_NAME')
DROP INDEX IX_ID_NAME on worker
go--檢查是否存在索引,有則刪除索引
create index IX_ID_NAME--創建索引
on worker(w_id,w_name)--在ID NAME 兩個欄位上創建非聚集索引
drop index worker.IX_ID_NAME--刪除索引
select *from sys.indexes where name = 'IX_ID_NAME'--查看索引
/*
創建唯一非聚集索引
*/
USE ZHANGXU
GO
IF EXISTS(SELECT NAME FROM SYS.INDEXES WHERE NAME = N'IX_W_NAME')
DROP INDEX IX_W_NAME ON WORKER
GO
CREATE UNIQUE INDEX IX_W_NAME--唯一非聚集索引
ON WORKER(W_NAME)
/*
查看索引T-SQL腳本
*/
--IX_W_NAME 唯一 非聚集索引
USE [zhangxu]
GO
/****** 對象: Index [IX_W_NAME] 腳本日期: 07/29/2007 16:54:53 ******/
CREATE UNIQUE NONCLUSTERED INDEX [IX_W_NAME] ON [dbo].[worker]
(
[w_name] ASC
)
WITH
(
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
IGNORE_DUP_KEY = OFF,
ONLINE = OFF
)
ON [PRIMARY]
--PK_W_ID聚集索引
USE [zhangxu]
GO
/****** 對象: Index [PK_W_ID] 腳本日期: 07/29/2007 16:56:45 ******/
ALTER TABLE [dbo].[worker]
ADD CONSTRAINT [PK_W_ID] PRIMARY KEY CLUSTERED
(
[w_id] ASC
)
WITH
(
SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF,
ONLINE = OFF
) ON [PRIMARY]
--UQ_WORKER 唯一,非聚集索引
USE [zhangxu]
GO
/****** 對象: Index [UQ__worker__07020F21] 腳本日期: 07/29/2007 16:58:38 ******/
ALTER TABLE [dbo].[worker]
ADD UNIQUE NONCLUSTERED
(
[w_name] ASC
)
WITH
(SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF,
ONLINE = OFF
) ON [PRIMARY]
select *from worker
insert into worker(w_name,w_age,w_pay) values('王國龍',25,4500)
2. 搜集SQL常用的操作語句
結構化查詢語言(Structured Query Language)簡稱SQL(發音:/ˈes kjuː ˈel/ "S-Q-L"),是一種特殊目的的編程語言,是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;同時也是資料庫腳本文件的擴展名。
一、插入(復制)表數據
1、INSERT INTO 語句:
(1)插入新的一行數
[sql] view plain ;
INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
(2)在指定的列中插入數據
[sql] view plain ;
INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');
2、SQL SELECT INTO 語句可用於創建表的備份復件
(1)在建表時復制所有數據
[sql] view plain ;
create table userinfo_new as select * from userinfo;
(2)在建表時復制部分數據
[sql] view plain ;
create table userinfo_new1 as select id,username from userinfo;
(3)在添加時復制所有數據
[sql] view plain ;
insert into userinfo_new select * from userinfo;
(4)在添加時復制部分數據
[sql] view plain ;
insert into userinfo_new(id,username) select id,username from userinfo;
二、修改表數據
Update 語句
(1)無條件更新
[sql] view plain ;
update userinfo set userpwd='111',email='[email protected]';
(2)有條件更新
[sql] view plain ;
update userinfo set userpwd='123456' where username='xxx';
三、刪除表數據
1、DELETE 語句
(1)無條件刪除
[sql]view plain;
daletefromuserinfo;
(2)有條件刪除
[sql]view plain;
='yyy';
四、查詢表數據
1、SELECT 語句:
(1)查詢所有欄位
[sql] view plain ;
select * from users;
(2)查詢指定欄位
[sql] view plain ;
select username,salary from users;
2、SELECT DISTINCT 語句
從 Company" 列中僅選取唯一不同的值,需要使用 SELECT DISTINCT 語句:
[sql] view plain ;
SELECT DISTINCT Company FROM Orders;