MySQL Error code 1452 occurs but values and tables do exist - mysql

I cannot for the life of me figure out where did I mess up. I Have the following tables, made using this SQL code:
CREATE TABLE IF NOT EXISTS Users(
username VARCHAR(50) NOT NULL PRIMARY KEY,
pwd TEXT NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
user_type VARCHAR(25) NOT NULL,
user_status VARCHAR(10) NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE UNIQUE INDEX Users_Index
ON Users (username);
CREATE TABLE IF NOT EXISTS Clients(
client_id VARCHAR(10) PRIMARY KEY,
client_name VARCHAR(255) NOT NULL UNIQUE,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
middle_name VARCHAR(25),
related_to VARCHAR(10),
relation_type VARCHAR(25),
ssn_ein VARCHAR(11) NOT NULL UNIQUE,
date_of_birth DATE NOT NULL,
manager VARCHAR(50) NOT NULL,
entity_type VARCHAR(50) NOT NULL,
client_status VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
state VARCHAR(2) NOT NULL,
zip INT NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
notes TEXT,
FOREIGN KEY(manager) REFERENCES Users(username)
);
CREATE UNIQUE INDEX username_index
ON Clients(manager);
I tried to insert data into the Clients table but it gave me error 1452, which is for when a constraint check has failed. Thing is, I already have data in the Users table, I TRIPLE checked for typos, I added the indexes to both tables to no avail, I checked that the reference was pointing to the right table, I checked to see that the engines were both InnoDB as well as the collations being exact, and both username and manager columns are the same exact datatype. I have checked the MySQL documentation and followed all requisites to the best of my knowledge. Does anyone know what could be the problem?
Here is the sample data:
INSERT INTO Clients VALUES (
client_id = "123456",
client_name = "John Doe",
first_name = "John",
last_name = "Doe",
middle_name = "Michael",
related_to = NULL,
relation_type = NULL,
ssn_ein = "123-456-7890",
date_of_birth = "1990-01-01",
manager = "OzzyTheGiant",
entity_type = "Individual",
client_status = "Active",
address = "123 Main St.",
city = "Anytown",
state = "XX",
zip = 78550,
email = "john.doe#example.com",
phone = "123-555-7890",
notes = "self-employed"
);

That's cause the row/record you are trying to insert into Clients table doesn't exists in Users table and thus the error. Since the record doesn't exists in parent table itself, how can you expect to insert in child table? that's not possible.
Also, your INSERT query syntax wrong. Your schema creation script should look like below. See a demo fiddle here: Demo Here
CREATE TABLE IF NOT EXISTS Users(
username VARCHAR(50) NOT NULL PRIMARY KEY,
pwd TEXT NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
user_type VARCHAR(25) NOT NULL,
user_status VARCHAR(10) NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE UNIQUE INDEX Users_Index
ON Users (username);
CREATE TABLE IF NOT EXISTS Clients(
client_id VARCHAR(10) PRIMARY KEY,
client_name VARCHAR(255) NOT NULL UNIQUE,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
middle_name VARCHAR(25),
related_to VARCHAR(10),
relation_type VARCHAR(25),
ssn_ein VARCHAR(11) NOT NULL UNIQUE,
date_of_birth DATE NOT NULL,
manager VARCHAR(50) NOT NULL,
entity_type VARCHAR(50) NOT NULL,
client_status VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
state VARCHAR(2) NOT NULL,
zip INT NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
notes TEXT,
FOREIGN KEY(manager) REFERENCES Users(username)
);
CREATE UNIQUE INDEX username_index
ON Clients(manager);
INSERT INTO `Users` VALUES('OzzyTheGiant', 'scxcxx','test1','test2','test','test','test');
INSERT INTO Clients VALUES (
'123456',
'John Doe',
'John',
'Doe',
'Michael',
NULL,
NULL,
'123-456-78',
'1990-01-01',
'OzzyTheGiant',
'Individual',
'Active',
'123 Main St.',
'Anytown',
'XX',
78550,
'john.doe#example.com',
'123-555-7890',
'self-employed'
);

Related

Creating a column in MySql with id such as CHV18000002

i could do this in Microsoft sql to create a column incrementing such as CHV180000001, CHV180000002 but trying to do that in MySql. I have tried but getting error: incorrect syntax. Any guide to achieve this: This is my code:
CREATE TABLE Candidates (ID INT AUTO_INCREMENT NOT NULL Primary Key,
[ApplicationID] AS ('CHV18'+right('000000'+CONVERT([varchar](6),[ID]),(6))),
[FirstName] [varchar](100) NOT NULL,
[MiddleName] [varchar](100) NOT NULL,
[LastName] [varchar](100) NOT NULL,
[DateOfBirth] [date] NOT NULL,
[Gender] [nchar](1) NOT NULL
Try this to create the table
CREATE TABLE Candidates (ID INT(11) AUTO_INCREMENT NOT NULL Primary Key,
ApplicationID varchar(6),
FirstName varchar(100) NOT NULL,
MiddleName varchar(100) NOT NULL,
LastName varchar(100) NOT NULL,
DateOfBirth date NOT NULL,
Gender varchar(1) NOT NULL);

Creating tables with foreign keys

I am creating tables where I have foreign keys. This is part of the statements. For some reason I cannot get it to work.
What am i doing wrong?
CREATE TABLE Doctor (
NPI NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN NUMBER PRIMARY KEY NOT NULL,
INSURANCE_POLICY_ID NUMBER NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));
You have tagged your question with SQL Server and MySql both, in both of these RDBMS there is no data type called Number but there is a data type to store numbers it is called INT or INTEGER.
Therefore your table definitions should be as follow:
CREATE TABLE Doctor (
NPI INT NOT NULL PRIMARY KEY ,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE INT NOT NULL, --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN INT NOT NULL PRIMARY KEY,
INSURANCE_POLICY_ID INT NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE INT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID)
REFERENCES INSURANCE (INSURANCE_POLICY_ID));

Foreign Keys in MySQL Workbench not working?

I am reverse engineering a database in MySQL Workbench but it doesn't seem to be importing or recognising the foreign keys. Which means I cant get it to draw the relationships.
While I am trying to get this to work I am using a snippet of a couple of tables, so its nothing complex.
Here are the 2 demo tables that I am trying to get to work:
CREATE TABLE users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID ),
CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID)
);
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
)
But in MySQL Workbench, there are no relationships or foreign keys being created. If I look at the table data, and the foreign keys tab, theres nothing there?
I think I'm creating the foreign keys correctly, so is there a reason its not importing them?
On further research there were several things that I needed to do for this to work.
I had to make sure that the tables reverse engineered from MySQL were defined as InnoDB.
And I also had to remove the foreign key constraint from the create table command and create the foreign keys after the tables were created, like so:
CREATE TABLE Users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID )
) ENGINE=InnoDB;
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
) ENGINE=InnoDB;
ALTER TABLE Users ADD CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID);
This then worked fine and the relationships were visible and connected in MySQL Workbench

Account Number Auto generation

I new to programming. How do I apply auto generation to the accountNo in accounts table. I tried everything. But I don't know how to get this done. Can anyone explain me.
CREATE TABLE accounts(
accountNo int(100) NOT NULL, // I need this to be auto generated.
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
Try this one.
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate VARCHAR(20) NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
ALTER TABLE accounts AUTO_INCREMENT = 1001;
When you are adding new data to your accounts table, accountNo will be auto generated.
Make it as an AUTO_INCREMENT:
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
For further informations have a look at this: https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
declear it AUTO_INCREMENT and you can define PRIMARY KEY like that also. Read manual auto_increment
accountNo INT NOT NULL AUTO_INCREMENT PRIMARY KEY

MySQL Not Null commands aren't doing anything

I'm just starting SQL and I'm trying to change a tables values from NULL to NOT NULL, except the command prompt is showing that it isn't being changed.
Here's the code I'm putting in:
CREATE TABLE my_contacts
(
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
gender CHAR(1) NOT NULL,
birthday DATE NOT NULL,
profession VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
interests VARCHAR(100) NOT NULL,
seeking VARCHAR(100) NOT NULL
);
I can't post images but when I use DESC my_contacts; the table prints out and shows "Yes" under the null column for all rows.
This is MySQL 5.6
So as you mentioned in your comments. Do as follow:
But remeber one thing. If you create the table and have inserted some data on it and left any of the columns that you want to change to not null you will have to delete the data or put some data on the field that is null.
alter table my_contacts modify last_name VARCHAR(30) NOT NULL;
alter table my_contacts modify first_name VARCHAR(20) NOT NULL;
alter table my_contacts modify email VARCHAR(50) NOT NULL;
alter table my_contacts modify gender CHAR(1) NOT NULL;
alter table my_contacts modify birthday DATE NOT NULL;
alter table my_contacts modify profession VARCHAR(50) NOT NULL;
alter table my_contacts modify location VARCHAR(50) NOT NULL;
alter table my_contacts modify status VARCHAR(20) NOT NULL;
alter table my_contacts modify interests VARCHAR(100) NOT NULL;
alter table my_contacts modify seeking VARCHAR(100) NOT NULl;
Or if it suits better:
drop table my_contacts;
CREATE TABLE my_contacts
(
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
gender CHAR(1) NOT NULL,
birthday DATE NOT NULL,
profession VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
interests VARCHAR(100) NOT NULL,
seeking VARCHAR(100) NOT NULL
);