I have 3 tables REVIEW,PAPER and PCMEMBER and the codes for it is as mentioned below:
CREATE TABLE REVIEW(
due_date DATE NOT NULL,
review_date DATE NOT NULL,
recommendation VARCHAR(50) NOT NULL,
comment VARCHAR(50) NOT NULL,
pcmem_id NUMBER(10) NOT NULL,
paper_id NUMBER(10) NOT NULL,
CONSTRAINT review_pk PRIMARY KEY (pcmem_id,paper_id),
CONSTRAINT review_fk FOREIGN KEY(paper_id)
REFERENCES PAPER(paper_id),
CONSTRAINT review_fk1 FOREIGN KEY(pcmem_id)
REFERENCES PCMEMBER(pcmem_id));
CREATE TABLE PCMEMBER(
pcmem_id NUMBER(10) NOT NULL PRIMARY KEY,
pc_fname VARCHAR(20) NOT NULL,
pc_sname VARCHAR(20) NOT NULL,
pc_title VARCHAR (20) NOT NULL,
pc_position VARCHAR(20) NOT NULL,
affiliation VARCHAR(20) NOT NULL,
pc_email VARCHAR(20) NOT NULL,
track_id NUMBER(6) NOT NULL,
CONSTRAINT pcmember_fk FOREIGN KEY(track_id)
REFERENCES TRACK(track_id));
CREATE TABLE PAPER(
paper_id NUMBER(10) PRIMARY KEY NOT NULL,
paper_title VARCHAR(20) NOT NULL,
abstract VARCHAR(50) NOT NULL,
paper_type VARCHAR(20) NOT NULL,
submission_date DATE NOT NULL,
track_id NUMBER(6) NOT NULL,
CONSTRAINT paper_fk FOREIGN KEY(track_id)
REFERENCES TRACK(track_id),
CONSTRAINT chk_type CHECK(paper_type IN ('full paper','Research-in-Progress','posters')),
);
I'm trying to add a condition where "Each paper will be reviewed by exactly 3 PC members". Not sure what CHECK constraints I should be using? I just need this for creating tables. Thanks
One way you could implement this type of check constraint is with a scalar function that performs the check.
The following function, given a paper id, will check whether the paper is reviewed by 3 members only and it returns 1 if the condition is satisfied and 0 otherwise.
CREATE FUNCTION isReviewdByThreeMembers (
#paperID NUMBER(10)
)
RETURNS INTEGER
AS
BEGIN
DECLARE #count INTEGER
SELECT #count = COUNT(*)
FROM Review
WHERE paper_id = #paperID
RETURN CASE
WHEN #count = 3 THEN 1
ELSE 0
END
END
And then use this function in your check constraint like so:
CHECK (isReviewdByThreeMembers(paper_id) = 1)
You mentioned you want each paper to "only be reviewed by 3 members" but this condition can never be satisfied by records that don't already exist because each new paper would initially start with no reviews and incrementally get new reviews until it gets to 3.
If you were to modify the constraint to allow each paper to be reviewed by "up to 3 reviewers" then the constraint would actually be a bit more useful. Perhaps in conjunction with another constraint that would maybe prevent the status of a paper from transitioning into "REVIEWED" or "COMPLETED" once the 3 required reviews are completed.
Related
I am creating an inventory management app in node.js that uses MySQL as a database. I have a weak entity “rental_item” that holds the items in a particualr rental. The issue is that the rental may not come back all at once so I need a way of marking the “rental_returned” boolean in the rental table true only when all of the “item_returned” entires are true.
Here is my table structure:
CREATE TABLE `rental` (
`rental_id` int NOT NULL AUTO_INCREMENT,
`renter_id` int NOT NULL,
`date_in` date NOT NULL,
`date_out` date NOT NULL,
`sig_path` varchar(50) NOT NULL,
`doc_path` varchar(50) NOT NULL,
`col_name` varchar(50) NOT NULL,
`col_path` varchar(50) NOT NULL,
`cost` decimal(15,2) NOT NULL,
`rental_returned` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`rental_id`),
UNIQUE KEY `doc_path` (`doc_path`),
UNIQUE KEY `col_path` (`col_path`),
UNIQUE KEY `sig_path` (`sig_path`),
KEY `renter_key` (`renter_id`),
CONSTRAINT `renter_key` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
)
CREATE TABLE `rental_item` (
`rental_id` int NOT NULL,
`i_ID` varchar(20) NOT NULL,
`item_returned` tinyint(1) NOT NULL DEFAULT '0',
KEY `rental_key` (`rental_id`),
KEY `rental_item_key` (`i_ID`),
CONSTRAINT `rental_item_key` FOREIGN KEY (`i_ID`) REFERENCES `item` (`i_ID`),
CONSTRAINT `rental_key` FOREIGN KEY (`rental_id`) REFERENCES `rental` (`rental_id`) ON DELETE CASCADE
)
I am currently doing this through the mysql2 node.js module and just checking for all the values of a given rental_id. I then found out about triggers and thought this way could be better. I fiddled round with things like this Trigger with table join, but couldn’t wrap my head around how to get the rental_id of the entry that was updated from rental_item, then check that all entires in rental_item with that id have item_returned = 1, and finally update the rental table to show that all the items/the complete rental has been returned.
I understand that this sould be an update after trigger on rental_item but dont know how to handle the conditionals or loops needed.
Use NEW.rental_id to get the ID of the row that was updated.
CREATE TRIGGER rental_returned AFTER UPDATE ON rental_item
FOR EACH ROW
UPDATE rental
SET rental_returned = (
NOT EXISTS (
SELECT *
FROM rental_item
WHERE rental_id = NEW.rental_id
AND item_returned = 0))
WHERE rental_id = NEW.rental_id
I have a little problem with one database. I have already entered data in the individual tables in the database. The problem is that with this code, it displays the column names, but didnt return rows. I can't find the error. I think the problem is in JOIN itself. Any ideas for solving the problem?
SELECT cars.brand,
cars.model,
cars.yearofproduction,
cars.engine_type,
parts.part_name,
parts.price AS MONEY,
parts.quantity
FROM CATALOG
JOIN parts
ON parts.part_name = parts.id
JOIN cars
ON CATALOG.car_id = cars.id
WHERE quantity >= '0'
HAVING MONEY < (
SELECT AVG(price)
FROM cars
);
And here the tables. I've already insert values in the tables.
CREATE TABLE CATALOG.parts
(
id INT AUTO_INCREMENT PRIMARY KEY,
part_name VARCHAR(255) NOT NULL,
price DECIMAL NOT NULL,
DESCRIPTION VARCHAR(255) DEFAULT NULL,
quantity TINYINT DEFAULT 0
);
CREATE TABLE CATALOG.cars
(
id INT AUTO_INCREMENT PRIMARY KEY,
brand VARCHAR(255) NOT NULL,
model VARCHAR(255) NOT NULL,
yearofproduction YEAR NOT NULL,
engine_type SET('Diesel', 'A95', 'Gas', 'Metan')
);
CREATE TABLE CATALOG.catalog
(
part_id INT NOT NULL,
CONSTRAINT FOREIGN KEY(part_id) REFERENCES parts(id)
ON DELETE RESTRICT ON UPDATE CASCADE,
car_id INT NOT NULL,
CONSTRAINT FOREIGN KEY(car_id) REFERENCES cars(id)
ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY(part_id, car_id)
);
I'm new and I'm not so good with the triggers, I place the tables and figure out how to create this sum
-- Table `databasestreaming`.`tipologiaabbonamento`
CREATE TABLE IF NOT EXISTS `databasestreaming`.`tipologiaabbonamento` (
`idtipologiaabbonamento` INT NOT NULL AUTO_INCREMENT,
`piano abbonamento` VARCHAR(45) NULL,
`dispositivi` VARCHAR(45) NULL,
`Risoluzione` VARCHAR(45) NULL,
`Prezzo` INT NULL,
PRIMARY KEY (`idtipologiaabbonamento`))
ENGINE = InnoDB;
-- Table databasestreaming.abbonamento
CREATE TABLE IF NOT EXISTS `databasestreaming`.`abbonamento` (
`idabbonamento` INT NOT NULL AUTO_INCREMENT,
`data attivazione` DATE NULL,
`data termine` DATE NULL,
`Sport` ENUM("Si", "No") NULL,
`cliente_idcliente` INT NOT NULL,
`tipologiaabbonamento_idtipologiaabbonamento` INT NOT NULL,
`Prezzo_finale` INT NULL,
PRIMARY KEY (`idabbonamento`, `cliente_idcliente`, `tipologiaabbonamento_idtipologiaabbonamento`),
INDEX `fk_abbonamento_cliente_idx` (`cliente_idcliente` ASC) ,
INDEX `fk_abbonamento_tipologiaabbonamento1_idx` (`tipologiaabbonamento_idtipologiaabbonamento` ASC) ,
CONSTRAINT `fk_abbonamento_cliente`
FOREIGN KEY (`cliente_idcliente`)
REFERENCES `databasestreaming`.`cliente` (`idcliente`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_abbonamento_tipologiaabbonamento1`
FOREIGN KEY (`tipologiaabbonamento_idtipologiaabbonamento`)
REFERENCES `databasestreaming`.`tipologiaabbonamento` (`idtipologiaabbonamento`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I would like that when the user selects from enum Si with Sport, the trigger will take the price from the type of subscription (three types of subscriptions that will choose the user) and add the type of subscription chosen to the eventual or not of the sport choice, if yes, then we add +5 if instead it chooses no, in the final price I would like it to simply report the price of the chosen subscription
Tables And what i need
I'm trying to Create a view with 5 tables for a school project. The statement so far goes like this:
CREATE view lager AS
Select produkt.produktNumber,
(SELECT buyphone.lagerNummer FROM bluecity.buyphone) AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
(SELECT buyphone.colorValue FROM bluecity.buyphone) AS 'Farve',
(SELECT buyphone.conditionValue FROM bluecity.buyphone) AS 'Stand'
FROM bluecity.produkt
JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
JOIN bluecity.color ON buyphone.colorValue = bluecity.color.colorInformation
JOIN bluecity.conditions ON buyphone.conditionValue = bluecity.conditions.conditionInformation
But i can't seem to get the joins right. The main table bluecity.produkt needs to join some of it's values with other table. The first join with Memory size works, but that is it. The main table is supposed to hold an Integer Value which draws meaning from the joined table if that makes sense.
Help is much appriciated, if you can explain why and how it would be even better so i can try to understand.
Added create stmt
CREATE DATABASE bluecity;
CREATE TABLE bluecity.Member
(memberNumber INTEGER(5) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(25) NOT NULL,
address VARCHAR(40) NOT NULL,
zipNumber INTEGER(4) NOT NULL,
phoneNumber INTEGER(8) NOT NULL,
email VARCHAR(35) NOT NULL,
statusValue INTEGER(1) NOT NULL,
FOREIGN KEY (zipNumber) REFERENCES bluecity.zipCode(zipNumber),
FOREIGN KEY (statusValue) REFERENCES bluecity.ID(statusValue),
PRIMARY KEY (memberNumber));
CREATE TABLE bluecity.ID
(statusValue INTEGER(1) NOT NULL,
information VARCHAR(19) NOT NULL,
PRIMARY KEY (statusValue))
CREATE TABLE bluecity.zipCode
(zipNumber INTEGER(4) NOT NULL,
city VARCHAR(32) NOT NULL,
PRIMARY KEY (zipNumber));
CREATE TABLE bluecity.Produkt
(produktNumber INTEGER(5) NOT NULL,
produktType VARCHAR(30) NOT NULL,
produktBrand VARCHAR(30) NOT NULL,
produktModel VARCHAR(30) NOT NULL,
memorySize INTEGER(2) NOT NULL,
FOREIGN KEY (memorySize) REFERENCES bluecity.sizeMemory(memorySize),
PRIMARY KEY (produktNumber));
CREATE TABLE bluecity.Conditions
(conditionValue INTEGER(1) NOT NULL,
conditionInformation VARCHAR(13) NOT NULL,
PRIMARY KEY (conditionValue));
CREATE TABLE bluecity.sizeMemory
(memorySize INTEGER(1) NOT NULL,
memoryInformation VARCHAR(5) NOT NULL,
PRIMARY KEY (memorySize));
CREATE TABLE bluecity.Color
(colorValue INTEGER(2) NOT NULL,
colorInformation VARCHAR(20) NOT NULL,
PRIMARY KEY (colorValue));
CREATE TABLE bluecity.Prices
(conditionValue INTEGER(1) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
price INTEGER(6) NOT NULL,
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue),
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
PRIMARY KEY (conditionValue, produktNumber));
CREATE TABLE bluecity.buyPhone
(IMEI Integer(15) NOT NULL,
lagerNummer INTEGER(7) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
colorValue INTEGER(2) NOT NULL,
conditionValue INTEGER(1) NOT NULL,
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
FOREIGN KEY (colorValue) REFERENCES bluecity.Color(colorValue),
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue), PRIMARY KEY (IMEI));
You should also make a join on the bluecity.buyphone instead of having it as subqueries in your select, as it is related to your produkt table.
Something like this:
Select produkt.produktNumber,
buyphone.lagerNummer AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
buyphone.colorValue AS 'Farve',
buyphone.conditionValue AS 'Stand'
FROM produkt
JOIN buyphone ON buyphone.produktNumber = produkt.produktNumber
JOIN sizememory ON produkt.memorySize = sizememory.memorySize
JOIN color ON buyphone.colorValue = color.colorInformation
JOIN conditions ON buyphone.conditionValue = conditions.conditionInformation
Select DISTINCT produkt.produktNumber AS 'Produkt #',
buyphone.lagerNummer AS 'Lager #',
produkt.produktBrand AS 'Mærke',
produkt.produktModel AS 'Model',
sizeMemory.memoryInformation 'Hukommelse',
color.colorInformation AS 'Farve',
conditions.conditionInformation AS 'Stand',
prices.price AS 'Pris'
FROM bluecity.buyphone
JOIN bluecity.produkt ON bluecity.buyphone.produktNumber = bluecity.produkt.produktNumber
JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
JOIN bluecity.color ON bluecity.buyphone.colorValue = bluecity.color.colorValue
JOIN bluecity.conditions ON bluecity.buyphone.conditionValue = bluecity.conditions.conditionValue
JOIN bluecity.prices ON bluecity.produkt.produktNumber = bluecity.prices.produktNumber
JOIN bluecity.prices p1 ON bluecity.buyphone.conditionValue = bluecity.prices.conditionValue
I got it working like this, thx guys.
Edit: there still was some flaws which i think is fixed now.
I am building as type on inventory table that keeps track of stock by 6 different factors. I am using an I query much like this one:
INSERT INTO inventory ( productid, factor1, factor2, factor3, factor4, factor5, factor6, quantity, serial_number)
VALUES (242332,1,1,1,'V67',3.30,'NEW',10,NULL)
ON DUPLICATE KEY UPDATE `quantity` = VALUES(`quantity`) + quantity;
The inventory table has a UNIQUE KEY for ( productid, factor1, factor2, factor3, factor4, factor5, factor6, serial_number ). For some reason, it is not picking up on the key and just INSERTing instead of UPDATEing. Can anyone offer an explanation why? What am I missing?
Here is the table create statement:
CREATE TABLE `inventory` (
`stockid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`productid` int(11) unsigned NOT NULL,
`factor1` int(11) unsigned NOT NULL,
`factor2` int(11) unsigned NOT NULL,
`factor3` int(11) unsigned NOT NULL,
`factor4` varchar(8) NOT NULL,
`factor5` decimal(10,2) NOT NULL,
`factor6` enum('A','B','C','D','NEW') NOT NULL,
`quantity` int(11) NOT NULL,
`stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`serial_number` varchar(11) DEFAULT NULL,
PRIMARY KEY (`stockid`),
UNIQUE KEY `serial_number` (`serial_number`),
UNIQUE KEY `productid_2` (`productid`,`factor1`,`factor2`,`factor3`,`factor4`,`factor5`,`factor6`,`serial_number`),
KEY `productid` (`productid`),
KEY `factor1` (`factor1`),
KEY `factor2` (`factor2`),
KEY `factor3` (`factor3`),
CONSTRAINT `books_stock_ibfk_2` FOREIGN KEY (`productid`) REFERENCES `produx_products` (`productid`),
CONSTRAINT `books_stock_ibfk_5` FOREIGN KEY (`factor1`) REFERENCES `table_factor1` (`factorid`),
CONSTRAINT `books_stock_ibfk_6` FOREIGN KEY (`factor2`) REFERENCES `table_factor2` (`factorid`),
CONSTRAINT `books_stock_ibfk_7` FOREIGN KEY (`factor3`) REFERENCES `table_factor3` (`factorid`)
)
ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=latin1
More in-depth:
The purpose of this table is to hold stock quantities. I think this is pretty straight forward. The factors that separate these quantities are as follows:
factor1 = storeid (the unique indentifier for the store that ownes this quantity).
factor2 = supplierid (the unique indentfier for the supplier that we got the quantity from)
factor3 = warehouseid (unique identifier for the warehouse where it resides)
factor4 = locationid (unique string for the location. Its physically painted on the shelf)
factor5 = cost (what we paid for each of the quantity)
factor6 = condition (enum ['NEW','USED','RENTAL','PREORDER']. The first three are easy, the fourth is for quantites we ordered, want to sell, but have not received it yet.)
I know this is a hefty key but I am forced to keep it this way. I have had many suggestion to move cost or condition to the product table. I cannot do this. The cost isn't always the same since we buy a lot from auctions or other places with very variable costs and conditions.
I hope this helps more to explain what I am trying to do.
Mysql allows multiple NULLs in an unique constraint.In your serial_number column replace NULL with a value and the constraint is triggered,see:
http://sqlfiddle.com/#!2/9dbd19/1
a UNIQUE index permits multiple NULL values for columns that can
contain NULL
Docs
Make the column NOT NULL and use '' which is empty.