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
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.
In class, we are all 'studying' databases, and everyone is using Access. Bored with this, I am trying to do what the rest of the class is doing, but with raw SQL commands with MySQL instead of using Access.
I have managed to create databases and tables, but now how do I make a relationship between two tables?
If I have my two tables like this:
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id )
)
and
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
PRIMARY KEY ( customer_id )
)
How do I create a 'relationship' between the two tables? I want each account to be 'assigned' one customer_id (to indicate who owns it).
If the tables are innodb you can create it like this:
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id ),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
) ENGINE=INNODB;
You have to specify that the tables are innodb because myisam engine doesn't support foreign key. Look here for more info.
as ehogue said, put this in your CREATE TABLE
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
alternatively, if you already have the table created, use an ALTER TABLE command:
ALTER TABLE `accounts`
ADD CONSTRAINT `FK_myKey` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON DELETE CASCADE ON UPDATE CASCADE;
One good way to start learning these commands is using the MySQL GUI Tools, which give you a more "visual" interface for working with your database. The real benefit to that (over Access's method), is that after designing your table via the GUI, it shows you the SQL it's going to run, and hence you can learn from that.
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY ( account_id )
)
and
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
)
How do I create a 'relationship' between the two tables? I want each account to be 'assigned' one customer_id (to indicate who owns it).
You have to ask yourself is this a 1 to 1 relationship or a 1 out of many relationship. That is, does every account have a customer and every customer have an account. Or will there be customers without accounts. Your question implies the latter.
If you want to have a strict 1 to 1 relationship, just merge the two tables.
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
)
In the other case, the correct way to create a relationship between two tables is to create a relationship table.
CREATE TABLE customersaccounts(
customer_id INT NOT NULL,
account_id INT NOT NULL,
PRIMARY KEY (customer_id, account_id),
FOREIGN KEY customer_id references customers (customer_id) on delete cascade,
FOREIGN KEY account_id references accounts (account_id) on delete cascade
}
Then if you have a customer_id and want the account info, you join on customersaccounts and accounts:
SELECT a.*
FROM customersaccounts ca
INNER JOIN accounts a ca.account_id=a.account_id
AND ca.customer_id=mycustomerid;
Because of indexing this will be blindingly quick.
You could also create a VIEW which gives you the effect of the combined customersaccounts table while keeping them separate
CREATE VIEW customeraccounts AS
SELECT a.*, c.* FROM customersaccounts ca
INNER JOIN accounts a ON ca.account_id=a.account_id
INNER JOIN customers c ON ca.customer_id=c.customer_id;
Adding onto the comment by ehogue, you should make the size of the keys on both tables match. Rather than
customer_id INT( 4 ) NOT NULL ,
make it
customer_id INT( 10 ) NOT NULL ,
and make sure your int column in the customers table is int(10) also.
Certain MySQL engines support foreign keys. For example, InnoDB can establish constraints based on foreign keys. If you try to delete an entry in one table that has dependents in another, the delete will fail.
If you are using a table type in MySQL, such as MyISAM, that doesn't support foreign keys, you don't link the tables anywhere except your diagrams and queries.
For example, in a query you link two tables in a select statement with a join:
SELECT a, b from table1 LEFT JOIN table2 USING (common_field);
Here are a couple of resources that will help get started: http://www.anchor.com.au/hosting/support/CreatingAQuickMySQLRelationalDatabase and http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561
Also as others said, use a GUI - try downloading and installing Xampp (or Wamp) which run server-software (Apache and mySQL) on your computer.
Then when you navigate to //localhost in a browser, select PHPMyAdmin to start working with a mySQL database visually. As mentioned above, used innoDB to allow you to make relationships as you requested. Makes it heaps easier to see what you're doing with the database tables. Just remember to STOP Apache and mySQL services when finished - these can open up ports which can expose you to hacking/malicious threats.
One of the rules you have to know is that the table column you want to reference to has to be with the same data type as
The referencing table . 2 if you decide to use mysql you have to use InnoDB Engine because according to your question that’s the engine which supports what you want to achieve in mysql .
Bellow is the code try it though the first people to answer this question
they 100% provided great answers and please consider them all .
CREATE TABLE accounts(
account_id INT NOT NULL AUTO_INCREMENT,
customer_id INT( 4 ) NOT NULL ,
account_type ENUM( 'savings', 'credit' ) NOT NULL,
balance FLOAT( 9 ) NOT NULL,
PRIMARY KEY (account_id)
)ENGINE=InnoDB;
CREATE TABLE customers(
customer_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
state VARCHAR(20) NOT NULL,
PRIMARY KEY ( account_id ),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
)ENGINE=InnoDB;
create table departement(
dep_id int primary key auto_increment,
dep_name varchar(100) not null,
dep_descriptin text,
dep_photo varchar(100) not null,
dep_video varchar(300) not null
);
create table newsfeeds(
news_id int primary key auto_increment,
news_title varchar(200) not null,
news_description text,
news_photo varchar(300) ,
news_date varchar(30) not null,
news_video varchar(300),
news_comment varchar(200),
news_departement int foreign key(dep_id) references departement(dep_id)
);