Database functional dependencies - mysql

Im trying to find some functional dependencies in my database but have no idea how to do it. Would anyone be able to explain how to do it or what the functional dependencies are . Heres my code for creating my tables
CREATE TABLE Customer(
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber varchar(128) NOT NULL,
UserEmail varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS OrderMaster(
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE OrderItem(
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Product(
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL,
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS SupplySchedule(
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS Supplier(
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

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.

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

Receiving a syntax error for this SQL code

Syntax error:-
CREATE TABLE CUSTOMER (
CustomerID Int NOT NULL IDENTITY(1,1) PRIMARY KEY,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL,
CONSTRAINT CustomerPK PRIMARY KEY(CustomerID)
);
In mysql there is nothing named identity, instead there is AUTO_INCREMENT
CREATE TABLE CUSTOMER (
CustomerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL
);
u have to declare primary key only once
CREATE TABLE CUSTOMER (
CustomerID INT NOT NULL AUTO_INCREMENT,
CustomerName Char(25) NOT NULL,
CustomerDeliveryAddress Char(25) NOT NULL,
CustomerPhone Char (10) NOT NULL,
CustomerBillingAddress Char(25) NOT NULL,
CustomerCreditCard Int NOT NULL,
PRIMARY KEY(CustomerID));

SQL beginner error creating a table

So im new to sql and im trying to create a series of tables for cant seem to get it to work. The error message that I get is.
The tables all add in individually no problem but when I want to import all of them at once it gives me this error.
Schema Creation Failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Customer](
CustomerID int PRIMARY KEY,
CustomerName varchar(' at line 1:
CREATE TABLE IF NOT EXISTS [Customer](
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber int default NULL,
User_Email varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Order](
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [OrderItem](
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Product](
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [SupplySchedule](
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS [Supplier](
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
order is a reserved word in MySQL. Either use backticks to escape it or use another name.
CREATE TABLE IF NOT EXISTS `Order` (...
I have rename Order table to OrderMaster as "Order" is mysql reserved word
try below script:
drop table if exists Customer;
CREATE TABLE Customer(
CustomerID int PRIMARY KEY,
CustomerName varchar(50) NOT NULL,
BillingAddress varchar(400) default NULL,
PhoneNumber int default NULL,
User_Email varchar(128) UNIQUE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists OrderMaster;
CREATE TABLE IF NOT EXISTS OrderMaster(
OrderID int NOT NULL PRIMARY KEY,
CustomerID int,
OrderDate date NOT NULL,
PurchaseOrderNumber int
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists OrderItem;
CREATE TABLE OrderItem(
OrderItemID int PRIMARY KEY,
ProductID int NOT NULL,
OrderID int NOT NULL,
Quantity int NOT NULL,
PricePerUnit double NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists Product;
CREATE TABLE IF NOT EXISTS Product(
ProductID int PRIMARY KEY,
ProductName varchar(100) NOT NULL,
ProductDescription varchar(100) NOT NULL,
SerialNumber int NOT NULL,
UnitesInStock int NOT NULL,
UnitsOnOrder int NOT NULL,
UnitPrice double NOT NULL,
SupplierID int not NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists SupplySchedule;
CREATE TABLE IF NOT EXISTS SupplySchedule(
ShippingID int PRIMARY KEY,
ShippingMode varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
drop table if exists Supplier;
CREATE TABLE IF NOT EXISTS Supplier(
SupplierID int PRIMARY KEY,
SupplierName varchar(40) NOT NULL,
SupplierAddress varchar(128) NOT NULL,
ContactName varchar(128) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

#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)