What is the wrong in the following MySql query? - mysql

create table rolemaster(roleno int(2) not null,
primary key(roleno),
rolename varchar(20));
create table roombed(roomnumber varchar(10),
bednumber varchar(10) not null primary key,
username varchar,
foreign key(username) references roleusers(username));
It is showing
ErrorNr: 1064.
How can I resolve this error?

You've forgot to define a size for username varchar.
CREATE TABLE roombed
(
roomnumber VARCHAR(10),
bednumber VARCHAR(10) NOT NULL PRIMARY KEY,
username VARCHAR(10),
-- ^--^----------- This
FOREIGN KEY(username) REFERENCES roleusers(username)
);

Give this a shot
create table roleusers(username varchar(10) not null primary key );
create table rolemaster(roleno int(2) not null primary key ,
rolename varchar(20));
create table roombed(roomnumber varchar(10),
bednumber varchar(10) not null primary key,
username varchar(10),
foreign key (username) references roleusers (username));
Ive added a roleusers table so I can give you a working script.
EDIT : What you were doing wrong for starters was the comma before the primary key.
The foreign key you added to a table we dont know about have to reference a key on the other table so in the example posted Ive added a roleusers table setting username as a primary key so it could be referenced later as a foreign key.
You had also forgotten to correctly define the varchar on username. as mentioned by another answer.
Hope this explanation helps

Related

How to create a weak entity in mySql?

I want to know how can I create weak entities in mySql by Creating tables, I have the code like this:
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp
);
CREATE TABLE personal_Infos(
...
);
These are the tables, all I want to Know is how can I Connect to each-other with foreign key and should I create another primary key at the second table? (Second table has some more informations for the first table)
should I create another primary key at the second table?
Yes you do need to create a primary key in your second table personal_Infos table and that primary key will become foreign key in your users table.
how can I Connect to each-other with foreign key ?
Suppose your primary key in personal_Infos table is P_id, then you can add it as foreign key in your users table like shown below
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp,
p_id int not null,
FOREIGN KEY (P_Id) REFERENCES personal_Infos(P_Id)
);

MySQL - error 1215, cannot add foreign key constraint

I know there are numerous questions regarding this error on here but I've searched through many and none seem to explain it in my case!
I've created a table using the following code:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
And am trying to create a table with a foreign key that refers to the first table using this code:
CREATE TABLE Concert (
referenceNo VARCHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
But it's giving me the 1215 error message!
Syntax wise this isn't correct.
The issue's resolved here:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
CREATE TABLE Concert (
referenceNo VARCaHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue(`venueEmail`) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
Note:
The column being referenced should be stated like table_name(column_name).
You missed the column_name part.
REFERENCE
More:
#Bill Karwin added the following useful info in the comment section:
FWIW this is a MySQL idiosyncrasy. In standard SQL, if you omit the
referenced column name, it defaults to the same name as the foreign
key column. But InnoDB doesn't support this shortcut syntax—you must
specify the column in both cases.

mysql table is not creating

I am getting error when I create table with foreign key
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id,_user_email,_user_mobile));
_users table created successfully..there were no error..
But When I want to create employee table :
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_mobile VARCHAR(15) not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_mobile) references _users(_user_mobile));
Its showing error:
ERROR 1005 (HY000): Can't create table 'DB.employee' (errno: 150)
What am I doing wrong??
Hey In this case you just need to do one thing ,
you just need to add index to the reference column of the user table and then run the create table for employee
ALTER TABLE `_users` ADD INDEX (`_user_mobile`);
After running above query just run the below query :-
CREATE TABLE `employee`(
`_Id` INT(11) NOT NULL AUTO_INCREMENT,
`_user_mobile` VARCHAR(15) NOT NULL,
`_name` VARCHAR(15),
`_org` VARCHAR(10),
PRIMARY KEY (`_Id`),
FOREIGN KEY (`_user_mobile`) REFERENCES `_users`(`_user_mobile`) );
In this way you will get rid of the error 1005 of mysql which says that you need to have index on the reference column of parent table.
150 is a foreign key error:
C:\>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed
Getting the exact error message is very tricky. You need to run this query:
show engine innodb status
... and search in the output:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
160627 14:09:32 Error in foreign key constraint of table test/employee:
foreign key (_user_mobile) references _users(_user_mobile)):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Once you know that, it'd be easy to add the missing index:
ALTER TABLE `_users`
ADD UNIQUE INDEX `_user_email` (`_user_email`);
But I wouldn't if I were you. It's weird to use mobile phone number as key. Instead, just simplify the primary key:
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id));
... and use in the linked table:
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_id int(20) unsigned not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_id) references _users(_id));
The problem is in the foreign key part. If you remove that, table will be created without a problem.
If you need to use that foreign key, you need to use InnoDB as the storage engine of MySQL. InnoDB allows a foreign key constraint to reference a non-unique key as can be seen in here.

making two columns primary key

Hello i have a table which has one primary key by the name of ImageID and i want to make another column
also primary key which is PropertyID means Composite Key
HERE IS THE CODE, but its showing this error for me "#1064 - 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 '( PropertyID INT, ImageID INT primary key (PropertyID, ImageID) )' at line "
Also the ImageID is already primary key, but with varchar(15) specification.
Alter TABLE properties (
PropertyID INT,
ImageID INT,
primary key (PropertyID, ImageID)
);
Each table can only have 1 primary key. You can only have one primary key, but you can have multiple columns in your primary key.
Taken from W3Schools:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)
Here the only primary key is pk_PersonID but that have stated that pk_PersonID is made up of P_Id and LastName.
Unique Indexes may be what you're looking for. This means unique values are required and runs like an index in that it can speed up queries.
Try this:
First drop the existing primary key
ALTER TABLE properties
DROP PRIMARY KEY;
Then add composite key
ALTER TABLE properties
ADD PRIMARY KEY(imageID, propertyID);
As I understand you already have a table with one key, which looks like the following:
CREATE TABLE `common`.`properties` (
`PropertyID` VARCHAR(15) NOT NULL,
`otherColumn` VARCHAR(45) NULL,
PRIMARY KEY (`PropertyID`));
And now you want to add another PK column and change the type of existing PK column from char to INT. So you need to do it as following:
ALTER TABLE `common`.`properties`
CHANGE COLUMN `PropertyID` `PropertyID` INT NOT NULL ,
ADD COLUMN `ImageID` INT NOT NULL AFTER `otherColumn`,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`PropertyID`, `ImageID`);
P.S. common is my schema name, you can use your own, or even skip it if your schema is default.

MYSQL in PHPMYADMIN - create table relationship from one table

I want to create a table relationship with MYSQL PHPMYADMIN.
I have this Create table:
CREATE TABLE students(code_students int(8)not null AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null,
primary key(code_students, code_advisor)
);
and i want to make a create table named advise relationship between code_students, code_advisor.
Ok this is my tryout.
CREATE TABLE advise (
code_students int(8),
code_advisor int(8),
primary key(code_students, code_advisor),
foreign key(code_students)references students(code_students),
foreign key(code_advisor)references students(code_advisor)
);
mySQL says :
A FOREIGN KEY constraint that references a non-UNIQUE key is not standard SQL. It is an InnoDB extension to standard SQL
Try adding the UNIQUE keyword to your first table:
CREATE TABLE students(
code_students int(8)not null unique AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null unique,
primary key(code_students, code_advisor)
);
Check out this sqlFiddle and see how it works, then try removing the UNIQUE keywords and see that you get the same error that you mentioned. ( Hit the Build Schema button )
http://sqlfiddle.com/#!2/46b69