I want to add more primary key into existing primary key - mysql

I wanted to set primary key by combining 3 columns but by mistake i set the key with one column. How should i add other two columns into existing primary keys.
CREATE TABLE facultyQualification(
facultyID CHAR(5) NOT NULL,
level_ VARCHAR(15) NOT NULL,
Exam_Degree VARCHAR(30) NOT NULL,
School_College VARCHAR(50) NOT NULL,
Board_Uni VARCHAR(30) NOT NULL,
year_of_passing DATE NOT NULL,
Max_marks_grades INT NOT NULL,
marks_grade_obtained INT NOT NULL,
perscent_marks INT NOT NULL,
division VARCHAR(10) NOT NULL,
achievement VARCHAR(50),
FOREIGN KEY(facultyID) REFERENCES facultyPersonal(facultyID)
);
SELECT * FROM facultyQualification;
ALTER TABLE facultyQualification
ADD PRIMARY KEY (facultyID);
The primary keys i wanted to make are (facultyID,level_,year_of_passing).

You can do:
alter table facultyQualification drop primary key;
alter table facultyQualification
add primary key (facultyID, level_, year_of_passing);
See example at DB Fiddle.

Related

Error Code 1824 - Failed to open the referenced table

I'm trying to create my DB project on MySQL, and when I tried to add foreign keys, it show the error code 1824 on all of the tables I'm trying to create. My code is this:
create table cliente (
id_cliente varchar(20) not null primary key,
nome varchar(50) unique not null,
endereco varchar(70) not null,
celular varchar(15) not null,
id_cartao_cliente int not null,
cod_encomenda_cliente int not null,
constraint fk_cartao foreign key (id_cartao_cliente) references cartao_credito(id_cartao),
constraint fk_encomenda foreign key (cod_encomenda_cliente) references encomenda(cod_encomenda)
) engine = innodb;
create table cartao_credito (
id_cartao int not null primary key,
num_cartao varchar(20) not null,
nome_cartao varchar(30) not null,
mes_validade varchar(2) not null,
ano_validade year,
id_cliente_cartao varchar(20) not null,
constraint fk_cliente foreign key (id_cliente_cartao) references cliente(id_cliente)
) engine = innodb;
create table Encomenda (
cod_encomenda int not null primary key,
tipo varchar(15) not null,
valor_encomenda varchar(10) not null,
local_destino varchar(20) not null,
id_destinatario_encomenda varchar(20) not null,
constraint fk_destinatario foreign key (id_destinatario_encomenda) references destinatario(id_destinatario)
) engine = innodb;
create table movimentacao (
id varchar(50) not null primary key,
local_movimentacao varchar(50) not null,
descricao varchar(30) not null,
data_movimentacao date not null,
hora_movimentacao time not null,
cod_encomenda_movimentacao int not null,
id_entregador_movimentacao varchar(20) not null,
foreign key (cod_encomenda_movimentacao) references encomenda(cod_encomenda),
foreign key (id_entregador_movimentacao) references entregador(id_entregador)
) engine = innodb;
create table entregador (
id_entregador varchar(20) not null primary key,
nome varchar(50) unique not null,
placa_veiculo varchar(10) not null,
tipo_veiculo varchar(15) not null,
cod_encomenda_entregador varchar(50) not null,
id_destinatario_entregador varchar(20) not null,
foreign key (cod_encomenda_entregador) references encomenda(cod_encomenda),
foreign key (id_destinatario_entregador) references destinatario(id_destinatario)
) engine = innodb;
create table destinatario (
id_destinatario varchar(20) not null primary key,
nome varchar(50) not null unique not null,
endereco varchar(70) not null,
celular varchar(15) not null,
cod_encomenda_destinatario int not null,
foreign key (cod_encomenda_destinatario) references encomenda(cod_encomenda)
) engine = innodb;
I need help with this code please.
P.S.: all of the columns are written in Portuguese
MySQL error code 1824 is "Failure to open referenced table."
I believe you are running into trouble because of the order you are creating in. I see that "Encomenda" does not have a foreign key, and may be a good place to start your creation.
Your design is wrong. Because when an entity in cliente refer to cartao_credito it means there is a (1 to N) relation between these two table. So no need to refer cartao_credito to cliente again. If you need an N to N relationship use a bridge (a new table) between them and use two 1 to N relationship.
In your design all tables refer to each other mutually. For example in two first table you need just one foreign key to join these two tables not two foreign key.

Trigger for last inserted value

can anyone help me create a trigger that appends the last record from table Sensordata into Lastsensordata. I only want 1 value per ConnectionDeviceId and the value needs to be the last inserted one. (its going to be used to show in a gauge). I will link my sql script under.
CREATE TABLE Revpi (
revpiName varchar(100) NOT NULL,
revpiDateTimeCreated datetime NOT NULL,
revpiLocation varchar(150) NOT NULL,
CONSTRAINT PK_revpiNavn PRIMARY KEY (revpiName)
);
CREATE TABLE Sensor (
sensorName varchar(100) NOT NULL,
revpiName varchar(100) NOT NULL,
sensorDateTimeCreated datetime NOT NULL,
CONSTRAINT PK_sensorNavn PRIMARY KEY (sensorName),
CONSTRAINT FK_revpiName FOREIGN KEY (revpiName) REFERENCES Revpi (revpiName)
);
CREATE TABLE Sensordata (
SensordataID int NOT NULL AUTO_INCREMENT,
data varchar (500),
ConnectionDeviceId varchar(500) NOT NULL,
EventProcessedUtcTime varchar(50) NOT NULL,
CONSTRAINT PK_sensorDataNavn PRIMARY KEY (SensordataID),
CONSTRAINT FK_sensorNavn FOREIGN KEY (ConnectionDeviceId) REFERENCES Sensor (sensorName)
);
CREATE TABLE Lastsensordata (
lastSensorNavn varchar(255) NOT NULL,
lastData varchar (500) NOT NULL,
LastTime varchar(50) NOT NULL,
CONSTRAINT PK_Lastsensordata PRIMARY KEY (lastSensorNavn),
CONSTRAINT FK_LastensordataSensordata FOREIGN KEY (lastSensorNavn) REFERENCES Sensordata (ConnectionDeviceId)
);
You can do it with the following trigger:
CREATE TRIGGER after_insert_sensordata
AFTER INSERT ON Sensordata
FOR EACH ROW
REPLACE INTO Lastsensordata VALUES (NEW.ConnectionDeviceId, NEW.`data`, NEW.EventProcessedUtcTime);
You will need to make the lastSensorNavn field unique. You should also fix the varchar size discrepancy between lastsensornavn and connectiondeviceid. The fixed table would look like:
CREATE TABLE Lastsensordata (
lastSensorNavn varchar(500) UNIQUE NOT NULL,
lastData varchar (500) NOT NULL,
LastTime varchar(50) NOT NULL,
CONSTRAINT PK_Lastsensordata PRIMARY KEY (lastSensorNavn),
CONSTRAINT FK_LastensordataSensordata FOREIGN KEY (lastSensorNavn) REFERENCES Sensordata (ConnectionDeviceId)
);

Single attribute used as a foriegn key for multiple tables

I want to make Emp_id as a foreign key for Employee table and Record table. Can this be done?
This is my code
CREATE TABLE Empolyee(
EID varchar(8) NOT NULL,
E_name varchar(30) NOT NULL,
NID varchar(30) NOT NULL,
sex varchar(40) NOT NULL,
history varchar(30) NOT NULL,
salary varchar(10) NOT NULL,
cid int NOT NULL,
FOREIGN KEY (cid) REFERENCES Employee_Contact_No(CID),
FOREIGN KEY (cid) REFERENCES Employee_email(Email_ID),
PRIMARY KEY (EID)
);
Receptionist table
CREATE TABLE Receptionist(
r_id varchar(8) NOT NULL,
Emp_ID varchar(8) NOT NULL,
PRIMARY KEY (r_id),
FOREIGN KEY (Emp_ID) REFERENCES Employee(EID),
FOREIGN KEY (Emp_ID) REFERENCES Record(EID)
);
And the receptionist's records
CREATE TABLE Record(
record_no varchar(8) NOT NULL,
EID VARCHAR(8) Not Null,
patient_id varchar(15) NOT NULL,
discription varchar(30) NOT NULL,
appointment varchar(40) NOT NULL,
PRIMARY KEY (record_no)
);
You can make a single field reference multiple tables, but I cannot think of a scenario where it would ever be a good idea; it would require both tables to have the same id value, and (at least imply) that those id values be meaningfully coordinated.
From the looks of your examples, it is probably more likely that you need tables like Employee_Contact_No and Employee_email to reference the Employee table.

trying to relate two table together

so pretty new to SQL I created 2 tables which I wanted to be related to one another but I'm getting an error "#1215 - Cannot add foreign key constraint" can someone point me to the right direction of this problem?
CREATE TABLE movie(
id INT(1) NOT NULL AUTO_INCREMENT,
nearname VARCHAR(25) NOT NULL,
release_date DATE NOT NULL,
lang VARCHAR(10) NOT NULL,
PRIMARY KEY(id),
CONSTRAINT same_movie FOREIGN KEY(id) REFERENCES movie_cast(movie_id)
);
CREATE TABLE movie_cast(
movie_id INT(1) NOT NULL AUTO_INCREMENT,
director_name VARCHAR(20) NOT NULL,
actor_name VARCHAR(20) NOT NULL,
actress_name VARCHAR(20) NOT NULL,
PRIMARY KEY(movie_id),
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie(id)
);
You need to refer to the same column name as the primary key. In this case, it is called id:
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie_cast(id)
Of course, your DDL doesn't define movie_cast. So, I am guessing the second table should be something like:
CREATE TABLE movie_cast (
id INT NOT NULL AUTO_INCREMENT,
movie_id int not null,
cast_name varchar(255)
PRIMARY KEY(id),
CONSTRAINT fk_movie_cast_movie FOREIGN KEY(movie_id) REFERENCES movie(movie_id)
);

How to create table in mysql database with foreign key which is primary key of another table?

CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id),
);
This is my first table(employee_login)and I want e_id as a foreign key in my next table (login) below
CREATE TABLE login(
login_id int auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (login_id),
e_id references employee_detail(e_id)
);
You can do it like follows :
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
I hope you understand the create table code. owner in the shirt table is the foreign key referencing id from the person table
You have several mistakes. You were missing e_id column in your Login Table, you can't add a foreign key without adding employee_detail's primary key column which in this case is e_id. Also you can't use password as a column name since it is a preestablished query you'll need to use something else like "pass".
CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id));
CREATE TABLE login(
login_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
e_id INT,
FOREIGN KEY (e_id) REFERENCES employee_detail(e_id)
);