I have a question about my database.
I try to delete my database but I understood that we need to clear the tables in a certain order.
I want to know how to do it with my database. In which order to enter my tables in my SQL file. I heard about the ON CASCADE DELETE thing and I don’t know either how to add records in my database (in order to be correct)
So, my question is : How can I delete, insert, update records in my database dynamically (not manually in PHPMYADMIN)
I know it’s much information I asked.
Here’s my code to help:
DROP TABLE IF EXISTS Panier;
DROP TABLE IF EXISTS Materiel;
DROP TABLE IF EXISTS Medicament;
DROP TABLE IF EXISTS ListePatient;
DROP TABLE IF EXISTS Patient;
DROP TABLE IF EXISTS Specialiste;
DROP TABLE IF EXISTS TelPers;
DROP TABLE IF EXISTS Personne;
DROP TABLE IF EXISTS Produit;
DROP TABLE IF EXISTS Telephone;
DROP TABLE IF EXISTS Adresse;
CREATE TABLE Adresse(
idAdresse INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
num INT(5) NOT NULL,
rue VARCHAR(30) NOT NULL,
ville VARCHAR(15) NOT NULL,
postal VARCHAR(6) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Telephone(
idTel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
typeTel VARCHAR(15) NOT NULL,
ind INT(3) NOT NULL,
quartier INT(3) NOT NULL,
num INT(4) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Personne(
idPersonne INT(100) PRIMARY KEY NOT NULL AUTO_INCREMENT,
nom VARCHAR(15) NOT NULL,
prenom VARCHAR(15) NOT NULL,
idTel INT(100) NOT NULL,
idAdresse INT(100) NOT NULL,
FOREIGN KEY(idAdresse) REFERENCES Adresse(idAdresse),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE TelPers(
idPersonne INT(100)NOT NULL,
idTel INT(100)NOT NULL,
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE Specialiste(
login VARCHAR(10) PRIMARY KEY NOT NULL,
password VARCHAR(10) NOT NULL,
profession VARCHAR(20) NOT NULL,
idListeP INT(5),
idPanier INT(5),
idPersonne INT(100),
INDEX(idListeP),
INDEX(idPanier),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE Patient(
idPatient INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
sexe CHAR NOT NULL,
anniv DATE,
assurance INT(3) NOT NULL,
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE ListePatient(
idListeP INT(5) NOT NULL,
idPatient INT(10)NOT NULL,
PRIMARY KEY (`idPatient`,`idListeP`),
FOREIGN KEY(idListeP) REFERENCES Specialiste(idListeP),
FOREIGN KEY(idPatient) REFERENCES Patient(idPatient)
)ENGINE=InnoDB;
CREATE TABLE Produit(
idProduit INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(50) NOT NULL,
descr VARCHAR(255) NOT NULL,
prix DECIMAL(5,2) NOT NULL,
qte INT(100) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Medicament(
idMedic INT(100)NOT NULL PRIMARY KEY AUTO_INCREMENT,
marque VARCHAR(10) NOT NULL,
typeMed VARCHAR(10) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Materiel(
idMateriel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
rabais INT(99) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Panier(
idPanier INT(5) NOT NULL,
idProduit INT(100) NOT NULL,
PRIMARY KEY (`idPanier`,`idProduit`),
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
If you want more precision, I delete all my records in my database manually and I tried to reimport those data (SQL FILE) in the database and it shoots me an Error:
-- Dumping data for table `adresse`
--
INSERT INTO `adresse` ( `idAdresse` , `num` , `rue` , `ville` , `postal` )
VALUES ( 1, 4256, 'de la Vallee', 'Laval', 'H8A1J7' ) , ( 2, 121, 'du Coin', 'Laval', 'N6A1B8' ) , ( 3, 31, 'rue Galt', 'Montreal', 'A3B6N8' ) , ( 4, 5, 'rue du Ranch', 'Laval', 'D1C0V8' ) ;
MySQL said: Documentation
#1062 - Duplicate entry '1' for key 'PRIMARY'
If you want to delete entire database just do
DROP DATABASE yourDatabaseName;If on the other hand, you want to make sure, that if you delete a row from a parent table (for example
Addresse) then rows from referencing tables (Personne,Specialiste) are deleted as well, then you need to modify your foreign key definitions(and similarly in other tables)
See more here: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
and be very, very careful, so that you don’t delete more than you want
Here’s a working example: