Trying to add a table to my database in SQL Server Management Studio but it's throwing a wobbly. I'm sure it's real simple but my brain has gone to mush and I can't find the problem. Basically it's telling me the database already exists, yet it clearly doesn't.
Error(s):
Msg 3701, Level 11, State 5, Line 2
Cannot drop the table 'MySchema.mix_Case_Study-Module', because it does not exist or you do not have permission.
Msg 2714, Level 16, State 5, Line 4
There is already an object named 'mix_Case_Study-Module' in the database.
Msg 1750, Level 16, State 0, Line 4
Could not create constraint. See previous errors.
Msg 4902, Level 16, State 1, Line 2
Cannot find the object "MySchema.mix_Case_Study-Module" because it does not exist or you do not have permissions.
SQL:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO
Any help appreciated.
Your constraint name and table name are the same.
CREATE TABLE [MySchema].[mix_Case_Study-Module]
and
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED
You are having issues because you're first trying to drop a table that does not exist.
You should be using something like this:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID([MySchema].[mix_Case_Study-Module], 'u') IS NOT NULL
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module_PK] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO
Related
I am trying to create a table with a stored procedure. I scripted the table as a CREATE statement and copied the file contents into my procedure, which works great. Except that when it sets defaults, it sets the first columns default as the whole second line.
Here's the procedure:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
')
After execution, column [fldName] has a default value of FOR [fldName] ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
It makes sense that it's just not recognizing the second quotation mark in (N''), but why not? And how do I fix it?
UPDATE:
I have learned that the proper way to create tables is through reading and executing sql script files. This keeps data manipulation and database manipulation seperate, and easier to manage.
If you have an apostrophe within a string in SQL, you need to escape it with an additional apostrophe. Try:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')
It's not clear (to me at least) why you're doing it this way though, instead of just:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
Also not clear why you're creating a table within a procedure, especially without checking whether it already exists first - can you guarantee that this table won't already exist every time your procedure is run?
You have to double the quotation marks when inside a string:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC ('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')`
Not saying it is the right thing to do to create a table via stored procedure but...you need to escape your quotes:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')
Im trying to use Linqpad so as to use LINQ instead of TSQL when trying to remove some records from a table that is part of a "dependency tree" that spans probably 10-12 tables. So I created some tables in a test db and im experimenting with the DeleteAllOnSubmit and SubmitChanges methods.
I get an FK error. Yet, Im deleting the child objects first. How do I delete when there is FK relations (without using cascading on the FK's)?
The DELETE statement conflicted with the REFERENCE constraint
"FK_OrderItem_Order". The conflict occurred in database "Test", table
"dbo.OrderItem", c...
Tables:
USE [Test]
GO
/****** Object: Table [dbo].[Order] Script Date: 6/9/2016 2:50:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Order](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrderNum] [varchar](20) NOT NULL,
[Description] [varchar](100) NULL,
CONSTRAINT [PK_Order] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[OrderItem] Script Date: 6/9/2016 2:50:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[OrderItem](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrderId] [int] NOT NULL,
[OrderItemName] [varchar](100) NOT NULL,
[Qty] [int] NOT NULL,
CONSTRAINT [PK_OrderItem] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[OrderItem] WITH CHECK ADD CONSTRAINT [FK_OrderItem_Order] FOREIGN KEY([OrderId])
REFERENCES [dbo].[Order] ([Id])
GO
ALTER TABLE [dbo].[OrderItem] CHECK CONSTRAINT [FK_OrderItem_Order]
GO
Here is my LINQ statement
List<Order> olist = (from a in Orders where a.Id == 2 select a).ToList();
List<int> olistidlist = olist.Select (o => o.Id).ToList();
List<OrderItem> oilist = (from a in OrderItems where olistidlist.Contains(a.Id) select a).ToList();
//see results before
olist.Dump();
OrderItems.DeleteAllOnSubmit(oilist);
Orders.DeleteAllOnSubmit(olist);
//save changes
SubmitChanges();
//see results after
olist.Dump();
You are deleting OrderItems of which the OrderItem.Id is equal to values in olistidlist (only 2, presently). Of course, this must be OrderItem.OrderId.
By the way, an easier way to delete parent and child items generally is:
foreach (var order in Orders.Where(...))
{
OrderItems.DeleteAllOnSubmit(order.OrderItems);
Orders.DeleteOnSubmit(order);
}
So you don't have to move around with Id values.
I'm aware that this question gets asked a million times, but I've already checked, double, and triple that corresponding values already exist in my parent table. I'm trying to populate a bridge table between my Albums and Artists, both of which are already populated. After getting errors in the C# program that was auto-populating, I tried inserting a single value manually, and still got the INSERT error.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Album](
[Album ID] [int] IDENTITY(1,1) NOT NULL,
[Album Title] [nchar](50) NOT NULL,
[Release Year] [int] NULL,
CONSTRAINT [PK_Album] PRIMARY KEY CLUSTERED
(
[Album 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
/****** Object: Table [dbo].[Album-Artists] Script Date: 3/6/2015 12:49:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Album-Artists](
[Album ID] [int] NOT NULL,
[Artist ID] [int] NOT NULL,
CONSTRAINT [PK_Album-Artists] PRIMARY KEY CLUSTERED
(
[Album ID] ASC,
[Artist 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
/****** Object: Table [dbo].[Artists] Script Date: 3/6/2015 12:49:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Artists](
[Artist ID] [int] IDENTITY(1,1) NOT NULL,
[Artists Name] [nchar](20) NOT NULL,
CONSTRAINT [PK_Artists] PRIMARY KEY CLUSTERED
(
[Artist 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
GO
ALTER TABLE [dbo].[Album-Artists] WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Album] FOREIGN KEY([Album ID])
REFERENCES [dbo].[Album] ([Album ID])
GO
ALTER TABLE [dbo].[Album-Artists] CHECK CONSTRAINT [FK_Album-Artists_Album]
GO
ALTER TABLE [dbo].[Album-Artists] WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Artists] FOREIGN KEY([Album ID])
REFERENCES [dbo].[Artists] ([Artist ID])
GO
ALTER TABLE [dbo].[Album-Artists] CHECK CONSTRAINT [FK_Album-Artists_Artists]
GO
(I've tried with and without the table quantifiers (Artists.[Artist ID], etc))
Your foreign key is trying to match ArtistId to AlbumId.
This is incorrect. You need to recreate it.
ALTER TABLE dbo.[Album-Artists]
DROP CONSTRAINT [FK_Album-Artists_Artists];
ALTER TABLE [dbo].[Album-Artists]
WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Artists]
FOREIGN KEY([Artist ID]) REFERENCES [dbo].[Artists] ([Artist ID]);
Also the correct syntax for the insert is
INSERT INTO [dbo].[Album-Artists]
([Album ID], [Artist ID])
VALUES (10, 3);
Though dot-separated prefixes are currently ignored in the column list for INSERT statements.
Finally I would avoid using spaces or - in object names so you don't have to continually use quoted identifiers or square brackets and can use the unquoted form.
INSERT INTO dbo.AlbumArtists
(AlbumId, ArtistId)
VALUES (10, 3);
I am trying to execute the following but its giving an error of, I tried doing it with designer, same error saying data type, precision and length must be the same, even though they are
ALTER TABLE CustomerUsers
ADD CONSTRAINT fk_CustomerUsers_Users
FOREIGN KEY (CustomerID)
REFERENCES Customers(UniqueID)
Error thrown:
Msg 1769, Level 16, State 1, Line 1
Foreign key 'fk_CustomerUsers_Users' references invalid column 'UniqueID' in referencing table 'CustomerUsers'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Here is My Table Structure.
CREATE TABLE [dbo].[Customers](
[CustomerID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](500) NOT NULL,
[ContentLocation] [nvarchar](500) NOT NULL,
[UniqueID] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[CustomerUsers](
[CustomerUserID] [int] IDENTITY(1,1) NOT NULL,
[CustomerUniqueID] [uniqueidentifier] NOT NULL,
[UserID] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_CustomerUsers] PRIMARY KEY CLUSTERED
(
[CustomerUserID] 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
ALTER TABLE [dbo].[CustomerUsers] WITH CHECK ADD CONSTRAINT [FK_CustomerUsers_AspNetUsers] FOREIGN KEY([UserID])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[CustomerUsers] CHECK CONSTRAINT [FK_CustomerUsers_AspNetUsers]
GO
There is no column with the name CustomerID in CustomerUsers just CustomerUniqueID
modify your query as below
ALTER TABLE CustomerUsers
ADD CONSTRAINT fk_CustomerUsers_Users
FOREIGN KEY (CustomerUniqueID)
REFERENCES Customers(UniqueID)
Query same as #Drew
I do believe the following should do the trick (in the case that you're wanting to set up CustomerUsers.CustomerUniqueID as a FK)
ALTER TABLE customerusers
ADD CONSTRAINT fk_customerusers_users FOREIGN KEY (customeruniqueid)
REFERENCES customers(uniqueid)
what I changed: inferred that error was related to column not existing, noticed that you had already created a column which looked like what you might have been trying to shoot for, added that
I created a table manually and after that selected script table as new query and changed table name and executed the query. I am getting the error as
Msg 170, Level 15, State 1, Line 12 Line 12: Incorrect syntax near
'('.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE dbo.[KitCodeProperties](
[KitPropertiesId] [int] IDENTITY(1,1) NOT NULL,
[KitCodeName] [varchar](50) NULL,
[KitCodeDescription] [varchar](200) NULL,
[ShippingInstructions] [varchar](200) NULL,
[DepartmentId] [int] NULL,
[KitCodeActive] [bit] NULL,
CONSTRAINT [PK_KitCodeProperties] PRIMARY KEY CLUSTERED
(
[KitPropertiesId] 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
I took you code and pasted it into my 2012 developer edition. Had no issues creating the table with and without SET commands.
Therefore, the syntax looks good.
Are you selecting on a section of the window?
Make sure you are not selection anything in the new query window and press F5 to execute the whole window as one batch.
If this works, you were highlighting only a section of the code.
A simplified version of the SSMS code.
-- Remove old existing table
IF OBJECT_ID('[dbo].[KitCodeProperties]') > 0
DROP TABLE [dbo].[KitCodeProperties];
-- Create new table
CREATE TABLE [dbo].[KitCodeProperties]
(
[KitPropertiesId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
[KitCodeName] [varchar](50) NULL,
[KitCodeDescription] [varchar](200) NULL,
[ShippingInstructions] [varchar](200) NULL,
[DepartmentId] [int] NULL,
[KitCodeActive] [bit] NULL,
);
The cut down version works fine in SQL Fiddler.