Mysql syntax error for creating table - mysql

I want to create a table in mysql:--
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT(1000),
primary key(app_id ),
FOREIGN KEY (user_id) REFERENCES student(sp_id));
where app_id is the primary key and sp_id is the foreign key reference from student table..
but I am getting error:--
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '(1000)0,primary key(app_id ),FOREIGN KEY (sp_id)
REFERENCE' at line 1
why the error is occuring?

Try to Run This one:-
use db;
CREATE TABLE parent (
id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
) ;
CREATE TABLE Persons
(
PID int UNSIGNED NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City longtext,
PRIMARY KEY (PID),
FOREIGN KEY (PID)
REFERENCES parent(id)
ON DELETE CASCADE
);
LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length of LONGTEXT columns also depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGTEXT value is stored using a 4-byte length prefix that indicates the number of bytes in the value.

You should user varchar(1000) in place of longtext(1000).
Moreover user_id column doesn't exist in you table.

You can´t specify the LONGTEXT in this context. Have a look in the manual.
Change your code to:
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT,
primary key(app_id),
FOREIGN KEY (user_id) REFERENCES student(sp_id));

Also, you shouldn't be using spaces inbetween the brackets at primary key(app_id).

Related

creating a table in the database with a foreign key

I am getting this error whenever I try to create my table:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL,
hehi VARCHAR NOT NULL,
adds VARCHAR NOT NULL,
aob VA...' at line 3
This is my code:
CREATE TABLE patients (
patientId int NOT NULL,
problem VARCHAR NOT NULL,
hehi VARCHAR NOT NULL,
adds VARCHAR NOT NULL,
aob VARCHAR NOT NULL,
PRIMARY KEY (patientID),
CONSTRAINT FK_userspatients FOREIGN KEY (user_id)
REFERENCES users(user_id)
);
What might be the problem?
You need to specify the lengths of your VARCHARs,
e.g. if the column problem is supposed to hold 255 characters max.:
problem VARCHAR(255) NOT NULL
see https://mariadb.com/kb/en/varchar/ for more info
Update:
As P.salmon mentioned in the comments, your foreign key definition should be updated to
CONSTRAINT FK_userspatients FOREIGN KEY (patientId)
If patientId refers to the corresponding field user_id of table users.

ENGINE=InnoDB' at line 10 in SQL

I'm trying to create a table with this sql:
CREATE TABLE angestellte (
PersonalNr int(11) NOT NULL AUTO_INCREMENT,
Vorname varchar(50) NOT NULL,
Nachname varchar(50) NOT NULL,
Beruf varchar(50) NOT NULL,
Gehalt int(11) NOT NULL,
arbeitetInAbteilung int(11) NOT NULL,
PRIMARY KEY (PersonalNr),
FOREIGN KEY abteilung (AbteilungID)
)
ENGINE=InnoDB;
But I get only the message
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')
ENGINE=InnoDB' at line 10
I looked really can't find my mistake but think it's probably something obvious I just don't see.
Supposing you have a table where a field (arbeitetInAbteilung) references a row in another table (say arbeiten), you need to define it like this:
CREATE TABLE `angestellte` (
`PersonalNR` INT(11) NOT NULL AUTO_INCREMENT,
...other fields...
`arbeitetInAbteilung` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`PersonalNR`),
INDEX `FK__arbeiten` (`arbeitetInAbteilung`),
CONSTRAINT `FK__arbeiten` FOREIGN KEY (`arbeitetInAbteilung`)
REFERENCES `arbeiten` (`arbeitID`)
ON UPDATE CASCADE
ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
This means that an index will be created to index the field you specify (arbeitetInAbteilung). Then a constraint will be set in place so that this index is linked to the values of another field in a different table, which could be defined like this:
CREATE TABLE `arbeiten` (
`arbeitID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`arbeitID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Note that the two fields must be absolutely identical; if they are text fields, they need to have the same collation; they must be both either NULL or NOT NULL; et cetera. The slightest difference will yield a "Foreign key constraint is incorrectly formed" error, and you'll need to show the engine status for InnoDB and parse its output to understand why.
The ON UPDATE and ON DELETE define what happens when you change a value in the master (arbeiten) table, or you delete it. If the ID 123 becomes 456, CASCADE means that all the rows that referred 123 will now refer 456. Another possibility would be to prevent the operation (RESTRICT), or set the mismatched rows to NULL (SET NULL).
As already commented, your FK syntax is total wrong.
FOREIGN KEY abteilung (AbteilungID)
should be
FOREIGN KEY some_column REFERENCES abteilung (AbteilungID)
Here, some_column should be replaced by the column which you want to designate as the referencing key and the definition of this column should exactly match with column AbteilungID of table abteilung

Confusing error regarding mysql table creation

This is the SQL line I'm using to create the table:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(255),
surname VARCHAR(255),
email VARCHAR(255) UNIQUE,
username VARCHAR(255) UNIQUE,
passwordHash CHAR(60),
admin BIT
)
Should be pretty generic, however, I'm getting the error "Specified key was too long; max key length is 767 bytes"
Which just leaves me wondering, what key is too long? The longest is supposedly 255B, no?
It depends on your server default encoding. For instance, in UTF-8 you use three bytes for each character.
The issue comes from 'UNIQUE' constraint on VARCHAR(255) columns.
Here Try This out:-
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(255),
surname VARCHAR(255),
email VARCHAR(255) UNIQUE,
username VARCHAR(255) UNIQUE,
passwordHash CHAR(60),
admin BIT,
PRIMARY KEY (id),
CONSTRAINT user_info UNIQUE(email,username)
);
i think this will work

sql create table with foreign key

In my database I need to create a table which has two foreign keys, I am unable to figure out the source of error although I tried .Any body help me in solving this problem.
The mysql command I gave to create the table
create table book_vegetable(
id int NOT NULL AUTO_INCREMENT,
producer_offer_id int NOT NULL,
consumer_id int NOT NULL,
booked_qty varchar,
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id)
REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);
The error I am getting
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'PRIMARY KEY(id),FOREIGN KEY(producer_offer_id) REFERENCES
producer_offer(id),FOR' at line 1
Your first problem was caused by a missing length specification on the booked_qty varchar column.
The usual suspects for error 150:
mismatch in primary vs. foreign key type (for example int - bigint). make sure they match exactly
different table engines (for example if you try to reference a MyIsam table in a InnoDB table)
This works in MySQL, just tested (note that producer offer and user are just a mock tables).
create table producer_offer (id int(10) not null auto_increment, primary key(id));
create table user (id int(10) not null auto_increment, primary key(id));
create table book_vegetable (
id int(10) NOT NULL AUTO_INCREMENT,
producer_offer_id int(10) NOT NULL,
consumer_id int(10) NOT NULL,
booked_qty varchar(255),
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id) REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);

Foreign key issue in mysql (error 1005)

I have attempted to create the following tables:
Customer:
CREATE TABLE Customer(customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
surname VARCHAR(15) NOT NULL,
forename VARCHAR(15) NOT NULL,
DOB TIMESTAMP NOT NULL,
phone_no VARCHAR(20) NOT NULL,
email_address VARCHAR(30),
postcode VARCHAR(15) NOT NULL,
PRIMARY KEY(customer_id)) ENGINE=INNODB;
When I attempt to create the 'Booking' table afterwards, I get an error
"ERROR 1005(HY000): can't create table 'Test.Booking' (errno:150)"
I checked the error online and found out that it was relating to the Foreign key connection that I attempted to make between the tables; which I will give below.
Booking:
CREATE TABLE Booking (booking_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
customer_id INT NOT NULL,
staying_from TIMESTAMP,
staying_until TIMESTAMP,
cost INT,
PRIMARY KEY(booking_id),
FOREIGN KEY(customer_id) REFERENCES Customer(customer_id)
ON UPDATE CASCADE ON DELETE RESTRICT) ENGINE=INNODB;
As documented under FOREIGN KEY Constraints:
Foreign keys definitions are subject to the following conditions:
[ deletia ]
Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.
In Customer you have INT UNSIGNED whereas in Booking you have INT.