My Code:
create table Products
(
ProductID int not null auto_increment primary key,
ProductName varchar(20),
Recommendedprice decimal(10,2),
Category varchar(10)
)
create table customers
(
CustomerID int not null auto_increment primary key,
FirstName varchar(50),
LastName varchar(50),
city varchar(50),
State char(2),
zip varchar(10)
)
create table sales
(
SalesID int not null auto_increment primary key,
ProductID int,
CustomerID int,
CONSTRAINT fk_PerProducts FOREIGN KEY (ProductId)
REFERENCES Products(ProductId),
CONSTRAINT fk_PerCustomers FOREIGN KEY (CustomerId)
REFERENCES customers(customerId),
SalesPrice decimal(10,2),
SalesDate date
)
I get null values in the table list.
You need to send the id of the foreigner key when you make an INSERT.
like this (paper_Author work like your sales table) :
CREATE TABLE Paper (`paperId` int, `title` varchar(7));
INSERT INTO Paper (`paperId`, `title`)
VALUES (1, 'hello'),(2, 'hola'),(3, 'bonjour');
CREATE TABLE Author (`authorId` int, `authorName` varchar(3));
INSERT INTO Author (`authorId`, `authorName`)
VALUES (1, 'me'),(2, 'moi');
CREATE TABLE Paper_Author (`paperId` int, `authorId` int);
INSERT INTO Paper_Author (`paperId`, `authorId`)
VALUES (1, 1),(1, 2),(2, 2);
and you use it like this :
http://sqlfiddle.com/#!2/eb61f/2
other think you should read :
Basics of Foreign Keys in MySQL?
Why use Foreign Key constraints in MySQL?
Related
Good evening,
following table situation:
create table TableA (
pkA int not null auto_increment,
propertyA varchar(20),
primary key (pkA) );
create table TableB (
pkB int not null auto_increment,
pkA int not null,
propertyB varchar(20),
primary key (pkB),
foreign key (pkA) references TableA (pkA) on delete cascade);
create table TableC (
pkC int not null auto_increment,
pkB int not null,
propertyC varchar(20),
primary key (pkC),
foreign key (pkB) references TableB (pkB) on delete cascade );
When I delete a row from parent table 'TableA' I need to know which rows (primary keys) got deleted in 'TableB' and 'TableC' as well.
I am working with PHP 7.0. I was hoping there is something like mysqli_insert_id for deletes, but... nada :(
Any ideas?
My code runs in phpmyadmin but the foreign key values are not showing in table and when I update it the message shows "0 rows affected"
my table schema is:
CREATE TABLE fee_report (
fr_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
school_id INT NOT NULL,
branch_id INT NOT NULL,
student_id INT NOT NULL,
ledger VARCHAR(55),
bill_no VARCHAR(255) NOT NULL,
fdate date NOT NULL,
receipt_no VARCHAR(255) NOT NULL,
dr float NOT NULL,
cr float NOT NULL,
balance float NOT NULL,
particulars VARCHAR(155),
updated TIMESTAMP NOT NULL,
CONSTRAINT fk_frschool FOREIGN KEY (school_id)
REFERENCES school(school_id),
CONSTRAINT fk_frbranch FOREIGN KEY (branch_id)
REFERENCES branch(branch_id),
CONSTRAINT fk_frstudent FOREIGN KEY (student_id)
REFERENCES student(student_id)
) ENGINE = INNODB;
INSERT INTO fee_report(school_id,branch_id,student_id,ledger,bill_no,fdate,dr,balance,particulars,feetype)
VALUES(1,1,13,'TGS007','SESNOV1020',CURDATE(),'2850','-14450','11','Tuition Fee,Transport Fee,Smart Class Fee,SA-1 Exam Fee')
Why foreign keys school_id, branch_id and student_id are not showing in table
and when I remove foreign key constraint the values are visible in table
I have read and searched all the site, but nothing I had found worked for me...so please help a newbie understand what he is doing wrong.
So I am trying to create an add to favorite function.
I need to create 3 tables. The first two worked like magic, but the 3rd one ...well I got in the trouble with the FOREIGN KEY
I get no error message, but it won't create the 3rd table.
Here are my codes:
<?php
$connect = mysql_connect("127.0.0.1","root","");
$db = mysql_select_db("mydb");
mysql_query("CREATE TABLE IF NOT EXISTS users
(
userid bigint,
firstname varchar(25),
lastname varchar(15),
email varchar(250),
gender varchar(10),
username varchar(15),
password varchar(15),
age int,
activ boolean,
date TIMESTAMP NULL default CURRENT_TIMESTAMP
)ENGINE=INNODB
");
mysql_query("CREATE TABLE IF NOT EXISTS products (
productid int(11) NOT NULL AUTO_INCREMENT,
productname varchar(100) NOT NULL,
productdescription varchar(250) NOT NULL,
price decimal(6,2) NOT NULL,
PRIMARY KEY (`productid`)
) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ");
mysql_query("CREATE TABLE IF NOT EXISTS favorites
(
userid bigint NOT NULL,
productid int(11) NOT NULL,
PRIMARY KEY (userid, productid),
FOREIGN KEY (userid) REFERENCES user (userid) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (productid) REFERENCES product (productid) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=INNODB
");
echo "The DataBase was successfully created!";
mysql_close();
?>
The foreign keys refer to non-existing tables.
The tables are names users and products while the third table refers to them as user and product (singular).
"userid" of "user" table must be primary key.
I have 1 user and I want to insert more than one language(and degree) to him. How can I do this with php?
My languagetype table(all languages inserted)
CREATE TABLE TLANGUAGETYPE (
ID INT NOT NULL AUTO_INCREMENT,
language VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
)
My language table connected with user table.
CREATE TABLE TLANGUAGE (
languageID INT NOT NULL AUTO_INCREMENT,
userID INT NOT NULL,
ID INT NOT NULL,
degree FLOAT,
PRIMARY KEY (languageID),
FOREIGN KEY (userID) REFERENCES TUSER(userID),
FOREIGN KEY (ID) REFERENCES TLANGUAGETYPE(ID)
)
Instead of adding the languageId in user Table. You can create a mapping table which just contains the "USERID" and "LanguageID" columns.
The exact error I keep seeing is:
Key column 'alarmID' doesn't exist in table
alarmID is my primary key field.
Here is the code I have:
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alarmID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
Note: I am coding in PHP.
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
alaramID
The primary key in your table is alaramID and note the error its alarmID.So correct the spelling in the query like this
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";