Combination of two columns as unique Key in mysql - php

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:

Related

SQL: Getting values which all other values related to each must be in another table

I'm working in a web page with PHP and MySQL where I have this DB:
CREATE TABLE action (
idaction INT NOT NULL AUTO_INCREMENT,
--Other columns
PRIMARY KEY (`idaction`));
CREATE event (
idevent INT NOT NULL AUTO_INCREMENT,
--Other columns
PRIMARY KEY (`idevent`));
CREATE TABLE currentGame (
user_email VARCHAR(45) NOT NULL,
--Other columns
PRIMARY KEY (`user_email`));
CREATE TABLE IF NOT EXISTS currentEvents (
currentGame_user_email VARCHAR(45) NOT NULL,
event_idevent INT NOT NULL,
PRIMARY KEY (currentGame_user_email, event_idevent),
FOREIGN KEY (currentGame_user_email) REFERENCES currentGame (user_email),
FOREIGN KEY (event_idevent) REFERENCES event (idevent));
CREATE TABLE IF NOT EXISTS spawnConditions (
action_idaction INT NOT NULL,
event_idevent INT NOT NULL,
PRIMARY KEY (action_idaction, event_idevent),
FOREIGN KEY (action_idaction) REFERENCES action (idaction),
FOREIGN KEY (event_idevent) REFERENCES event (idevent));
So I need to do a query which has the actions which fulfill any of these conditions:
It is not in spawnConditions.
If it is in spawnConditions, all the events which it is related to in this table, must be in the subgroup of currentEvent with a certain known user_email.
In other words, for action A1, being in spawnConditions with events E1 and E2, to be able to be selected from table action, both E1 and E2 must be in currentEvents with the known currentGame_user_email.
Can it be written using only SQL or do I need to involve PHP?
I think that not exists can get the work done here. Basically, you want to filter out actions for which a corresponding record exists in currentEvents with a currentGame_user_email other than a fixed value:
select a.*
from action a
where not exists (
select 1
from spawnConditions sc
inner join currentEvents ce
on ce.event_idevent = sc.event_idevent
where
sc.action_idaction = a.idaction
and ce.currentGame_user_email <> ?
)
The question mark represents the "certain known user_email" you mentioned in the question.

create table column based on another table column value in sql

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

I want to add multiple data in mysql table with php

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.

Assign a unique ID to each name in a table where names are repeated

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

PHP/MySQL database question?

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)

Categories