Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:43:34+00:00 2026-05-27T21:43:34+00:00

I have a mySQL database and I have a Perl script that connects to

  • 0

I have a mySQL database and I have a Perl script that connects to it and performs some data manipulation.
One of the tables in the DB looks like this:

CREATE  TABLE `mydb`.`companies` (
  `company_id` INT NOT NULL AUTO_INCREMENT,
  `company_name` VARCHAR(100) NULL ,
  PRIMARY KEY (`company_id`) );

I want to insert some data in this table. The problem is that some companies in the data can be repeated.

The question is: How do I check that the “company_name” already exist? If it exist I need to retrieve “company_id” and use it to insert the data into another table. If it does not, then this info should be entered in this table, but I already have this code.

Here is some additional requirement: The script can be run multiple times simultaneously, so I can’t just read the data into the hash and check if it already exist.

I can throw an additional “SELECT” query, but it will create an additional hit on the DB.

I tried to look for an answer, but every question here or the thread on the web talks about using the primary key checking. I don’t need this. The DB structure is already set but I can make changes if need to be. This table will be used as an additional table.

Is there another way? In both DB and Perl.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:43:36+00:00Added an answer on May 27, 2026 at 9:43 pm

    “The script can be run multiple times simultaneously, so I can’t just read the data into the hash and check if it already exist.”

    It sounds like your biggest concern is that one instance of the script may insert a new company name while another script is running. The two scripts may check the DB for the existence of that company name when it doesn’t exist, and then they might both insert the data, resulting in a duplicate.

    1. Assuming I’m understanding your problem correctly, you need to look at transactions. You need to be able to check for the data and insert the data before anyone else is allowed to check for that data. That will keep a second instance of the script from checking for data until the 1st instance is done checking AND inserting.

      Check out: http://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html

      And: http://dev.mysql.com/doc/refman/5.1/en/commit.html

      MyISAM doesn’t support transactions. InnoDB does. So you need to make sure your table is InnoDB. Start your set of queries with START TRANSACTION.

    2. Alternatively, you could do this, if you have a unique index on company_name (which you should).

      $query_string = "INSERT INTO `companies` (NULL,'$company_name')";
      

      This will result in an error if the company_name already exists. Try a sample run attempting to insert a duplicate company name. In PHP,

      $result = mysql_query($query_string);
      

      $result will equal false on error. So,

      if(!$result) {
        $query2 = "INSERT INTO `other_table` (NULL,`$company_name`)";
        $result2 = mysql_query($query2);
      }
      

      If you have a unique key on company_name in both tables, then MySQL will not allow you to insert duplicates. Your multiple scripts may spend a lot of time trying to insert duplicates, but they will not succeed.

    EDIT: continuing from the above code, and doing your work for you, here is what you would do if the insert was successful.

    if(!$result) {
      $query2 = "INSERT INTO `other_table` (NULL,`$company_name`)";
      $result2 = mysql_query($query2);
    } else if($result !== false) { // must use '!==' rather than '!=' because of loose PHP typing
      $last_id = mysql_insert_id();
      $query2 = "UPDATE `other_table` SET `some_column` = 'some_value' WHERE `id` = '$last_id'";
      // OR, maybe you want this query
      // $query2a = "INSERT INTO `other_table` (`id`,`foreign_key_id`) VALUES (NULL,'$last_id');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that gets data from a MySQL database on one
I have a MySQL database that has a few very simple tables. I would
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a MySQL database of keywords that are presently mixed-case. However, I want
I have a mysql database which has grown to over 200 tables in it.
I have a MySQL database that I want to archive . What is the
I have a mysql database set as utf-8, and csv data set as utf-8,
I have a mysql database which as one of the fields contains a html
I have a Perl script that reads a command file and restarts itself if
I have this script in perl and it crashes. The database is probably million

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.