I am new to creating/testing/working with MySQL.
In this I have created a database and have used the command to USE the Database, however when trying to create a table, Error Code:1064 keeps coming up.
I have no previous tables in this database, this would be the first table.
I am unsure where the error is and would greatly appreciate it if someone could help me identiy the error and the reason why?
CREATE TABLE Customers(
customerNumber INT NOT NULL,
firstName VARCHAR(60),
lastName VARCHAR(60),
address VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
state ENUM('QLD','VIC','NSW','WA','TAS','NT','SA') NOT NULL,
postCode INT(4) NOT NULL,
region VARCHAR(60),
email VARCHAR(254),
PRIMARY KEY(customerNumber),
FOREIGN KEY(customerNumber)
);
A foreign key must reference another table.
The other table must exist before you can reference it in a foreign key.
I contributed to a checklist for foreign keys in this post:
https://stackoverflow.com/a/4673775/20860
Related
I used a script to make a tables within the same database. Once both tables were made, I made a seperate script to insert a set of values in the table, however with one of the scripts it doesnt seem to insert any data into the table, but comes up with the error.
These are my tables;
CREATE TABLE IF NOT EXISTS customers(
customer_id INT UNSIGNED NOT NULL,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password VARCHAR(40) NOT NULL,
dob DATETIME NOT NULL,
address_line VARCHAR(40) NOT NULL,
postcode VARCHAR(20) NOT NULL,
PRIMARY KEY(customer_id),
FOREIGN KEY(postcode) REFERENCE postcodes(postcode)
);
In a seperate script,
CREATE TABLE IF NOT EXISTS postcodes(
postcode VARCHAR(20) NOT NULL,
address_line_2 VARCHAR(20),
city VARCHAR(40) NOT NULL,
PRIMARY KEY(postcode)
);
The scripts to insert data into the tables are here.
This one works without any errors and successfully populates the table.
INSERT INTO postcodes(postcode,address_line_2,city)
Values
('DH1 568','Forest Lane','Durham'),
('DH1 679','Dry Wood','Durham'),
('DH1 4AS','North Of the Wall','Westeros'),
('DH1 4LA',"Snoop's Crib",'Durham');
And this is the one which comes up with an error message,
INSERT INTO customers(customer_id,first_name,postcode)
values
('1','Zaak','DH1 568'),
('2','Matt','DH1 679'),
('3','Jon','DH1 4AS'),
('4','Zak','DH1 4LA'),
('5','Gaz','DH1 7SO');
The error message which appears is,
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`hardware_store`.`customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`postcode`) REFER
ENCES `postcodes` (`postcode`))
You have a foreign key constraint specifying that customers(postcode) refers to a valid postal code in the postcodes table.
Then, you try to insert 'DH1 7SO' and it doesn't work because this postcode is not in postcodes.
This is how foreign key references work. The database is working exactly as it should and doing exactly what you instructed it to do.
If you want the valid rows to be inserted and the invalid ones ignored, then use the IGNORE option on INSERT (see here).
I've got a problem with an RIM I have to write from scratch for school.
The thing is, any syntax checker I use doesn't give me any helpul clues.
create database FullHouseGr1;
use FullHouseGr1;
create table Player
(player_id int not null,
first_name varchar(20) not null,
surname varchar(20) not null,
addres varchar(40) not null,
postal_code varchar(6) not null,
place varchar(40) not null,
phone_number varchar(20) not null,
email_addres varchar(255) not null,
points int not null,
primary key(player_id));
the error is near create table Player at line 2.
edit: It is working in MYSQL workbench, but not in online syntax checkers.
snip -
After creating the database, and before creating any tables, you need to tell MySQL where you want those tables created.
USE FullHouseGr1;
Second, int() is not a valid column type. You can just use INT (or INT(10), or whatever number you need).
Finally, your ON DELETE SET NULL and ON UPDATE cascade statements. They cannot be on their own, they are part of FOREIGN KEY definitions.
FOREIGN KEY(date_time) REFERENCES Event ON DELETE no action ON UPDATE cascade
Notice how that's all one line (without commas). In a CREATE TABLE command, you separate definitions with commas. ON DELETE SET NULL must be part of a FOREIGN KEY definitions, it's not its own definition.
I have installed MySQL Workbench 6.0 and am trying to import a .sql file. The file should create a number of tables, insert data into each of these and commit these changes. It's a sample file for a course I'm doing and I'm going to use it to practice writing queries. Nothing fancy.
I want to create a schema in Workbench, run this script and end up with a database containing these tables and all the data, however I'm having trouble doing so.
I've tried creating a new schema and using the Forward Engineer... option and modifying the script myself, but I keep getting an error message that already exists.
I'm a complete beginner with Workbench so I wouldn't be surprised if I'm going about this all wrong.
Can anyone advise?
EDIT:
I'm now getting the error 1215: Cannot add foreign key constraint
create table Debiteur (
debiteurcode char(6) not null,
naam varchar(30) not null,
adres varchar(40) not null,
postcode varchar(7) not null,
plaats varchar(30) not null,
land varchar(30) not null,
telefoon varchar(12) not null,
fax varchar(12),
korting numeric(6,3),
primary key (debiteurcode)
);
create table Factuur (
factuurnummer integer not null,
debiteurcode char(6) not null,
besteldatum date not null,
leverdatum date,
factuurdatum date,
bedrag numeric(8,2) not null,
transportkosten numeric(8,2),
betaaldatum date,
primary key (factuurnummer),
foreign key (debiteurcode) references Debiteur
);
The problem seems to be in the second table 'Factuur'.
Your foreign key definition is incomplete. See the online documentation for "Using Foreign Key Constraints" for more details. You have no target column set.
I'm new to both this site and databases. I am creating a database for a point of sale for a company. Im having problems since I have so many composite primary keys okay so here is my question... I have the table member:
DROP TABLE IF EXISTS member;
CREATE TABLE member (
fName varchar(10) NOT NULL,
lname varchar(10) NOT NULL,
shippingAddress varchar(50) NOT NULL,
billingAddress varchar(50) NOT NULL,
memberNo varchar(15) NOT NULL,
memStartDate date NOT NULL,
memOf varchar(15) ,
PRIMARY KEY(memberNo, shippingAddress)
);
and table shipping:
DROP TABLE IF EXISTS shipping;
CREATE TABLE shipping (
company varchar(10) NOT NULL,
warehouseAddress varchar(50) NOT NULL,
memberAddress varchar(50) NOT NULL,
memberNum varchar(15) NOT NULL,
orderNumber varchar(20) NOT NULL,
PRIMARY KEY(orderNumber)
);
ALTER TABLE shipping ADD CONSTRAINT shipping_memberAddnNUM_refs_member_memberNoNshippingAddress FOREIGN KEY (memberAddress, memberNum)
REFERENCES member (shippingAddress, memberNo);
So it gives me an errno: 150
I dont necessarily need member(shippingAddress) to be part of the primary key. But i do need to reference it my shipping table. Does anyone know how to get past this I have been looking at examples online but none of them seem to fix it.
I do it like this:
CONSTRAINT NAME_OF_PK PRIMARY KEY(Field1, Field2)
But this is Microsoft's SQL way of doing things.
You have the two columns in reverse order - from how they appear in the PRIMARY KEY of member. Try this:
ALTER TABLE shipping
ADD CONSTRAINT shipping_memberAddnNUM_refs_member_memberNoNshippingAddress
FOREIGN KEY (memberNum, memberAddress)
REFERENCES member (memberNo, shippingAddress);
When using jdbcrealm in Glassfish v3 how strictly must I follow the recommendations regarding tables? Currently I have the following setup:
CREATE TABLE roles (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
rolename VARCHAR(255) NOT NULL,
);
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(255) PRIMARY KEY NOT NULL,
password VARCHAR(255) NOT NULL,
firstname VARCHAR(255),
lastname VARCHAR(255),
email VARCHAR(255),
status VARCHAR(255),
role_id INTEGER,
CONSTRAINT FOREIGN KEY(role_id) REFERENCES roles(id)
);
Is it possible to use this setup without changing anything to create a jdbcrealm or must I change my tables?
Thanks in advance!
Have you tried it? It seems ok. The strange thing with the jdbcRealm is that it expects an unnormalized database, one would like something more like:
user (userid, username, passw, ...)
security_group (security_groupid, name)
user_in_group (user_in_groupid, userid, security_groupid)
Wich is more normalized. However this setup does not work. But if you're like me and think this should work take a look at the lovely custom Flexible JDBC Realm. It worked for me.