Jump to content

HELP :: script SQL with variables (or sort of...)


sgalmeida

Recommended Posts

Greetings :)

 

I have a question about SQL on MySQL. I want to make a SQL file to import to MySQL for filling the tables, but I want to preserve the foreign keys.

I want to know if it is possible to use variables (or something like that) in SQL scripts for MySQL. Imagine this: my database has 2 tables: product and lot. The table product is defined as:

 

CREATE TABLE product

(

productCode VARCHAR(32),

price VARCHAR(32),

 

oid INT UNSIGNED AUTO_INCREMENT,

 

PRIMARY KEY(oid)

);

 

and the table lot is defined like:

 

CREATE TABLE lot

(

lotcode VARCHAR(32),

product INT UNSIGNED,

quantity INT,

 

oid INT UNSIGNED AUTO_INCREMENT,

 

FOREIGN KEY(product) REFERENCES product(oid),

 

PRIMARY KEY(oid)

);

 

In this way, while developing the script SQL file, I habe to know the OID\'s of the products when I do the insert on table lot.

 

INSERT INTO product (productCode, price) VALUES (\"Nike001\",\"120\");

INSERT INTO product (productCode, price) VALUES (\"Adidas001\",\"100\");

 

-- were somewere I have to do something like this

-- SELECT oid FROM product WHERE productCode=\"Nike001\" AND price=\"120\";

-- to be able to insert data on lot\'s table.

 

INSERT INTO lot (lotcode, product, quantity) VALUES (\"LotNike001\",oid of product nike001, 12);

 

or

 

INSERT INTO lot (lotcode, product, quantity) VALUES (\"LotNike001\", [sELECT OID FROM product WHERE productCode=\"Nike001\" AND price=\"120\"], 12); <--- does this work??

 

 

So, is it possible to retrieve and save this data (oid of product) and use it in insert on other tables? If not, what you suggest for me to do to achieve what I want (fill the database with data and preserve the foreign keys)

 

Thank you all.

 

Almeida

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.