is there any possibility to create table column base on another table column value?
existing table(geozone) would look like this and it is not fixed(can contain more id and name)
id | name
1 | UK
2 | CANADA
3 | JAPAN
and i am trying to create new one from php page
mysql_query("CREATE TABLE shipping (
`id` int(11) NOT NULL auto_increment,
`product_id` int(11) NOT NULL,
`shipping_cost` decimal(15,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)");
the query above create shipping table successfully but that's not what i need, how can i create shipping_cost column has an id of geozone id? example: shipping_cost_1, shipping_cost_2 and shipping_cost_3
It sounds like the shipping cost depends on the product and the geozone it's sent to, which means a geozone_id column needs to be added to your shipping_cost table. Also add a unique constraint on (geozone_id,product_id) because each unique pair should have only one shipping cost.
CREATE TABLE shipping (
`id` int(11) NOT NULL auto_increment,
`geozone_id` int(11) NOT NULL, -- specify which geozone this cost is for
`product_id` int(11) NOT NULL,
`shipping_cost` decimal(15,2) NOT NULL,
PRIMARY KEY (`id`),
-- UNIQUE KEY `id` (`id`), -- Not necessary because Primary keys are already unique
UNIQUE KEY `product_id_geozone_id` (`product_id`,`geozone_id`) -- each product, geozone can only have 1 cost
)
Then you can select the cost for each product/geozone pair with a join:
select geozone.name, product.name,
shipping.shipping_cost
from products
join shipping on shipping.product_id = product.id
join geozone on shipping.geozone_id = geozone.id
Related
How can I combine these queries into a single query with where clause from another parent table? Please consider my SQL code and suggest a better method to work with
//look my code
CREATE TABLE IF NOT EXISTS first (
fid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
p_name varchar(60) NOT NULL
);
CREATE TABLE IF NOT EXISTS second (
sed int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
firstname varchar(20) NOT NULL,
fid int(11) NOT NULL,
FOREIGN KEY (fid) REFERENCES first(fid)
);
CREATE TABLE IF NOT EXISTS third (
thid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
start_date date NOT NULL,
end_date date NOT NULL,
sed int(11) NOT NULL,
FOREIGN KEY (sed) REFERENCES second(sed),
fid int(11) NOT NULL,
FOREIGN KEY (fid) REFERENCES first(fid)
);
CREATE TABLE IF NOT EXISTS fourth (
fid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
start_date date NOT NULL,
end_date date NOT NULL,
sed int(11) NOT NULL,
FOREIGN KEY (sed) REFERENCES second(sed),
fid int(11) NOT NULL,
FOREIGN KEY (fid) REFERENCES first(fid)
);
CREATE TABLE IF NOT EXISTS fifth (
fiid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
start_date date NOT NULL,
end_date date NOT NULL,
sed int(11) NOT NULL,
FOREIGN KEY (sed) REFERENCES second(sed),
fid int(11) NOT NULL,
FOREIGN KEY (fid) REFERENCES first(fid)
);
CREATE TABLE IF NOT EXISTS sixth (
sid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
start_date date NOT NULL,
end_date date NOT NULL,
sed int(11) NOT NULL,
FOREIGN KEY (sed) REFERENCES second(sed),
fid int(11) NOT NULL,
FOREIGN KEY (fid) REFERENCES first(fid)
);
//As you can see above, I want to create a single query to query all data at the samee time i.e
//All table from third table depend on first and second table, but the second table have column firstname and the first table has the p_name column
//I want
SELECT second.*, third.* FROM second INNER JOIN third ON third.sed = second.sed
SELECT second.*, fourth.* FROM second INNER JOIN fourth ON fourth.sed = second.sed
SELECT second.*, fifth.* FROM second INNER JOIN fifth ON fifth.sed = second.sed
SELECT second.*, sixth.* FROM second INNER JOIN sixth ON sixth.sed = second.sed
....WHERE fid = 1;
I want to combine these queries into a single query ie, $newqueries = '.....';
The concept
The second table is used to carry all details, ie student details, but the third to sixth tables are tables with few different details but they took all other details from the second table, ie a student can be a chairman, secretary and vice secretary but not all students so that I classified them in third to sixth table. The first table used to keep few info about i.e classes so I want to differentiate chairman etc base on class tables but all of them are students
In short
A chairman, secretary and vice secretary are students but not all students have these role in a class but we have more than one classes, how to differentiate these leaders based on class
in a single query
You can use left join
SELECT second.*, third.*,fourth.*,fifth.*,sixth.* FROM second
LEFT JOIN third ON third.sed = second.sed
LEFT JOIN fourth ON fourth.sed = second.sed
LEFT JOIN fifth ON fifth.sed = second.sed
LEFT JOIN sixth ON sixth.sed = second.sed
WHERE second.fid = 1;
I assume that if student is chairman then there will be an entry of that student in third table. Above query will return null if the student is normal student. You can use CASE Statement if you want role as well. For example,
CASE WHEN third.startdate IS NULL THEN '' ELSE 'Chairman' END
Need to set a unique key by combining two column in mysql. For example
id Ticket_number Code Name
1 5 123 a
2 5 89 b
3 2 89 a
4 8 123 c
Here I want to set the Ticket_number and Code as unique by combining both. i.e error should be occur only if the same combination occurs again. Ticket Number and code can be duplicated but the combination should not be duplicated. How is it possible. Please help me.
Here is the create statement:
CREATE TABLE arlog (
id int(11) NOT NULL AUTO_INCREMENT,
Ticket_Number varchar(40) NOT NULL,
Code varchar(10) NOT NULL,
Name varchar(100) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY Ticket_Number (Ticket_Number)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Create a unique index:
create unique index idx_table_ticket_code on table(ticket_number, code)
use this code to create table
CREATE TABLE ticket(
id INT,
ticket_number INT,
code INT,
PRIMARY KEY (id)
)
CREATE UNIQUE INDEX index_name ON ticket(id, ticket_number, code);
which will act as a composite key.
follow as given
CREATE TABLE tablename (
id INT,
user_id INT,
setting_id INT,
NAME VARCHAR(255)
);
CREATE UNIQUE INDEX index_name ON tablename(user_id, setting_id);
INSERT INTO tablename (NAME,user_id,setting_id) VALUES ('1',34,15)
ON DUPLICATE KEY UPDATE NAME = '1', user_id = 34, setting_id = 15;
traffic_hit_reports is a table name, ALTER TABLE change the table schema, ADD UNIQUE add the unique constraint and content_id,hit_date is a field name.
Now,
ALTER TABLE `traffic_hit_reports` ADD UNIQUE `unique_index`(`content_id`,`hit_date`);
Result:
I have a table that contains millions of sales records and looks like this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NULL);
The first three columns are populated with data. I would like to insert data into the company_id column that will identify each company with an auto_incremented integer. I plan to use the company_id field as a foreign key referencing another table that will contain each company's details. Many companies have multiple transactions, so the code needs to assign the same company_id to each row in the sales table with a matching company_name.
Is there a way to do this using only MySQL?
First, I'd recommend creating the company table:
CREATE TABLE company (
company_id INT NOT NULL AUTO_INCREMENT,
company_name VARCHAR(45),
PRIMARY KEY(company_id));
Then insert the companies from your sales data:
INSERT INTO company (company_name)
SELECT distinct company_name
FROM sales;
Finally, update your sales table with a join to get the company_id:
UPDATE sales s
JOIN company c ON s.company_name = c.company_name
SET s.company_id = c.company_id;
SQL Fiddle Demo
You should also remove the company_name field from the sales table since this is now stored in the company table.
To define an auto incremented integer, you just use the AUTO_INCREMENT keyword. However, if you define any columns as auto_increment, you must also make that column your primary key. Which, in this case, would make sense in order for it to be a foreign key elsewhere.
Try this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(company_id);
SQL Fiddle
I'm working on a web site where users can post articles with this table structure :
CREATE TABLE IF NOT EXISTS `articles` (
`id_articles` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_users` int(10) unsigned NOT NULL,
`articles` text NOT NULL,
PRIMARY KEY (`id_articles`),
UNIQUE KEY `id_articles` (`id_articles`),
KEY `id_users` (`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Each user can 'like' the articles.
Is that the right way below to create a 'like table' :
CREATE TABLE IF NOT EXISTS `articles_likes` (
`id_articles` int(10) unsigned NOT NULL,
`id_users` int(10) unsigned NOT NULL,
KEY `id_articles` (`id_articles`,`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
It is correct but you will want to add separte indexes on id_articles and id_users (also you might want to name the columns 'id_article' and 'id_user' for sanity).
CREATE TABLE IF NOT EXISTS `article_likes` (
`id_article` int(10) unsigned NOT NULL,
`id_user` int(10) unsigned NOT NULL,
KEY `id_article` (`id_article`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB;
The reason you want separate indexes is because in mysql if you create an index on columns (A, B) that index will be used in queries having in the where clause column A, or columns A and B.
In your case for example if you made a query "SELECT * FROM article_likes WHERE id_user=X" this query would not use an index.
An ever better option would be to add a combined index and a separate index on the second column from the combined index. Like this:
CREATE TABLE IF NOT EXISTS `article_likes` (
`id_article` int(10) unsigned NOT NULL,
`id_user` int(10) unsigned NOT NULL,
KEY `id_article_user` (`id_article`, `id_user`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB;
This way you would have optimal performance on queries like 'WHERE id_user=X', "WHERE id_article=X', "WHERE id_article=X AND id_user=Y"
This is a valid way Chris. You can use COUNT() to match the id_articles in the articles_likes table against the current article you are viewing in articles.
$articles_id = 23;
mysql_query("SELECT COUNT(*) FROM articles_likes
WHERE id_articles = ".$articles_id);
You can also just leave COUNT() (MySQL) out and instantly know which users are the "likers" of the articles and use count() (PHP) on the returned Array to duplicate the effect of COUNT() in MySQL.
i would have a total of 3 tables. an articles table, and the user id could be a column in that for users who submit articles , but you need a separate user table since not all users will submit articles (i am assuming), and then a 3rd table for likes, that takes the primary key from users and the primary key from articles and uses them as foreign keys. so each time an article is liked, an entry is made in the 3rd table
I have a question I'm trying to have the q_id column take the value of the id column when a tag is submitted I have been trying to do this using PHP but I was thinking should I just add AUTO INCREMENT to the q_id column since but tables are updated when the tag is submited. or is this the wrong way to do it or is there a better way to do this?
Here is the MySQL tables below.
CREATE TABLE q_tags (
q_id INT UNSIGNED NOT NULL,
users_q_id INT UNSIGNED NOT NULL
);
CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
AUTO INCREMENT is used mainly to avoid adding the same ID into a table, so that you can keep a loyal index. I'd suggest you add a primary key to the q_tags, and change the current q_id to a foreign key, so that it can become a reference to tags table.
EDIT :
CREATE TABLE q_tags (
q_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
t_id INT UNSIGNED NOT NULL,
users_q_id INT UNSIGNED NOT NULL,
PRIMARY KEY (q_id)
);
CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
t_id in the first table is the "foreign key". You can add it as a real mysql foreign key or just keep it this way (if you're planning on be able to "cascade delete" tags and t_tags related, it would matter)