zoukankan      html  css  js  c++  java
  • SQL脚本修改表结构

    SQL脚本修改表结构 
    新建表:
    create table [表名]
    (
    [自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,
    [字段1] nVarChar(50) default '默认值' null ,
    [字段2] ntext null ,
    [字段3] datetime,
    [字段4] money null ,
    [字段5] int default 0,
    [字段6] Decimal (12,4) default 0,
    [字段7] image null ,
    )

    删除表:
    Drop table [表名]

    Truncate table [表名] --不写log 而且自增字段复位从1开始

    插入数据:
    INSERT INTO [表名] (字段1,字段2) VALUES (1,'ABC')

    删除数据:
    DELETE FROM [表名] WHERE [字段名]>100

    更新数据:
    UPDATE [表名] SET [字段1] = 1,[字段2] = 'ABC' WHERE [字段三] = 'what'

    新增字段:
    ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL

    删除字段:
    ALTER TABLE [表名] DROP COLUMN [字段名]

    修改字段名称:

    sp_rename   'IncidentReport.CompletedById ', 'CompletedBy', 'column '    

    修改字段:
    ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL

    重命名表:(Access 重命名表,请参考文章:在Access数据库中重命名表)
    sp_rename '表名', '新表名', 'OBJECT'

    新建约束:
    ALTER TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= '2010-12-1')

    删除约束:
    ALTER TABLE [表名] DROP CONSTRAINT 约束名

    删除索引:

    DROP INDEX [IX_afk_ProtectionRisk_ProtectionDepartment] ON [ProtectionRisk] ;
    go

    新建默认值
    ALTER TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT 'ABC' FOR [字段名]

    删除默认值
    ALTER TABLE [表名] DROP CONSTRAINT 默认值名

    新增主外键关系:

    ALTER TABLE [dbo].[IncidentReport] WITH CHECK ADD CONSTRAINT [FK_IncidentReport_IncidentReportTemplate] FOREIGN KEY([IncidentReportTemplateId])
    REFERENCES [dbo].[IncidentReportTemplate] ([Id])
    GO

    新增字段含默认值

    ALTER TABLE incidenttemplate ADD IsShowHealthCheck bit NOT NULL default 1

    --Synchronize field 'AspNetIsApproved' with field 'IsApproved'
    WITH Users AS
    (
    SELECT u.UserName,m.IsApproved FROM [AspNetApplicationServices].[dbo].[aspnet_Membership] m JOIN [AspNetApplicationServices].[dbo].[aspnet_Users] u ON m.UserId=u.UserId
    )
    UPDATE dbo.UserProfile SET AspNetIsApproved=Users.IsApproved FROM dbo.UserProfile,Users WHERE dbo.UserProfile.UserName=Users.UserName
    --插入自增长字段值
    set IDENTITY_INSERT  entityformatGroup ON
    INSERT INTO entityformatGroup (id) VALUES (1)
    set IDENTITY_INSERT  entityformatGroup OFF
    --判断数据是否存在
    IF (exists (select * from sys.objects where name = 'UpdateTranslationForDynamicTextTrigger'))
        ...
    go

    添加一行新数据:

    IF ((Select count(*) from [dbo].[DifficultyLevel]) = 0)

           BEGIN

                  SET IDENTITY_INSERT [dbo].[DifficultyLevel] ON

                 

                  INSERT [dbo].[DifficultyLevel] ([Id]) VALUES (1)

                  INSERT [dbo].[DifficultyLevelTranslation] ([Id], [Language], [TITLE], [IsSynchronized]) VALUES (1, 'xx-XX', 'Default',1)

                  SET IDENTITY_INSERT [dbo].[DifficultyLevel] OFF

           END

    将已经存在数据的列修改为自增长列:

    begin transaction
     
    create table test_tmp
    (
    Id int not null identity(1,1) PRIMARY KEY
    )
    go
     
    set identity_insert test_tmp on
    go
     
    if exists(select * from dbo.ReportText)
    exec(' insert into test_tmp(Id) select id from ReportText with(holdlock tablockx)')
    go
     
    set identity_insert test_tmp off
    go
     
    ALTER TABLE [ReportText] DROP CONSTRAINT FK_ReportText_OpModelWorkstream
    ALTER TABLE [dbo].[ReportTextTranslation] DROP CONSTRAINT FK_ReportTextTranslation_ReportText
    
    Drop table [ReportText]
    go
     
    exec sp_rename N'test_tmp' ,N'ReportText' , 'OBJECT'
    GO
    
    
    ALTER TABLE [dbo].ReportTextTranslation WITH CHECK ADD CONSTRAINT FK_ReportTextTranslation_ReportText FOREIGN KEY([Id])
    REFERENCES [dbo].ReportText ([Id])
    GO
     
    commit
     
    GO

     FUNCTION:

    CREATE FUNCTION [dbo].[udf_GetLanguageFromUserName]
    (    
        -- Add the parameters for the function here
        @UserName NVARCHAR(255)
    )
    RETURNS @rTable TABLE 
    (
    Id NVARCHAR(5),
    Language NVARCHAR(2),
    Country NVARCHAR(2),
    Name NVARCHAR(120)
    )
    AS
    
    BEGIN
    
    DECLARE @IsAdministrator BIT
    DECLARE @UserLanguage NVARCHAR(5)
    SELECT @IsAdministrator = IsAdministrator, @UserLanguage = LanguageId
    FROM UserProfile
    WHERE UserName = @UserName
    
    -- === 
    IF @IsAdministrator = 1 OR RIGHT(@UserLanguage, 2) = 'XX'
        BEGIN
            --- Admin - return all languages
            INSERT INTO @rTable(Id, Language, Country, Name)
            SELECT Id, LEFT(Id, 2) AS Language, RIGHT(Id, 2) AS Country, Name
            FROM [Language] L
        END
    ELSE
        BEGIN
            --- Not Admin - return languages that match with the users country
            INSERT INTO @rTable(Id, Language, Country, Name)
            SELECT Id, LEFT(Id, 2) AS Language, RIGHT(Id, 2) AS Country, L.Name
            FROM [Language] L INNER JOIN UserProfile UP ON RIGHT(L.Id, 2) = RIGHT(UP.LanguageId, 2)    
            WHERE UP.UserName = @UserName
        END
        
    -- === 
    RETURN
    -- === 
    END
  • 相关阅读:
    Java泛型
    pyhthon 求GPA平均学分绩点
    机器学习 数据预处理之独热编码(One-Hot Encoding)
    python 取整itertools
    python 输入输出
    jQuery插件开发模式
    shiro 与 web 的结合
    java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
    使用quartz 定时任务
    EasyIcon:免费图标搜索和下载平台
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/2334873.html
Copyright © 2011-2022 走看看