2

In Oracle database, we can link SQL scripts inside other SQL script like this:

@@ drop.sql

Is it possible to do similar in MySQL database?

Basically, I want to automate whole database installation by creating new tables, constraints and basic data. For that, it would be good to maintain different type of SQL statements in different SQL scripts/files.

But to install database, we should run run only one SQL script, which should be pointing other SQL scripts from inside.

Also want to do it without any other programming language. It would be better to do it with SQL script only (like Oracle supports).

1 Answer 1

2

Yes, you can, using the command source (which short form is \.). Example:

script1.sql

SELECT 'First hello from script1';
source script2.sql;
SELECT 'Second hello from script1';

script2.sql

SELECT 'Hello from script2';

Calling script1.sql:

$ mysql -s < script1.sql
First hello from script1
Hello from script2
Second hello from script1

More info here: http://dev.mysql.com/doc/refman/5.6/en/mysql-commands.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Muur, it's working. but one thing is, I have to mention everywhere absolute path for all links. There are many nested sql scripts linked, so is there a way to change current working directory after logged in to MySQL console?
I don't think it is possible after. You can do it before with one command: cd /path/to/your/scripts/ && mysql < script.sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.