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');
is it possible to make two tables have a unique key like if my first table has the unique key and the second table cannot have the same text as the first table, it is possible to make that?
--
-- Table structure for table admin
DROP TABLE IF EXISTS admin;
CREATE TABLE IF NOT EXISTS admin (
ID varchar(11) NOT NULL,
Name varchar(100) NOT NULL,
Password varchar(100) NOT NULL,
Email varchar(100) NOT NULL,
PhoneNumber varchar(255) NOT NULL,
UniqueCode varchar(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY UniqueCode (UniqueCode)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table admin
INSERT INTO admin (ID, Name, Password, Email, PhoneNumber, UniqueCode) VALUES
('AA11', 'Admin Low', '827ccb0eea8a706c4c34a16891f84e7b', 'AA11#gmail.com', '6012346778', 'Lmao'),
('AA12', 'Admin Tyler', '827ccb0eea8a706c4c34a16891f84e7b', 'AA11#gmail.com', '6033556778', 'Rofl');
--
-- Table structure for table lecturer
DROP TABLE IF EXISTS lecturer;
CREATE TABLE IF NOT EXISTS lecturer (
ID varchar(11) NOT NULL,
Name varchar(100) NOT NULL,
Password varchar(100) NOT NULL,
Email varchar(100) NOT NULL,
PhoneNumber varchar(255) NOT NULL,
UniqueCode varchar(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY UniqueCode (UniqueCode)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table lecturer
INSERT INTO lecturer (ID, Name, Password, Email, PhoneNumber, UniqueCode) VALUES
('AL11', 'Cat Eat my son', '827ccb0eea8a706c4c34a16891f84e7b', 'AL11#gmail.com', '6012342222', 'Meow'),
('AL12', 'Dog Eat my son', '827ccb0eea8a706c4c34a16891f84e7b', 'AL12#gmail.com', '6033345678', 'Woof');
You can simply check before inserting data by useng "EXIST".
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
CREATE TABLE User (
id int NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
email VARCHAR(40) NOT NULL,
image_file VARCHAR(40),
password VARCHAR(40) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Post (
id int NOT NULL AUTO_INCREMENT,
date_posted DATE NOT NULL,
content VARCHAR(10000) NOT NULL,
user_id INT,
like_count INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Post_comment (
id int NOT NULL AUTO_INCREMENT,
date_commented DATE NOT NULL,
content VARCHAR(10000) NOT NULL,
user_id INT,
like_count INT NOT NULL,
post_id INT,
PRIMARY KEY (id)
);
CREATE TABLE post_liked_by (
user_id int NOT NULL,
post_id int NOT NULL,
PRIMARY KEY (user_id,post_id)
);
CREATE TABLE comment_liked_by (
user_id int NOT NULL,
comment_id int NOT NULL,
PRIMARY KEY (user_id,comment_id)
);
ALTER TABLE User AUTO_INCREMENT=1;
ALTER TABLE Post
ADD FOREIGN KEY(user_id)
REFERENCES User(id)
ON DELETE SET NULL;
ALTER TABLE Post_comment
ADD FOREIGN KEY(user_id)
REFERENCES User(id)
ON DELETE SET NULL;
ALTER TABLE Post_comment
ADD FOREIGN KEY(post_id)
REFERENCES User(id)
ON DELETE SET NULL;
INSERT INTO User (username, email, password) VALUES ('egname', 'eg#eg.com', 'egpassword')
This code gives the error ER_NO_DEFAULT_FOR_FIELD: Field 'id' doesn't have a default value when I try to insert into the User table but I am not really sure why. I thought by setting AUTO_INCREMENT for the id field the default value would be 1? Could it be a problem with the configuration of my code editor? I am using PopSQL btw.
It works fine when I specify a value for id but I need it to increment automatically each time I insert a new data.
For context I am trying to build a simple project something like a clone of facebook. Tqvm in advance.
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.
I want 'ProjectID' to start at 1000 and increment by 100.
How do I set an AUTO_INCREMENT default value and rate in MySQL 5.6? In other words, if I want to create a primary key that starts at 1000 and increases by 100, how do I do that in MySQL?
CREATE TABLE IF NOT EXISTS PROJECT(
ProjectID Int(4) AUTO_INCREMENT PRIMARY KEY,
ProjectName Char(20) NOT NULL,
DepartmentName Char(30) NOT NULL,
MaxHours Int(14) NOT NULL,
StartDate Char(10) NOT NULL,
EndDate Char(10) NULL)
ENGINE=InnoDB AUTO_INCREMENT=1000;
I want 'ProjectID' to start at 1000 and increment by 100.
Start with empty table.
ALTER TABLE tblName AUTO_INCREMENT = 1000;
Perform your insert to this special 100 gap table in one and only one place.
Let's call that place a stored proc (not mandatory).
Commit to the discipline of that approach.
In that stored proc lock the table, do an insert.
DECLARE a query as a "string" and then execute that string via a Prepared Statement. That string is like the above Alter Table but with auto_increment=xxxx
Where xxxx=max(ProjectID)+100
Unlock table. Exit stored proc.
The reason is that Alter Table tblName auto_increment = variable_name will barf. So it needs to be an executed Prepared Statement.
Edit as promised:
drop schema wpc;
CREATE SCHEMA IF NOT EXISTS WPC;
use wpc;
CREATE TABLE IF NOT EXISTS department
(
Department varchar(30) NOT NULL, -- chg
BudgetCode int(20) NOT NULL,
OfficeNumber int(10) NOT NULL,
Phone varchar(12) DEFAULT NULL, -- chg
PRIMARY KEY (Department)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS employee
(
EmployeeNumber int(4) AUTO_INCREMENT,
FirstName varchar(25) NOT NULL, -- chg
LastName varchar(25) NOT NULL, -- chg
Department varchar(30) NOT NULL DEFAULT 'Human Resources',
Phone varchar(17) DEFAULT NULL, -- chg
Email varchar(100) NOT NULL,
PRIMARY KEY (EmployeeNumber),
UNIQUE KEY Email (Email),
KEY DepartmentFK (Department),
CONSTRAINT DepartmentFK
FOREIGN KEY (Department)
REFERENCES department (Department) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS project
(
ProjectID INT(4) NOT NULL AUTO_INCREMENT,
ProjectName VARCHAR(30) NOT NULL, -- chg
Department VARCHAR(30) NOT NULL, -- chg
MaxHours INT(3) NOT NULL DEFAULT '100',
StartDate datetime NOT NULL, -- chg
EndDate datetime DEFAULT NULL, -- chg
PRIMARY KEY (ProjectID),
KEY ProjectFK (Department),
CONSTRAINT ProjectFK
FOREIGN KEY (Department)
REFERENCES department (Department) ON UPDATE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=LATIN1 AUTO_INCREMENT=1000;
CREATE TABLE IF NOT EXISTS assignment
(
ProjectID INT(4) NOT NULL AUTO_INCREMENT,
EmployeeNumber INT(4) NOT NULL,
HoursWorked INT(4) NOT NULL,
PRIMARY KEY (ProjectID),
-- UNIQUE KEY EmployeeNumber (EmployeeNumber), -- kill this dupe, plus it won't be unique
-- KEY ProjectFK1 (ProjectID), -- don't have this it is already a PK
KEY EmployeeFK1 (EmployeeNumber), -- keep this as it won't be unique
-- duplicate and unnecessary keys just slow down system. you had 4. you need 2
CONSTRAINT EmployeeFK1
FOREIGN KEY (EmployeeNumber)
REFERENCES employee (EmployeeNumber),
CONSTRAINT ProjectFK1
FOREIGN KEY (ProjectID)
REFERENCES project (ProjectID) ON DELETE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=LATIN1;
show table status like '%'; -- auto_incs look good
insert project (ProjectName,Department,MaxHours,StartDate,EndDate) values ('Dismantle Kryptonite','Engineering',1000,'2015-04-01',null);
-- fk error, good, do dept first. BUT NOTE this failure screws up auto_inc so next insert is 1001 yikes
-- so re-do, drop schema, create schema, skip failed first insert above and start below:
insert department (Department,BudgetCode,OfficeNumber,Phone) values ('Engineering',111,222,null);
insert department (Department,BudgetCode,OfficeNumber,Phone) values ('Human Resources',107,223,null);
select * from department;
-- all looks well
insert project (ProjectName,Department,MaxHours,StartDate,EndDate) values ('Dismantle Kryptonite','Engineering',1000,'2015-04-01',null);
select * from project; -- projectId 1000
alter table project auto_increment=1010;
insert project (ProjectName,Department,MaxHours,StartDate,EndDate) values ('Fire old employees','Human Resources',2,'2015-04-02',null);
alter table project auto_increment=1020;
insert project (ProjectName,Department,MaxHours,StartDate,EndDate) values ('Regret, Hire back','Human Resources',2,'2015-04-02',null);
alter table project auto_increment=1030;
select * from project;
fk's look good and as expected. Try employee:
insert employee (EmployeeNumber,FirstName,LastName,Department,Phone,Email) values (222,'Donald','','bad-dept','1','d#g.com');
insert employee (EmployeeNumber,FirstName,LastName,Department,Phone,Email) values (222,'Donald','','Engineering','1','d#g.com');
insert employee (EmployeeNumber,FirstName,LastName,Phone,Email) values (223,'Kris','','2','k#g.com');
insert employee (EmployeeNumber,FirstName,LastName,Phone,Email) values (2277,'Kim','','3','kim#g.com');
select * from employee;
insert employee (FirstName,LastName,Phone,Email) values ('Auto','','44','auto1#g.com'); -- 2278
do a re-do of everything top to bottom but skipping employee inserts except the below to run:
insert employee (FirstName,LastName,Department,Phone,Email) values ('Donald','','Engineering','1','d#g.com');
insert employee (FirstName,LastName,Phone,Email) values ('Kris','','2','k#g.com');
insert employee (FirstName,LastName,Phone,Email) values ('Kim','','3','kim#g.com');
select * from employee;
insert project (ProjectName,Department,MaxHours,StartDate,EndDate) values ('Hire Joe','Human Resources',2,'2015-05-02',null);
alter table project auto_increment=1040;
insert employee (FirstName,LastName,Phone,Email) values ('Jason','','66','jj#g.com');
select * from employee;
select * from project;
Create table:
CREATE TABLE IF NOT EXISTS PROJECT(
ProjectID Int(4) AUTO_INCREMENT PRIMARY KEY,
ProjectName Char(20) NOT NULL,
DepartmentName Char(30) NOT NULL,
MaxHours Int(14) NOT NULL,
StartDate Char(10) NOT NULL,
EndDate Char(10) NULL)
ENGINE=InnoDB AUTO_INCREMENT=1000;
Then type:
SET ##AUTO_INCREMENT_INCREMENT=100;
Now ProjectID is a surrogate key that starts at 1000 and increases by 100.
https://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_offset
Thank you #NorbertvanNobelen !