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.
Related
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
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 am unable to create these tables in MySQL. Everything looks complete to me but I keep getting errors.
Here is my SQL:
CREATE TABLE Movies(
title char PRIMARY KEY NOT NULL,
Years date NOT NULL,
length decimal not null,
genre char NOT NULL,
academy_award char not null,
FOREIGN KEY(the_name) REFERENCES Studio,
FOREIGN KEY(directorName) REFERENCES Director);
CREATE TABLE StarsIn(
FOREIGN KEY(title) REFERENCES Movies,
FOREIGN KEY(Years) REFERENCES Movies,
FOREIGN KEY(the_name) REFERENCES MovieStar);
CREATE TABLE Director(
the_name char PRIMARY KEY NOT NULL,
birthdate date NOT NULL,
nationality char NOT NULL);
CREATE TABLE MovieStar(
the_name char PRIMARY KEY NOT NULL,
birthdate date NOT NULL,
address char NOT NULL,
gender char NOT NULL);
CREATE TABLE Studio(
the_name char PRIMARY KEY NOT NULL,
address char NOT NULL);
This isn't working because your tables are not being created in the proper order.
The parent table must always be created first, because otherwise you will be trying to reference a column that does not exist.
For example, look at this part here:
CREATE TABLE Movies(title char PRIMARY KEY NOT NULL, Years date NOT NULL,
length decimal not null, genre char NOT NULL, academy_award char not null,
FOREIGN KEY(the_name) REFERENCES Studio, FOREIGN KEY(directorName) REFERENCES Director);
You can't reference tables studio or director because they don't even exist.
The proper order could be:
Studio
Movie Star
Director
Movies
StarsIn
In addition, you have a lot of other problems going on here.
You do not define any columns in the Stars IN table, you only declare foreign keys:
CREATE TABLE StarsIn(
FOREIGN KEY(title) REFERENCES Movies,
FOREIGN KEY(Years) REFERENCES Movies,
FOREIGN KEY(the_name) REFERENCES MovieStar);
Not only are you not defining any columns, you aren't referencing anything. This will also throw an error. Your foreign key title must reference some column in the movies table. And in addition to that, you can't create a foreign key reference for Years. Mostly because you cannot create a foreign key to a column that is not primary key in another table, and also why should you need two foreign keys to the same table?
Those problems exist in other create table statements as well, so given everything I've said please go back and look at each of your create table statements.
Here is a working SQL Fiddle you can use for reference though.
EDIT
I would like to also add that I do not recommend using the char datatype. Please look into using VARCHAR().
The problem you are having is that you are referencing columns in tables that do not exist.
Although you create these tables later, the DBMS engine does not know they will exist.
The solution - create the tables without the FK's that do not exist, and add these later using the "ALTER TABLE" command.
It seems like you know the syntax, so I'll let you inform me if you cannot find it.
your syntax should look like (All I did is re-order, and change char to be archer...):
CREATE TABLE Studio(the_name varchar(50) PRIMARY KEY NOT NULL, address varchar(50) NOT NULL);
CREATE TABLE Director(the_name varchar(50) PRIMARY KEY NOT NULL, birthdate date NOT NULL, nationality varchar(50) NOT NULL);
CREATE TABLE Movies(title varchar(50) PRIMARY KEY NOT NULL, Years date NOT NULL,
length decimal not null, genre varchar(50) NOT NULL, academy_award varchar(50) not null,
the_name REFERENCES Studio(the_name), directorname REFERENCES Director(the_name));
CREATE TABLE MovieStar(the_name char PRIMARY KEY NOT NULL, birthdate date NOT NULL, address char NOT NULL, gender char NOT NULL);
CREATE TABLE StarsIn(title REFERENCES Movies(title),movie REFERENCES Movies(title), moviestar REFERENCES MovieStar(the_name));
A little note, depends on the version - you'd might have to use the FOREIGN KEY(col_name) instead of col_name. I like more the syntax without it, but some versions force you to use FOREIGN KEY(title) instead of title in the last SQL for example.
The syntax you used - Foreign key NAME References TABLE (Column) -- you forgot the column-- Allows you to name the FK. If you don't use the "Foreign Key" the system will randomly assign a name. This is usually not important, unless you want the DB design to be "clean" and to be able to reference it by name.
P.S. - I did not run it, I trust you to inform if there's any other issue - I did not check the syntax, just fixed the error you reported- references that do not exist...
P.S. P.S. Always check syntax... http://dev.mysql.com/doc/refman/5.1/en/create-table.html
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 working on mySQL and have installed WAMP-server and mySQL workbench.
I know mySQL is a standard, but with various dialects. My question is, how can I know when to use which code?
Sometimes when creating a table, you have to write test and sometimes 'test'.
Some examples:
CREATE TABLE test (id INT NOT NULL PRIMARY KEY,name VARCHAR(15) NOT NULL, no INT FOREIGN KEY REFERENCES Persons(no);
CREATE TABLE 'test'('id' NOT NULL PRIMARY KEY, 'name' VARCHAR(15) NOT NULL, 'no' INT FOREIGN KEY REFERENCES Persons(no));
CREATE TABLE test(id INT NOT NULL, name VARCHAR(15) NOT NULL, no INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY(no) REFERENCES Persons(no));
Some places you need to end the command with ; and some places not...
Same with ' .
And some places you write the PRIMARY KEY at the back of the decleration and some places you enter it at the end.
WAMP server DOS, WAMP server phpmyadmin and mySQL workbench is all different.
THanks in advance for the answer:)
For table and columns names use backticks `tableName`,but they are necessary only when using reserved names.Reserved names in Mysql
Quotes are used for strings '123'-this is a string
Semicolon indicates that the statement ends,so it is necessary when you have multiple statements.
Primary key you can add it on the column definition,or add it later.