I created some tables but when I go to enter the values for the table I\'m getti
ID: 3689824 • Letter: I
Question
I created some tables but when I go to enter the values for the table I'm getting an error.
CREATE TABLE Users (UserID INT PRIMARY KEY, EmailAddress VARCHAR(255) NOT NULL, FirstName VARCHAR(60) NOT NULL, LastName VARCHAR(60) NOT NULL);
CREATE TABLE Products (ProductID INT PRIMARY KEY, ProductName VARCHAR(255) NOT NULL);
CREATE TABLE Downloads (DownloadID INT PRIMARY KEY, UserID INT FOREIGN KEY REFERENCES Users(UserID), DownloadDate DATE, FileName VARCHAR(255) NOT NULL, ProductID INT FOREIGN KEY REFERENCES Products(ProductID));
error:
Msg 515, Level 16, State 2, Line 1 Cannot insert the value NULL into column 'UserID', table 'MyWebDB.dbo.Users'; column does not allow nulls. INSERT fails. The statement has been terminated.
Explanation / Answer
you are created tables are wrong because primary key means It must contain a unique value for each row of data. It cannot contain null values.Please let me know your comments.
you are using below table creation quries
CREATE TABLE Users
(
UserID INT PRIMARY KEY not null identity (1,1),
EmailAddress VARCHAR(255) NOT NULL,
FirstName VARCHAR(60) NOT NULL,
LastName VARCHAR(60) NOT NULL
);
CREATE TABLE Products (
ProductID INT PRIMARY KEY not null identity (1,1),
ProductName VARCHAR(255) NOT NULL);
CREATE TABLE Downloads
(DownloadID INT PRIMARY KEY not null identity (1,1),
UserID INT FOREIGN KEY REFERENCES Users(UserID),
DownloadDate DATE,
FileName VARCHAR(255) NOT NULL,
ProductID INT FOREIGN KEY REFERENCES Products(ProductID));
Sample insert data quires
insert into Users (EmailAddress,FirstName,LastName) values('na@gmail.com','kondala','naidu')
insert into Products (ProductName) values('Lux')
insert into Downloads(UserID,DownloadDate,FileName,ProductID) values(1,'3-3-2016','test',1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.