Table is still exist after dropping it and the whole database too - sql

I get this message after I dropped the table and the database completely
Msg 2714, Level 16, State 6, Line 12
There is already an object named 'Users' in the database
I tried to create and drop the same database but no use.
I also dropped the same table but when I want to create it, it shows the same error again.
I'll be glad for any help
CREATE DATABASE Music
GO
CREATE TABLE [Users](
[User_ID] int NOT NULL identity (1,1) Primary key,
[UserName] nvarchar (30) NOT NULL,
[UserEmail] nvarchar (30) NOT NULL,
[Password] nvarchar (30) NOT NULL
)
GO

CREATE DATABASE Music GO
USE [Music]
GO
CREATE TABLE [Users]( [User_ID] int NOT NULL identity (1,1) Primary key, [UserName]
nvarchar (30) NOT NULL, [UserEmail] nvarchar (30) NOT NULL, [Password] nvarchar (30) NOT NULL ) GO
you are creating the table in the database where you have the initial query to. See above to first switch to the new DB...

Related

Cannot insert the value NULL into column 'Id' although Id is set

I have a strange problem.
I want to insert an item to a table from database. I use Entity Framework.
Although the Id is set, I keep getting the following error:
Cannot insert the value NULL into column 'Id', table 'project_atp.dbo.ShoppingCarts'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
The table definition:
CREATE TABLE [dbo].[ShoppingCarts] (
[Id] INT NOT NULL,
[Guid] UNIQUEIDENTIFIER NULL,
[Name] NVARCHAR (255) NULL,
[Code] NVARCHAR (255) NULL,
[SupplierNo] NVARCHAR (255) NULL,
[SupplierName] NVARCHAR (255) NULL,
[Price] NVARCHAR (50) NULL,
[Quantity] INT NULL,
CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Can you please advise what could be wrong here! Thanks!
By default Entity Framework assumes that an integer primary key is database generated. As the result Entity Framework would not include Primary Key field in the actual INSERT statement.
I would try to either play along and ALTER the table to auto-generate the ID (which judging by your comment you did)
or set StoreGeneratedPattern property of OnlineCarStore.Models.ShoppingCarts Id column to 'None'
or use annotation: [DatabaseGenerated(DatabaseGeneratedOption.None)].

The model already has an element that has the same name - ASP.NET

I'm working with ASP.Net web application and whenever I tried to add a FOREIGN KEY this error appears in the Data tools operations :
SQL71508 :: The model already has an element that has the same name
dbo.FK_Sellers_Users. SQL71508 :: The model already has an element
that has the same name dbo.FK_Sellers_Users.
I don't understand what's the problem with FK! I have 2 tables with this error
table Sellers :
CREATE TABLE [dbo].[Sellers] (
[Seller_ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[SUsername] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[SEmail] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[City] NVARCHAR (50) NOT NULL,
[LastLoginDate] DATETIME NULL,
[CreatedDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Seller_ID] ASC),
CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SEmail]) REFERENCES [Users]([Email]),
CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SUsername]) REFERENCES [Users]([Username])
);
and table Users :
CREATE TABLE [dbo].[Users] (
[Id] INT NOT NULL IDENTITY,
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[UserType] INT NULL,
PRIMARY KEY CLUSTERED ([Id]),
CONSTRAINT [AK_Users_Username] UNIQUE ([Username]),
CONSTRAINT [AK_Users_Email] UNIQUE ([Email]),
);
Right there in your CREATE TABLE statement for dbo.Sellers, you have two FK constraints named FK_Sellers_Users.
Make those names unique, perhaps by adding the column name on the end.
You have two foreign keys with the same name FK_Sellers_Users. You should better use FK_Sellers_Users_Email and FK_Sellers_Users_Username.
Actually, this has to do with data that was already entered. Some of the PK and FK data that were entered before might have the same values. You need to delete those first and make the changes you need.
If your project uses SSDT (SQL Server Data Tools), you probably have a foreign key defined in a folder for Keys. Look in "DB Name\Schema Objects\Schemas\dbo\Tables\Keys."
Then Look in the table definition. "DB Name\Schema Objects\Schemas\dbo\Tables". The key could be defined in BOTH places.

an error occured while the batch was being executed

I'm trying to make a simple table in a database.
CREATE TABLE [dbo].[klanten]
(
[Klant_naam] TEXT NOT NULL PRIMARY KEY,
[Klant_adres] TEXT NULL,
[klant_gsm] TEXT NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] TEXT NOT NULL
)
When I try to update it, the following error pops-up.
As the documentation warns:
ntext , text, and image data types will be removed in a future version
of Microsoft SQL Server. Avoid using these data types in new
development work, and plan to modify applications that currently use
them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
So, try this instead:
CREATE TABLE [dbo].[klanten] (
[Klant_naam] varchar(max) NOT NULL PRIMARY KEY,
[Klant_adres] varchar(max) NULL,
[klant_gsm] varchar(max) NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] varchar(max) NOT NULL
)
Well, this doesn't quite work either, because there is a limit of 900 bytes for index keys. How about using a surrogate key and reasonable column lengths?
CREATE TABLE [dbo].[klanten] (
Klant_Id int not null identity(1, 1) primary key,
[Klant_naam] varchar(255) NOT NULL unique,
[Klant_adres] varchar(max) NULL,
[klant_gsm] varchar(max) NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] varchar(max) NOT NULL
);
Try to change "TEXT" data type to "NCHAR()" or any other similar to this.
This error usually occured when you try to update database, but some fields can't be updated. For example, I have 5 fields in a table:
[Id] INT IDENTITY (1, 1) NOT NULL,
[Path] NVARCHAR (MAX) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Url] NVARCHAR (MAX) NULL,
[SecureUrl] NVARCHAR (MAX) NULL
I've worked for some time with it, and make some records. And some of them have value NULL at [Url]. Suddenly I decide to change table:
[Id] INT IDENTITY (1, 1) NOT NULL,
[Path] NVARCHAR (MAX) NOT NULL,
[Name] NVARCHAR (50) NULL,
[Url] NVARCHAR (MAX) NOT NULL, //!!! HERE A PROBLEM, I ALREADY HAVE NULL RECORDS IN DATA
[SecureUrl] NVARCHAR (MAX) NULL
Problem is that my data has been made with the old model and it has records with the NULL at [Url], but in new model NULL value can't be at [Url]. So as with new model, old data can't be correct. Thus we have the error when update.

Unique Constraint in Visual Studio SQL Designer

I was looking at ways to make a newly added column a Unique but not primary column. I had a look at this W3School link. But Instead of following their approach I simply changed my table in the Visual Studio designer as.
CREATE TABLE [dbo].[Userpro] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NULL,
[Postcode] NVARCHAR (4) NULL,
[Gender] INT NULL,
[Blog] NVARCHAR (MAX) NULL,
[FeedBack] NVARCHAR (MAX) NULL,
[Username] NVARCHAR (50) NOT NULL Unique,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Notice that I simply added "Unique" [Username] NVARCHAR (50) NOT NULL Unique. I am unsure if this has the same effect or should I go back and just use the script in the link.
That is perfect.
Adding UNIQUE will have the effect you describe. It is also explained in the link you provide.

MSG 2714 There is an object already named

I'm using sql server 2008 management studio. Im trying to create two seemingly simple tables but continue getting this error at execution. I have tried changing the names of these tables to many different variations. Nothing is helping. Here below is what I am typing.
USE POS410;
go
CREATE TABLE [empl]
(
employeeID INT NOT NULL,
lname VARCHAR(15) NOT NULL,
fname VARCHAR(15) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(10) NOT NULL,
areacode INT NOT NULL,
tnumber INT NOT NULL,
EEO1classification VARCHAR(10) NOT NULL,
hiredate DATE NOT NULL,
salary INT NOT NULL,
gender VARCHAR(1) NOT NULL,
age INT NOT NULL,
clocknumb INT NOT NULL,
PRIMARY KEY(employeeID),
UNIQUE(employeeID),
UNIQUE(clocknumb),
FOREIGN KEY(clocknumb) REFERENCES [jb_ttl](Empnum),
);
go
CREATE TABLE [jb_ttl]
(
EEO1 VARCHAR(10) NOT NULL,
JobTitle VARCHAR(15) NOT NULL,
JobDscrpt TEXT NOT NULL,
ExemptNonExemptStatus VARCHAR NOT NULL,
Empnum INT NOT NULL,
PRIMARY KEY (Empnum),
);
go
Here is the error report:
Msg 2714, Level 16, State 6, Line 2 There is already an object named 'empl' in the database. Msg 2714, Level 16, State 6, Line 2 There is already an object named 'jb_ttl' in the database.
I would say that you've tried to run this script more than once. It would have failed the first time because you're trying to create the first table with a foreign key to the second, which didn't exist yet. I use the word "didn't" because if my guess is correct, it does now, and that's why you're getting the object already exists error.
As a side note, SQL has supported wrapping DDL (creating/modifying objects) at least going back to 2005. I normally wrap table changes in transactions, and don't commit the transaction until everything goes through with no error messages.
Give this a run to confirm if jb_ttl already exists:
select * from master.dbo.sysobjects where [name] = 'jb_ttl'
That will show you if anb object named jb_ttl already exists. Could it already exist as a procedure or trigger perhaps?