1、創建表:
--創建表
--PS:會先檢查是否存在,在添加表
if NOT exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[User]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[User](
[ID] [int] IDENTITY(1,1) NOT NULL, --主鍵ID
[Name] [nvarchar](50) NULL, --姓名
[Sex] [bit] NULL,--性別
[Age] [int] NULL,--年齡
[CreationTime] [datetime] NULL,--創建時間
[Remark][nvarchar](MAX) NULL --備注
)
END
GO
2、洗掉表:
--洗掉表
drop table <表名>
--例如
drop table [User]
3、添加欄位
--添加欄位:
ALTER TABLE <表名> ADD <欄位名> <欄位型別> NULL
GO
例如
ALTER TABLE USer ADD ABCD Education nvarchar(50) NULL
--給User表添加一個字串型別的欄位名叫Education(學歷)的欄位,可為空
4、洗掉欄位
--洗掉欄位
ALTER TABLE <表名> DROP COLUMN <表列名>
例如
ALTER TABLE User DROP COLUMN Education
--洗掉表User的列Education
5、修改欄位名
--修改欄位名
EXEC sp_rename @objname = '表名.舊列名', @newname = '新列名', @objtype = 'column'
例如
EXEC sp_rename @objname = '[User].[Remark]', @newname = 'Remark2', @objtype = 'column'
--PS:執行完成后會提示:“注意: 更改物件名的任一部分都可能會破壞腳本和存盤程序,”
6、修改欄位型別
--修改欄位型別
alter table <表名> alter column <欄位名> <型別> not null
例如
alter table [User] alter column Remark decimal(18, 2) null
今天分享的就是些基礎的操作,后續會再添加的
如果對你有幫助,希望能得到你的認可(一鍵三連:點贊、收藏 + 關注)
謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/257518.html
標籤:其他
上一篇:mysql
下一篇:mysql事務相關復習
