Relational database question with php
Not really a coding question more a little help with my idea of a Relational database.
If I have 3 tables in a SQL database. In my php script I basically query the companies which are in industry "a" and 开发者_如何学Gothen insert a row into a separate table with their details such as companyId , companyName etc is that a type of Relational database ?
I have explained it in a simple way so we don't get confused what I am trying to say.
You're on the right track.
What you are describing is known as the concept of database normalization, where separate (and unique) forms of data are divided into entities (in your case, an industry and a company) that are related in some way.
And, furthermore, the proper name for that relationship is "one-to-many," where one entity has many entities that belong to it (one industry has many companies).
is that a type of Relational database ?
From what I understood ... Table Industry has many Companies. So this IS a relational database.
Here is some SQL that you could use to create the tables to store the companies and industries
CREATE TABLE `industry`
(
`ind_id` INTEGER AUTO_INCREMENT,
`ind_name` VARCHAR(255),
PRIMARY KEY (`ind_id`)
);
CREATE TABLE `company`
(
`com_id` INTEGER AUTO_INCREMENT,
`com_name` VARCHAR(255),
`com_address` VARCHAR(255),
`com_city` VARCHAR(255),
`ind_id` INTEGER,
PRIMARY KEY (`com_id`)
);
Optionally you could create on index to enforce the relational integrity
CREATE INDEX `company_ind_id_idxfk` ON `company`(`ind_id`);
ALTER TABLE `company` ADD FOREIGN KEY ind_id_idxfk (`ind_id`) REFERENCES `industry` (`ind_id`);
精彩评论