I am running and keep getting this error. Any help would be appreciated.
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
I have had other errors with senmcolons, commas and other small mistakes, but this one is eating me alive
-- 1 - Create Faculty Table
create table Faculty (
FacultyID int not null primary key,
FirstName varchar(50) not null,
LastName varchar(50) not null,
Email varchar(50) not null,
Date_of_birth date,
Number_of_courses smallint not null
);
Just use AUTO_INCREMENT for the PRIMARY KEY column name.
Example :
CREATE TABLE dogs ( dog_id NOT NULL PRIMARY KEY AUTO_INCREMENT), (breed VARCHAR(100), age INT);
INSERT INTO dogs ( breed, age ) VALUES ( 'labrador', 4), ('pug', 5);
Just use AUTO_INCREMENT for the PRIMARY KEY column name.
Example:
CREATE TABLE dogs (dog_id NOT NULL PRIMARY KEY AUTO_INCREMENT), (breed VARCHAR(100), age INT);
It looks like you don't have auto-increment for FacultyID and it is inserting 0 for each record and you are getting
Duplicate entry '0' for key 'PRIMARY'
Insert value for FacultyID or use Auto increment (AUTO_INCREMENT) for FacultyID.
create table Faculty (
FacultyID int not null AUTO_INCREMENT primary key,
FirstName varchar(50) not null,
LastName varchar(50) not null,
Email varchar(50) not null,
Date_of_birth date,
Number_of_courses smallint not null
);
Please use this kind of insert:
insert into Faculty (FirstName , LastName, Email, Date_of_birth, Number_of_courses) values ('Name1','Lastname2','xx#gmail.com','2016-12-07',2);
You need to specify which column you are inserting.
Related
I'm new in mysql and don't know why I get an error I'm stuck here get an error in the foreign key one of the questions is
The "DepartmentID" fields in both Employee and Project are foreign keys referencing the "Did" field of Department Table" and when I insert a value in the department I get that error not sure why
``
`create table Department
(
did integer default 1,
Dname varchar(50) default 'HR',
location varchar(50) default 'Chicago',
primary key(did)`
);`
`create table Employee(
Eid integer default 12,
DepartmentID integer default 5,
Ename varchar(50) default 'Josh',
Erank integer default 2,
Salary real default 500000,
primary key(Eid),`
foreign key(DepartmentID) references Department(did)`
);
`create table Project`
(
Pid integer default 20,
DepartmentID integer default 9,
Pname varchar(50) default 'Sorting',
budget real default 5.000,
StartYear integer default 2000,
primary key(Pid),
foreign key(DepartmentID) references Department(did)
);
insert
into Department(Dname)
values ('Marketing'),
('Human Resources');
It is required to specify the primary key value unless it is auto_increment
insert into Department(did, Dname)
values (1, 'Marketing'),
(2, 'Human Resources');
Or alter your table to use auto_increment
create table Department (
did integer not null auto_increment,
Dname varchar(50) default 'HR',
location varchar(50) default 'Chicago',
primary key(did)
);
insert into Department(Dname)
values ('Marketing'),
('Human Resources');
I need to insert some data into the 'ItemBook' table after inserting the following values for one row for the 'Item' Table:
Name='Clippers', itemLink='amazon.com' description='hair clippers'
Now I would also like to insert some data to the ItemBook table as well but I am not sure on how to do this with a table that has a foreign key. Here is the SQL code:
CREATE TABLE Item (
Name VARCHAR(100) NOT NULL,
itemLink VARCHAR(100) NOT NULL,
description VARCHAR(1000) NOT NULL,
PRIMARY KEY (Name)
);
CREATE TABLE ItemBook (
ItemName VARCHAR(100) NOT NULL,
Publisher VARCHAR(100) NOT NULL,
PRIMARY KEY (ItemName),
FOREIGN KEY (ItemName) REFERENCES Item(Name)
);
My attempt:
INSERT INTO itemBook (Name, Publisher)
VALUES ('Clippers', 'Bob');
Error Msg:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
Other Attempt:
INSERT INTO eventbook (EventName, Publisher)
SELECT E.name
FROM event E
WHERE E.name = eventbook.EventName;
Error Message:
Error Code: 1054. Unknown column 'eventbook.EventName' in 'where clause'
Is there any specific reason to not use integer IDs? I would do the following:
CREATE TABLE Item (
Id INTEGER NOT NULL IDENTITY PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
itemLink VARCHAR(100) NOT NULL,
description VARCHAR(1000) NOT NULL,
);
CREATE TABLE ItemBook (
Id INTEGER NOT NULL IDENTITY PRIMARY KEY,
ItemName VARCHAR(100) NOT NULL,
Publisher VARCHAR(100) NOT NULL,
ItemId INTEGER NOT NULL,
FOREIGN KEY (ItemId) REFERENCES Item(Id)
);
INSERT INTO ItemBook Values(1, 'ItemName', 'Publisher', 0)
What are your thoughts?
Edit 1. Based on your response and example, I have produced the following SQL for SQLITE (should work fine in other DBs as well)
CREATE TABLE Item (
Name VARCHAR(100) NOT NULL,
ItemLink VARCHAR(100) NOT NULL,
Description VARCHAR(1000) NOT NULL,
PRIMARY KEY (Name)
);
CREATE TABLE ItemBook (
ItemName VARCHAR(100) NOT NULL,
Publisher VARCHAR(100) NOT NULL,
PRIMARY KEY (ItemName),
FOREIGN KEY (ItemName) REFERENCES Item(Name)
);
INSERT INTO Item (Name, ItemLink, Description) VALUES("Test Book", "http://www.testlink.com/", "This is a test book");
INSERT INTO ItemBook (ItemName, Publisher) Values("Test Book", "Test Publisher");
SELECT * FROM Item i JOIN ItemBook b on b.ItemName = i.Name
Check the result in this print
The task given is:
Create a new relational table to store information about the company names of all suppliers and the total number of products supplied by each supplier. Enforce, the appropriate consistencyconstraints on the new table. Next, copy into the new table information about the company names of all suppliers and the total number of products supplied by each supplier.
I'm recieving an error Duplicate Entry for key "PRIMARY" When i try and run this script
CREATE TABLE COMPANY_AND_SUPPLIERS (
COMPANY_NAME VARCHAR (40) NOT NULL DEFAULT 'EMPTY',
PRODUCT_NAME VARCHAR(40) NOT NULL DEFAULT 'EMPTY' ,
TOTAL_PRODUCTS VARCHAR(40) NOT NULL DEFAULT 'EMPTY',
CONSTRAINT SUPPLIER_PKEY PRIMARY KEY(COMPANY_NAME) ,
CONSTRAINT SUPPLIER_FKEY FOREIGN KEY (COMPANY_NAME) REFERENCES SUPPLIER(COMPANY_NAME)
);
INSERT INTO COMPANY_AND_SUPPLIERS(COMPANY_NAME, PRODUCT_NAME)
SELECT DISTINCT SUPPLIER.COMPANY_NAME, PRODUCT.PRODUCT_NAME
FROM SUPPLIER, PRODUCT;
UPDATE COMPANY_AND_SUPPLIERS
SET TOTAL_PRODUCTS = (SELECT COUNT(*) AS TOTALPRODUCTS
FROM PRODUCT);
The Whole purpose of the exercise is to copy company names of all suppliers and the total number of products supplied by each supplier.
TABLES GIVEN
CREATE TABLE SUPPLIER
(
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30),
CONTACT_TITLE VARCHAR(30),
ADDRESS VARCHAR(60),
CITY VARCHAR(15),
REGION VARCHAR(15),
POSTAL_CODE VARCHAR(10),
COUNTRY VARCHAR(15),
PHONE VARCHAR(24),
FAX VARCHAR(24),
HOME_PAGE VARCHAR(500),
CONSTRAINT PK_SUPPLIER PRIMARY KEY (COMPANY_NAME)
);
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20),
UNIT_PRICE DECIMAL(10,2) NOT NULL DEFAULT 0,
UNITS_IN_STOCK DECIMAL(9) NOT NULL DEFAULT 0,
UNITS_ON_ORDER DECIMAL(9) NOT NULL DEFAULT 0,
REORDER_LEVEL DECIMAL(9) NOT NULL DEFAULT 0,
DISCONTINUED CHAR(1) NOT NULL DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
The column company_name should not be a primary key because a primary key is a unique value.
Take this as an example
If a Database administrator creates a table with first_name as the primary key, it would be a disaster since there are a lot of people who has John as their first name
That is why most of the time, the primary key is a integer, then we make it unique using this method.
Your table isn't supposed to contain the product names, just the company and total products. The PRODUCTS table already has the supplier names in it for each product. So you just need to count the number of products for each supplier from that table.
CREATE TABLE Company_Totals (
Company_name VARCHAR(40) NOT NULL,
Total_Products INT(11) NOT NULL,
PRIMARY KEY (Company_name),
FOREIGN KEY (Company_name) REFERENCES Supplier(Company_name)
);
INSERT INTO Company_Totals (Company_name, Total_Products)
SELECT SUPPLIER_NAME, COUNT(*)
FROM PRODUCT
GROUP BY SUPPLIER_NAME;
I am getting this error message ...
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'CONSTRAINT'.
I've done my research and I'm still stuck on completing this script.
Here`s what I entered.
DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null);
CREATE TABLE CLASS (
title CHAR(40) not null PRIMARY KEY,
instructor CHAR(40) not null );
CREATE TABLE SEMESTER (
year int not null );
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
CONSTRAINT pk_class PRIMARY KEY (title, instructor),
CONSTRAINT pk_semester PRIMARY KEY (semid, year));
I think those CONSTRAINT statements need to be inside the CREATE TABLE statements. For example, the STUDENT table should look like this:
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null,
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
);
Otherwise, how could the RDBMS know to which table each constraint actually belongs?
DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),);
CREATE TABLE CLASS (
title CHAR(40) not null ,
instructor CHAR(40) not null ,
CONSTRAINT pk_class PRIMARY KEY (title, instructor));
CREATE TABLE SEMESTER (
year int not null ,
semid int not null
CONSTRAINT pk_semester PRIMARY KEY (semid, year));;
AS per my understanding
Here constraint must be create inside the table for student
and
second table u are creating 2 times primary key on class(title)
and
coming to third table semid itself not present in semester table
I looked through other questions on this topic, and the general consensus was to check my data type consistency. I am trying to create the table 'reviews' that has SSN and ProjectID as foreign keys, referencing SSN from Table Reviewer and ProjectID from Table Project
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);
The following are the create table statements for 'Project' and 'Reviewer'
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
Project and Reviewer created without issue. My create Reviews query works if I remove the FK declaration for SSN and leave the FK declaration for ProjectID, but not the other way around.
What is the reason for the error? Is there another constraint I need to add?
You forgot to add a primary key to the SSN column in the Reviewer table. The query below builds correctly in sqlfiddle:
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
PRIMARY KEY(SSN)
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);
A foreign key can only reference to a Primary Key column in another table, you need to make SNN a primary key column.
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL primary key CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
The referenced column for a foreign key column, needs to be Primary key OR have a unique constraint so your Reviewer table need to be:
CREATE TABLE Reviewer(
SSN INTEGER PRIMARY KEY CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
or
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
UNIQUE(SSN)
);
Also as far as I know, MySQL Ignores Check Constraints, so you can remove them.