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

  • SEARCH
  • Home
  • 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 7581691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:15:24+00:00 2026-05-30T18:15:24+00:00

I got table with 3 columns (Primary)id, type, name. I am inserting data into

  • 0

I got table with 3 columns (Primary)id, type, name. I am inserting data into this table, and I need to prevent multiple rows with the same type and name. How can I check this and insert in one query?

I was trying

IF ((select id from models where type = 1 and name = 'test') is null)
THEN insert into models(type, name) values(1, 'test');
END IF;

but it is not working and I have no idea why( 0 experience with conditions in mysql) – it is giving me syntax error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF ((select id from models where type = 1 and name = 'test') is null)
THEN inse' at line 1 
  • 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-30T18:15:25+00:00Added an answer on May 30, 2026 at 6:15 pm

    If this is needed in general (not only for this Insert), you should add a UNIQUE constraint on the (type, name) combination:

    ALTER TABLE models
      ADD UNIQUE KEY type_name_U (type, name) ;
    

    Then you can simply add rows and if the combination exists in the table, the Insert will fail:

    INSERT INTO  models(type, name)
      VALUES (1, 'test') ;
    

    For one-time check only, try this instead:

    INSERT INTO  models(type, name)
      SELECT 1, 'test'
      FROM dual
      WHERE NOT EXISTS
            ( SELECT *
              FROM models
              WHERE type = 1 
                AND name = 'test'
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this table with an int(11) column and hundreds of millions of rows.
I got a table with a few columns. The table has a primary key
I've got a table of 5,651,744 rows, with a primary key made of 6
In a table I've got 3 columns: id tag1 tag2 id is a primary
Pulling my hair out. I've got a table 'Belts' with columns 'BeltID' and 'BeltColor'.
I've got a question regarding a SQL-select-query: The table contains several columns, one of
I'm using PHPMyAdmin and I've got a MySQL table column called timestamp. The type
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
So I have this table of book orders, it contains 2 columns, one is
I want to create a table with Primary key referenced to Two columns of

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.