How do I declare a multi-column PK in MySQL - mysql

I'm trying to create a table with two columns comprising the primary key in MySQL, but I can't figure out the syntax. I understand single-column PKs, but the syntax isn't the same to create a primary key with two columns.

CREATE TABLE table_name
(
c1 INT NOT NULL,
c2 INT NOT NULL,
PRIMARY KEY (c1, c2)
)

Try:
create table .....
primary key (`id1`, `id2`)
)

Example:
CREATE TABLE `synthesis`.`INV_MasterItemList` (
`MasterItemList_ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`Customer_ID` INTEGER UNSIGNED NOT NULL,
`Model_ID` INTEGER UNSIGNED NOT NULL,
`Serial` VARCHAR(45) NOT NULL,
PRIMARY KEY (`MasterItemList_ID`),
UNIQUE INDEX `INDEX_UNIQUE`(`Customer_ID`, `Model_ID`, `Serial`)
)

An example (from osCommerce) :
CREATE TABLE categories_description (
categories_id int DEFAULT '0' NOT NULL,
language_id int DEFAULT '1' NOT NULL,
categories_name varchar(32) NOT NULL,
PRIMARY KEY (categories_id, language_id),
KEY idx_categories_name (categories_name)
);

Related

Failed to add the foreign key constraint, MySQL

I have this error on PopSQL :
Error: Failed to add the foreign key constraint. Missing index for constraint 'etape__ibfk_1' in the referenced table 'etapexprojet_'
Error Code: ER_FK_NO_INDEX_PARENT
This is my code, i'm only creating tables and it has a problem in the "Etape_" part, i really, don't know why
I'm really, really new to SQL. Like... this is my first time doing this
--Client
CREATE TABLE IF NOT EXISTS jegere.client_ (
idClient INT PRIMARY KEY,
nomClient VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Employés
CREATE TABLE IF NOT EXISTS jegere.Employe_ (
idEmploye INT PRIMARY KEY,
nomEmploye VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Projet
CREATE TABLE IF NOT EXISTS jegere.Projet_ (
idProjet INT PRIMARY KEY,
idClient INT ,
nomProjet VARCHAR(45) NOT NULL ,
dateDebut DATE NOT NULL ,
dateFin DATE,
idResponsable INT NOT NULL ,
FOREIGN KEY (idClient ) REFERENCES jegere.Client_ (idClient ),
FOREIGN KEY (idResponsable ) REFERENCES jegere.Employe_ (idEmploye )
);
--RessourcesProjet
CREATE TABLE IF NOT EXISTS jegere.RessourcesProjet_ (
idProjet INT NOT NULL,
idEmploye INT NOT NULL,
nbrHeure INT NOT NULL,
PrixHeure FLOAT NOT NULL,
PRIMARY KEY(idProjet, idEmploye),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet),
FOREIGN KEY(idEmploye) REFERENCES employe_(idEmploye)
);
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
--Etapes
CREATE TABLE IF NOT EXISTS jegere.Etape_ (
idEtape INT,
nomEtape VARCHAR(45) NOT NULL,
Livrable VARCHAR(100) NOT NULL,
PRIMARY KEY(idEtape),
FOREIGN KEY(idEtape) REFERENCES etapexprojet_(idEtape)
);
The referenced column in a foreign key needs to be indexed, so you need to add an index on the idEtape column in etapexprojet:
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
INDEX (idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
Having it as part of the primary key isn't sufficient. A prefix of an index is also an index, but idEtape is a later part of the primary key index, so it's not indexed by itself.
Another solution is to change the order of the primary key:
PRIMARY KEY(idEtape, idProject),
Your last table etape references only the column idetape, but all referenced columns need an index , primary key or UNIQUE constraint.
so add a KEY to your EtapexProjet_ for the column idetape, and you can run the code
CREATE TABLE IF NOT EXISTS EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
KEY(idEtape),
FOREIGN KEY(idProjet) REFERENCES Projet_(idProjet)
);
see https://dbfiddle.uk/z9QFoH0c

Add a foregin key to a different table

I'm trying to add username from my st_accounts column into my table results using mySQL.
st_accounts table
`id` int(11) NOT NULL, //Primary Key
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
results table
`score_ID` int(11) NOT NULL, //Primary key
`score` int(20) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want:
create table accounts (
account_id int auto_increment primary key,
. . .
);
create table results (
result_id int auto_increment primary key,
account_id int not null
score int NOT NULL,
score_date date NOT NULL,
constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id)
) ;
Notes:
Declare the primary key explicitly, not in a comment.
My convention is to name the primary key after the name of the table (in singular) with _id after it.
The foreign key has the same name as the primary key -- self documenting.
I made the integer primary keys auto_increment, so the database can assign unique values.

MYSQL 1215 Cannot add Foreign Key Constraing

Problem is on the bounding table column GroupDim.FacultyID and FacultyDim.FacultyID, data types are identity BIGINT This data store for OLAP cube wherein Rating_facts stores result of question which i exploring. table without signature Dim is transitional and serve for additional data for analysis.Maybe it is needs to divide this table?
DROP TABLE IF EXISTS BlockDim;
CREATE TABLE BlockDim (
BlockKey bigint NOT NULL AUTO_INCREMENT,
BlockID bigint NOT NULL,
BlockName varchar(32) NOT NULL,
PRIMARY KEY (BlockKey)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS FacultyDim;
CREATE TABLE FacultyDim (
FacultyKey bigint NOT NULL AUTO_INCREMENT,
FacultyID bigint NOT NULL,
FacultyName varchar(32) NOT NULL,
PRIMARY KEY (FacultyKey, FacultyID)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS `Group`;
CREATE TABLE `Group` (
GroupID bigint NOT NULL,
FacultyID bigint NOT NULL,
GroupName varchar(32) NOT NULL,
PRIMARY KEY (GroupID),
FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS Subblock;
CREATE TABLE Subblock (
SubblockKey bigint NOT NULL AUTO_INCREMENT,
SubblockID bigint NOT NULL,
BlockID bigint NOT NULL,
SubblockName varchar(32) NOT NULL,
PRIMARY KEY (SubblockKey)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS Paragraph;
CREATE TABLE Paragraph (
ParagraphKey bigint NOT NULL AUTO_INCREMENT,
ParagraphID bigint NOT NULL,
SubblockID bigint NOT NULL,
ParagraphName varchar(64) NOT NULL,
PRIMARY KEY (ParagraphKey)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS RatingDim;
CREATE TABLE RatingDim (
RatingKey bigint NOT NULL AUTO_INCREMENT,
RatingID bigint NOT NULL,
ParagraphID bigint NOT NULL,
Score double NOT NULL,
StageOfApprove int NOT NULL,
Comment varchar(128) NOT NULL,
`Date` DATE NOT NULL,
PRIMARY KEY (RatingKey)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS TimeDim;
CREATE TABLE TimeDim (
TimeKey bigint NOT NULL AUTO_INCREMENT,
`Date` DATE NOT NULL,
DayOfWeek varchar(16) NOT NULL,
`Month` varchar(16) NOT NULL,
`Year` varchar(4) NOT NULL,
PRIMARY KEY (`TimeKey`)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS Rating_Fact;
CREATE TABLE Rating_Fact (
TimeKey bigint NOT NULL,
BlockKey bigint NOT NULL,
FacultyKey bigint NOT NULL,
RatingKey bigint NOT NULL,
PopularBlock bigint NOT NULL,
AvgFacultyScore bigint NOT NULL,
StudentsMaxRating bigint NOT NULL,
StudentsNotApprovedRating bigint NOT NULL,
PRIMARY KEY (TimeKey,BlockKey,FacultyKey,RatingKey),
FOREIGN KEY (TimeKey) REFERENCES TimeDim(TimeKey),
FOREIGN KEY (BlockKey) REFERENCES BlockDim(BlockKey),
FOREIGN KEY (FacultyKey) REFERENCES FacultyDim(FacultyKey),
FOREIGN KEY (RatingKey) REFERENCES RatingDim(RatingKey)
)ENGINE=InnoDB;
First you have check your table architecture.then try below two solutions
Solution 1:-
DROP TABLE IF EXISTS FacultyDim;
CREATE TABLE FacultyDim (
FacultyKey bigint NOT NULL AUTO_INCREMENT,
FacultyID bigint NOT NULL,
FacultyName varchar(32) NOT NULL,
PRIMARY KEY (FacultyKey, FacultyID)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS `Group`;
CREATE TABLE `Group` (
GroupID bigint NOT NULL,
FacultyID bigint NOT NULL,
GroupName varchar(32) NOT NULL,
PRIMARY KEY (GroupID),
FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID,FacultyKey)
)ENGINE=InnoDB;
Solution 2:-
DROP TABLE IF EXISTS FacultyDim;
CREATE TABLE FacultyDim (
FacultyKey bigint NOT NULL AUTO_INCREMENT,
FacultyID bigint NOT NULL,
FacultyName varchar(32) NOT NULL,
PRIMARY KEY (FacultyID)
)ENGINE=InnoDB;
DROP TABLE IF EXISTS `Group`;
CREATE TABLE `Group` (
GroupID bigint NOT NULL,
FacultyID bigint NOT NULL,
GroupName varchar(32) NOT NULL,
PRIMARY KEY (GroupID),
FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID)
)ENGINE=InnoDB;

how to make one coulmn as auto increment other can primary key in the same table

i have a table named as tbl_bank, in that i have created 4 columns as id,acc_name,acc_type,bank_name.
I need id as autoincrement and acc_name as primary key. What is the solution?
Any idea?
you should try this:
CREATE TABLE `tbl_bank` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`acc_name` varchar(55) NOT NULL,
`acc_type` varchar(55) NOT NULL,
`bank_name` varchar(55) NOT NULL,
PRIMARY KEY (`acc_name`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Try this
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
Acc_name varchar(255) NOT NULL PRIMARY KEY,
acc_type varchar(255),
bank_name varchar(255),
UNIQUE KEY id (id)
)
SQL FIDDLE
create table tbl_bank(
id int unique auto_increment,
acc_name varchar(20) not null primary key,
acc_type varchar(10),
bank_name varchar(20));
for further query visit kuchhbhiii.blogspot.in
While creating table, use following
ID int NOT NULL AUTO_INCREMENT

#1005 - Can't create table 'workswell_database.skoleni' Novice user with MySQL

sorry for my stupid question,but I do database in MySQL and was so happy,when i finished.Unfortunately,I had found a lot of mistakes in my database n when i will properly fix a bug,so i've another. Also,this is my Database and that guy what really boring right now,please write about my bugs and solution for them.I novice as a programmer and this database i do about 4 days !
CREATE TABLE`Skoleni` (
`sk_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Cena` MEDIUMINT NOT NULL,
`Obsazenost` text NOT NULL,
`Kapacita` datetime NOT NULL,
`Popis` text,
`Prerekvizity` text,
`Certifikat` MEDIUMINT NOT NULL,
PRIMARY KEY (`sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Termin(`Skoleni_sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Uzivatel(`Skoleni_sk_id`)
);
CREATE TABLE `Skoleni_has_Termin`(
`Skoleni_sk_id`INT NOT NULL UNIQUE,
`Termin_ter_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Termin` (
`ter_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Datum_cas` DATETIME NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`ter_id`),
FOREIGN KEY (`ter_id`) REFERENCES Skoleni_has_Termin(`Termin_ter_id`)
);
CREATE TABLE `Misto` (
`mo_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`ulice` MEDIUMINT NOT NULL,
`cislo_popisne` MEDIUMINT NOT NULL,
`lat` FLOAT (10,6) NOT NULL,
`lng` FLOAT (10,6) NOT NULL,
PRIMARY KEY (`mo_id`)
)ENGINE = MYISAM;
SELECT TABLE.Misto(
INSERT (`ulice`, `cislo_popisne`, `lat`, `lng`),
VALUES (`dr_Zikmunda_Wintra_376_5``16000 Praha 6 Bubenec``14.407438``50.101049`)
);
CREATE TABLE `Skoleni_has_Uzivatel` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Uzivatel_uziv_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Uzivatel` (
`uziv_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Jmeno` VARCHAR(30) NOT NULL,
`Typ` MEDIUMINT UNIQUE,
`Heslo` VARCHAR(32) NOT NULL,
`Potvrzeni` VARCHAR(1) NOT NULL,
PRIMARY KEY (`uziv_id`),
FOREIGN KEY (`uziv_id`) REFERENCES Skoleni_has_Uzivatel(`Uzivatel_uziv_id`)
);
CREATE TABLE `Skoleni_has_Lektor` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Lektor_lek_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Lektor_lek_id`)
);
CREATE TABLE `Lektor` (
`lek_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Titul'pred'` VARCHAR(10) NOT NULL,
`Jmeno` VARCHAR(20) NOT NULL,
`Prijmeni` VARCHAR(20) NOT NULL,
`Titul'za'` VARCHAR(10),
`Firma` VARCHAR(30),
`Rodne cislo` CHAR(11) NOT NULL,
`Datum narozeni` DATE NOT NULL,
PRIMARY KEY (`lek_id`)
);
CREATE TABLE `Firma` (
`fir_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`E-mail` VARCHAR(15) NOT NULL,
`Telefon` VARCHAR(15) NOT NULL,
`Web` VARCHAR(30),
`IC` CHAR(8) NOT NULL,
`DIC` VARCHAR(12) NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`fir_id`),
PRIMARY KEY (`Misto_mo_id`),
FOREIGN KEY (`Misto_mo_id`) REFERENCES Firma(`Misto_mo_id`)
);
The tables `skoleni_has_termin' and 'skoleni_has_lektor' are most likely not needed here. I would bring dates to trainings table and leave just lektor_id in skoleni table as relation between trainings and lecturers is 1:n not m:n (unless one training can have more than one lecturer)