您好,登錄后才能下訂單哦!
小編給大家分享一下sqlserver的表、視圖、索引如何實(shí)現(xiàn)創(chuàng)建、修改、刪除操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
一、表相關(guān)
1、創(chuàng)建
USE [test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Ceshi]( [id] [int] NOT NULL, [name] [varchar(30)] NULL, CONSTRAINT [PK_Ceshi] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO --添加字段注釋 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主鍵一個(gè)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Ceshi', @level2type=N'COLUMN',@level2name=N'id' GO --修改字段注釋 EXEC sys.sp_updateextendedproperty @name=N'MS_Description', @value=N'主鍵一個(gè)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Ceshi', @level2type=N'COLUMN',@level2name=N'id' GO
2、修改
(1)修改表名:
EXEC sp_rename 'table_name', 'table_new_name'
(2)新增字段:
ALTER TABLE table_name ADD column_name datatype
(3)修改字段名:
EXEC sp_rename '表名.column_name','new_column_name','column'
(4)修改字段類型:
ALTER TABLE table_name ALTER COLUMN column_name datatype
(5)刪除字段:
ALTER TABLE table_name DROP COLUMN column_name
3、刪除
DROP TABLE `test`;
二、視圖相關(guān)
1、創(chuàng)建
USE [test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --注釋卸載這兒 CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition GO
2、修改
ALTER VIEW view_name AS SELECT * FROM ceshi;
3、刪除
DROP VIEW view_name
三、索引相關(guān)
1、創(chuàng)建
(1)在表上創(chuàng)建一個(gè)簡(jiǎn)單的索引
USE [test] GO CREATE NONCLUSTERED INDEX index_name ON table_name ( column_name ASC, column_name2 DESC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO
(2)在表上創(chuàng)建一個(gè)唯一的索引
USE [test] GO CREATE CLUSTERED INDEX index_name ON table_name ( column_name ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO
2、刪除
USE [test] GO DROP INDEX index_name ON table_name WITH ( ONLINE = OFF ) GO
以上是“sqlserver的表、視圖、索引如何實(shí)現(xiàn)創(chuàng)建、修改、刪除操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。