cant figure out SQL syntax for this code - mysql

Its between so long since i did SQL when i try to paste this code into phpmyadmin database it wont create the table i get error:
#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 'NOT NULL PRIMARY KEY, WORKER CHAR(30), CONSTRAINT WORKER_FK FOREIGN KEY(WORKE' at line 2
this is code for the sql table:
CREATE TABLE REPORT(
REPORT_ID NOT NULL PRIMARY KEY,
WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
COMMENT CHAR(30)
)engine innoDB;
can anyone help me out please

The actual error message comes from the fact that report_id doesn't have a data type.
REPORT_ID NOT NULL PRIMARY KEY,
should be
REPORT_ID INTEGER NOT NULL PRIMARY KEY,
But you have a lot more problems. The "inline" foreign keys are silently ignored by MySQL - even with InnoDB. You have to move them to the end.
Additionally your foreign key to the jobs table can not be correct. First off, there is no job column in your table, so FOREIGN KEY(JOB) REFERENCES JOB(END_DATE) can't be correct.
Secondly you are referencing two different PK definitions to the same table. My guess is you actually want:
CONSTRAINT START_END_DATE_FK
FOREIGN KEY (START_DATE, END_DATE)
REFERENCES JOB(START_DATE, END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
This assumes that (START_DATE, END_DATE) is the primary key of the JOB table - which does sound a bit strange.
Not sure if comment is a reserved word in MySQL. If it is, you need to quote the column name.
So put this alltogether, you probably need something like this:
CREATE TABLE REPORT
(
REPORT_ID INTEGER NOT NULL PRIMARY KEY,
WORKER CHAR(30) ,
CLIENT CHAR (30),
START_DATE DATE ,
END_DATE DATE,
COMMENT CHAR(30),
CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT CLIENT_FK FOREIGN KEY (client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT START_END_DATE_FK FOREIGN KEY (START_DATE, END_DATE) REFERENCES JOB(START_DATE, END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL
);

You are not defining a column type for REPORT_ID. ie REPORT_ID int(11) NOT NULL PRIMARY KEY,
CREATE TABLE REPORT(
REPORT_ID int(11) NOT NULL PRIMARY KEY,
WORKER CHAR(30) CONSTRAINT WORKER_FK FOREIGN KEY(WORKER) REFERENCES WORKER(WORKER_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CLIENT CHAR (30) CONSTRAINT CLIENT_FK FOREIGN KEY(client) REFERENCES CLIENT(CLIENT_ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
START_DATE DATE CONSTRAINT STARTDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(START_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
END_DATE DATE CONSTRAINT ENDDATE_FK FOREIGN KEY(JOB) REFERENCES JOB(END_DATE)
ON UPDATE CASCADE
ON DELETE SET NULL,
COMMENT CHAR(30)
)engine innoDB;

Related

what is the error this question,please explain me

CREATE TABLE doctor(
Did varchar(30) not null,
spid int,
Hid int,
Dname varchar(200)not null,
Dnumber int,
fee decimal(10,2)not null,
constraint primary key(Did),
constraint unique(Did,Dname),
constraint foreign key(spid)references speciality(spid)
on delete cascade on update cascade,
constraint foreign key(Hid)references hospital(Hid)
on delete cascade on update cascade
)engine=innodb;
It's hard to give an answer without the error message or your version of mysql, but I'd recommend to be sure that spid column of speciality is also defined is int, not int(11) or unsigned int(and also for other foreign key references).
And also your primary key is already always unique, I don't see any point including that in another unique constraint.

mariadb foriegn key insert error

So, I was using mysql server on Centos 6 and it was alright then, I shifted my code to Centos 7 server.
I had a constraint in an table where I used to insert null values by default in MySQL Server. I guess thats not happenning in MariaDB.
I get the following error when inserting data.
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (dbName.tableName, CONSTRAINT constraintName FOREIGN KEY (columnName) REFERENCES externalTableName (externalTableColumnName))
Any help would be appreciated.
Thanks
UPDATE 1 :
Table with the key to be referenced :
CREATE TABLE `users` (
`uid` int(25) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`uid`)
);
CREATE TABLE `abc_xyz` (
`isUser` int(25) DEFAULT NULL,
`last_modified_user` int(25) DEFAULT NULL,
KEY `abc_xyz_is_user` (`isUser`),
KEY `abc_xyz_last_modified_user` (`last_modified_user`),
CONSTRAINT `abc_xyz_is_user` FOREIGN KEY (`isUser`) REFERENCES `users` (`uid`),
CONSTRAINT `abc_xyz_last_modified_user`
FOREIGN KEY (`last_modified_user`) REFERENCES `users` (`uid`)
);
If the columnName is NULLable then you are not inserting a NULL value. I really doubt that inserting NULL in a NULLable column can create a foreign key failure. Check the query being called (if you use ORM, etc).
The issue is that your referring column can contain a NULL value while the referred column cannot (it's a PRIMARY KEY). You can't enter a default null value in the abc_xyz.last_modified_user column and maintain a valid foreign key to the users.uid that cannot accept null.
A PRIMARY KEY Constraint is either a < Table Constraint> or a and defines a rule that constrains a unique key to non-duplicate, non-null values only. The required syntax for a PRIMARY KEY Constraint is:
https://mariadb.com/kb/en/sql-99/constraint_type-primary-key-constraint/

MySQL reference non-unique value in parent table

I have an approval tag and would like it to be populated down when it is changed in the parent table, however I cannot figure out how to reference the parent from the child to pull the value down.
For reference I want the value approval in reservations to be populated down to reservedTickets when I update the approval from 0 to 1 in reservations. However, to my understanding I cannot use a standard FOREIGN KEY or REFERENCE reference to reservations since approval is not unique.
Before anyone says anything about "why not have the value in only one table" it is to separate concerns between the administrator who has the ability to update the reservations table and a non-admin updating reservedTickets. Also, due to the high number of FOREIGN KEY constraints in reservedTickets having to join with the reservations table to track approval can be tricky depending on my starting point.
CREATE TABLE reservations(
rid int NOT NULL AUTO_INCREMENT,
aid int NOT NULL,
approval tinyint(1) NOT NULL,
creationTime TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rid),
FOREIGN KEY (aid) REFERENCES accounts (aid)
ON DELETE CASCADE
);
CREATE TABLE reservedTickets(
rid int NOT NULL,
tid int NOT NULL,
hid int NOT NULL,
approval tinyint(1) NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (rid) REFERENCES reservations (rid)
ON DELETE CASCADE,
FOREIGN KEY (tid) REFERENCES tickets (tid)
ON DELETE CASCADE,
FOREIGN KEY (hid) REFERENCES people (hid)
ON DELETE CASCADE,
FOREIGN KEY (approval) REFERENCES reservations (approval)
ON UPDATE CASCADE
);
Personally I'd avoid cascade updates because of many locks the engine issues behind the scene, but if you really want it you can have a unique constraint on reservations(rid,approval) and foreign key in reservedTickets that references it. As far as I remember Mysql, it comes with a cost of extra index, but it gives you what you want.
On the other hand , you can implement desired functionality with trigger on reservation, or leave it up to application (or update table only through stored procedure that takes care of carrying this flag).
CREATE TABLE reservations(
rid int NOT NULL AUTO_INCREMENT,
aid int NOT NULL,
approval tinyint(1) NOT NULL,
creationTime TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rid),
CONSTRAINT UQ_RESERVATION_COMPOSITE UNIQUE(rid, approval),
...
);
CREATE TABLE reservedTickets(
rid int NOT NULL,
tid int NOT NULL,
hid int NOT NULL,
approval tinyint(1) NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (rid,approval) REFERENCES reservations (rid,approval)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (tid) REFERENCES tickets (tid)
ON DELETE CASCADE,
FOREIGN KEY (hid) REFERENCES people (hid)
ON DELETE CASCADE , ....
);
It's not so much that you cannot use a foreign key on a non-unique column, because you can in InnoDB. However, the problem that arises with this would be if you have automatic updating or deleting on the foreign key, because the database would update all keys that were the same.
What you may be able to do, is use a prepared statement that allows you to update the approval column when updated from the reservations table, etc.

Getting a "foreign key constraint fails" even though I have "on delete cascade"

I thought the point of ON DELETE CASCADE was that this wouldn't happen. :\ I have the following tables:
CREATE TABLE Tweets (
tweetID INTEGER NOT NULL AUTO_INCREMENT,
userID INTEGER NOT NULL,
content VARCHAR(140) NOT NULL,
dateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
hasPoll INTEGER NOT NULL,
visible INTEGER NOT NULL DEFAULT 1,
PRIMARY KEY (tweetID),
FOREIGN KEY (userID) REFERENCES Users(userID)
ON DELETE CASCADE
);
CREATE TABLE Polls (
pollID INTEGER NOT NULL AUTO_INCREMENT,
tweetID INTEGER NOT NULL,
pollOptionText VARCHAR(300),
PRIMARY KEY (pollID),
FOREIGN KEY (tweetID) REFERENCES Tweets(tweetID)
);
The problem is that when I try to delete a Tweet which has a Poll attached to it, I get the following error (via Flask):
_mysql_exceptions.IntegrityError
IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`twitter`.`polls`, CONSTRAINT `polls_ibfk_1` FOREIGN KEY (`tweetID`) REFERENCES `Tweets` (`tweetID`))')
Help please!
That is indeed the point of on delete cascade. You get the error, because your code doesn't declare on delete cascade from "poll" to "tweet".
CREATE TABLE Polls (
pollID INTEGER NOT NULL AUTO_INCREMENT,
tweetID INTEGER NOT NULL,
pollOptionText VARCHAR(300),
PRIMARY KEY (pollID),
FOREIGN KEY (tweetID) REFERENCES Tweets(tweetID)
ON DELETE CASCADE
);
This will delete rows in "Polls" when corresponding rows are deleted in "Tweets".
You have to put ON DELETE CASCADE after FOREIGN KEY (tweetID) REFERENCES Tweets(tweetID)
According to the MySQL Foreign Key Constraints reference:
CASCADE: Delete or update the row from the parent table, and
automatically delete or update the matching rows in the child table.
Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
Also, according to the MySQL Foreign Keys reference:
For storage engines other than InnoDB, it is possible when defining a
column to use a REFERENCES tbl_name(col_name) clause, which has no
actual effect, and serves only as a memo or comment to you that the
column which you are currently defining is intended to refer to a
column in another table.
So since the foreign key is from the child table to the parent table, it makes foo a parent table and Polls a child table, so deleting a row from Tweets will cascade deletions to Pools, providing you use InnoDB or some other storage engine that supports it.
UPDATE:
This error is because you have a relation between poll and twitter... without cascading you have to delete or update the polls removing the relation with the Tweet that will be deleted. Or use ON DELETE CASCADE:
CREATE TABLE Tweets (
tweetID INTEGER NOT NULL AUTO_INCREMENT,
content VARCHAR(140) NOT NULL,
PRIMARY KEY (tweetID)
);
CREATE TABLE Polls (
pollID INTEGER NOT NULL AUTO_INCREMENT,
tweetID INTEGER NOT NULL,
pollOptionText VARCHAR(300),
PRIMARY KEY (pollID),
FOREIGN KEY (tweetID) REFERENCES Tweets(tweetID)
ON DELETE CASCADE
);
INSERT INTO Tweets VALUES(1,'tweet');
INSERT INTO Polls VALUES(1,1,"pool");
DELETE FROM Tweets WHERE tweetID = 1;
You will surely get this error. you have to keep at least one record into tweets.

ERROR 1452: Cannot add or update a child row

I'm having a bit of a strange problem, I'm trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge of MySQL, the only thing that could possibly be suspect is that there is a foreign key on a different table referencing the one I am trying to reference.
Here is a picture of my table relationships, generated via workbench: Relationships
CREATE TABLE `beds` (
`bedId` int(11) NOT NULL,
`wardId` int(11) DEFAULT NULL,
`depId` int(11) DEFAULT NULL,
`desc` varchar(45) DEFAULT NULL,
PRIMARY KEY (`bedId`),
KEY `departmentId_idx` (`depId`),
KEY `wardId_idx` (`wardId`),
CONSTRAINT `departmentId` FOREIGN KEY (`depId`)
REFERENCES `department` (`Department_Id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `wardId` FOREIGN KEY (`wardId`) REFERENCES `wards` (`wardId`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(`asiahospitaldb`.`beds`, CONSTRAINT `departmentId` FOREIGN KEY (`depId`)
REFERENCES `department` (`Department_Id`)
ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `asiahospitaldb`.`Beds` (`bedId`, `wardId`, `depId`, `desc`)
VALUES ('456', '7444', '4555', 'ikiuj')
This
ERROR 1452: Cannot add or update a child row: a foreign key constraint
fails (`asiahospitaldb`.`beds`, CONSTRAINT `departmentId` FOREIGN KEY
(`depId`) REFERENCES `department` (`Department_Id`) ON DELETE NO
`enter code here`ACTION ON UPDATE NO ACTION)
is telling you that the row you have inserted expects a corresponding value for department/department_id for the value you have inserted into column depId (as Omesh pointed out). The important bit is here:
(depId) REFERENCES department (Department_Id)
In other words, you have tried to create a bed in a non-existent department.