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 6803307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:19:43+00:00 2026-05-26T19:19:43+00:00

I am doing everything below from the command line and/or mysql> prompt. I am

  • 0

I am doing everything below from the command line and/or mysql> prompt.

I am newbie to DBs and have noticed there are a lot of sites on the structure (tutorials) and definitions (manuals), but none about practical examples. If I have two tables like so

CREATE TABLE IF NOT EXISTS owner
(
  ID INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(32) NOT NULL UNIQUE,
  PRIMARY KEY(ID)
)ENGINE=InnoDB


CREATE TABLE IF NOT EXISTS dog
(
  ID INT NOT NULL AUTO_INCREMENT,
  owner INT NOT NULL,
  name VARCHAR(32) NOT NULL UNIQUE,
  PRIMARY KEY(ID),
  FOREIGN KEY (owner) REFERENCES owner(ID)
)ENGINE=InnoDB

And if I want to add to table dog

INSERT INTO dog (owner, name) VALUES(get_owner_ID("Peter Griffin"), "Brian Griffin");

How do I get the cryptic owner ID from the name (“Peter Griffin”)

  • 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-26T19:19:43+00:00Added an answer on May 26, 2026 at 7:19 pm

    You could either run a sub-select when inserting into the dog table or create a user defined function of your own. A user defined function has an advantage over a stored procedure here since it can be called during your insert. Hence:

    CREATE TABLE IF NOT EXISTS owner
    (
      ID INT NOT NULL AUTO_INCREMENT,
      name VARCHAR(32) NOT NULL UNIQUE,
      PRIMARY KEY(ID)
    )ENGINE=InnoDB;
    
    CREATE TABLE IF NOT EXISTS dog
    (
      ID INT NOT NULL AUTO_INCREMENT,
      owner INT NOT NULL,
      name VARCHAR(32) NOT NULL UNIQUE,
      PRIMARY KEY(ID),
      FOREIGN KEY (owner) REFERENCES owner(ID)
    )ENGINE=InnoDB;
    

    Now create the function:

    DELIMITER //
    
    DROP FUNCTION IF EXISTS get_owner_id//
    
    CREATE FUNCTION get_owner_id(i_owner_name varchar(32)) returns integer 
    READS SQL DATA
    BEGIN
     declare v_owner_id int;
     select id into v_owner_id from owner where name = i_owner_name; 
     return v_owner_id;
    END//
    
    DELIMITER ;
    
    insert into owner(name) values ('Peter Griffin');
    insert into dog (owner,name) values (get_owner_id('Peter Griffin'),'Brian Griffin');
    

    You would have to think of a way of handling the case where the owner does not exist in the owner table of course. Maybe have a default ‘unknown owner’ in the owner table and return that from the function if no owner is found? Kinda up to you…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing a lot of searching and still can't seem to figure
I have a NSURLConnection that gets data from a JSON web service, and everything
I'm trying to make everything apart from the first element in an array have
As far as I can tell i'm doing everything by the book, but the
Sorry for stupid questions, I'm doing everything as described in this tutorial: http://www.functionx.com/visualc/howto/calldlgfromdlg.htm I
If a company has developed a framework for doing almost everything their business requires,
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Doing my first tryouts with foreign keys in a mySQL database and are trying
Doing some jquery animation. I have certain divs set up with an attribute of
When doing an INSERT with a lot of data, ie: INSERT INTO table (mediumtext_field)

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.