How can you know which mySQL code to use in which program? - mysql

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.

Related

I keep getting this mysql error code #1089

CREATE TABLE `movies`.`movie`
( `movie_id` INT(3) NULL AUTO_INCREMENT, `movie_name` VARCHAR(25) NULL,
`movie_embedded_id` VARCHAR(50) NULL, `rating_no` INT(3) NULL,
`movie_description` VARCHAR(50) NULL, PRIMARY KEY (`movie_id`(3))) ENGINE = InnoDB;
I keep getting this error:
#1089 - Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't
support unique prefix keys.
but I've got no idea what it means, anyone have a clue?
With the part
PRIMARY KEY (`movie_id`(3))
you are telling mysql to create a sub part key* on the first 3 Bytes of movie id. This only works for string types.
You need to use
PRIMARY KEY (`movie_id`)
without providing a length.
*Is this sure the query resulting in the error? Never saw that on a primary key, its used for indexes.
after selecting PRIMARY KEY when you create table, don't input any value in pop dialog
You can also get this error when creating an index if you specify a prefix length that is longer than the length of the actual column. If you tried to create an index containing someColumn(20) but in the table someColumn is VARCHAR(15), then this error will occur.
For the primary key do not enter any length of type.
Example:
int()

mysql RIM error

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.

Unable to create table in MySQL

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

MySQL - Trouble running a script from an existing .sql file which should create a schema, its tables and insert all necessary data in Workbench

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.

What is wrong with the following MySQL create statement?

I am struggling to get this simple table created in MySQL:
create table events (
id int,
when timestamp not null,
summary varchar(256) not null,
description varchar(500) not null,
owner int not null,
attendee int not null,
PRIMARY KEY(id),
FOREIGN KEY(owner) REFERENCES calendar_users(id),
FOREIGN KEY(attendee) REFERENCES calendar_users(id)
);
The description of the FKs are fine and the PK statement. However, MySQL seems to have a problem in the third line for some reason. Could anyone please help me out. Thanks.
when is a reserved word.
To use it as a name, surround it with backticks.
when is a reserved word (http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html)
it would be best just to change the field title. I use datetime for example