ERROR when Creating Table

2

I have the following Script.

USE [PS_GameData]
GO

/****** Object:  Table [dbo].[Guilds]    Script Date: 07/24/2018 17:33:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Guilds](
    [RowID] [int] IDENTITY(1,1) NOT NULL,
    [GuildID] [int] NOT NULL,
    [GuildName] [varchar](30) NOT NULL,
    [MasterUserID] [varchar](12) NOT NULL,
    [MasterCharID] [int] NOT NULL,
    [MasterName] [varchar](30) NOT NULL,
    [Country] [tinyint] NOT NULL,
    [TotalCount] [smallint] NOT NULL,
    [GuildPoint] [int] NOT NULL,
    [Del] [tinyint] NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [DeleteDate] [datetime] NULL,
 CONSTRAINT [PK_Guilds] PRIMARY KEY CLUSTERED 
(
    [GuildID] 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


SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Guilds] ADD  CONSTRAINT [DF_Guilds_Del]  DEFAULT (0) FOR [Del]
GO

I want to Execute but when I try Create the table I get an error:

Msg 2714, Level 16, State 5, Line 2
There is already an object named 'PK_Guilds' in the database.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
Msg 4902, Level 16, State 1, Line 2
Cannot find the object "dbo.Guilds" because it does not exist or you do not 'have permissions.'

I think I should bore the PK_Guilds but I do not know where or how I get it in the SQL server DB 2008 R2

    
asked by Juan Carlos Villamizar Alvarez 24.07.2018 в 23:58
source

1 answer

2

If you do not know which table is the constraint, you can run this query:

USE databasename; 

GO

SELECT i.name AS IndexName, OBJECT_NAME(ic.OBJECT_ID) AS TableName, 
       COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1

Then to erase it:

ALTER TABLE "table_name"
DROP CONSTRAINT "CONSTRAINT_NAME";

Or if it's easier, just change the name to the constraint.

    
answered by 25.07.2018 / 00:07
source