Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Databases and PHP Getting Started with PDO

Alex Flores
Alex Flores
7,864 Points

How do you actually create a SQL Database file?

She never explained how she created the SQL database file (database.sql). It's very long and very redundant, so I imagine there's a better way than just typing out everything. Say I want to make my own database from scratch, how would I do that and what would be the quickest way?

2 Answers

Hi there Alex!

There are at least three common options.

1) phpmyadmin -> (with visual friendly interface)

2) mysql.exe ->(through console and without user friendly interface).

3) Inside your code editor.

Faster is console of course, because you are interacting with Database Mechanism directly without any graphical interface. But in this case you need to know commands to interact with it.

Here is small example for your question(in this test case i'm using xampp that is installed on windows 10), but this commands can be used on Unix like systems also:

D:\>mysql -u user_name -p

After this you can list all your database(and see if the database already exist). This command can be skipped. But if you want to see the whole picture you will need it.

MariaDB [user_name]> show databases;
+--------------------+
| Database           |
+--------------------+
| tests              |
+--------------------+

After this, we can create new database and check if it already exist.

MariaDB [user_name]> create database if not exists `new_test`;
Query OK, 1 row affected (0.07 sec)
MariaDB [user_name]> show databases;
+--------------------+
| Database           |
+--------------------+
| new_test           |
| tests              |
+--------------------+

To exit mysql console, type next:

MariaDB [user_name]>exit;

In order to export, and create new_test.sql file. Type this command.

D:\>mysqldump -u root -p new_test > test.sql

This command will create dump with all tables, properties and data for them. In this case file will be save in

D:\>test.sql

But the most easiest way, is to create it in your code editor =) And write next commands(but before importing all your changes you need to be sure that you created database before):

create table `new_table` (
 id INT,
 name VARCHAR(256)
) engine=MyISAM;

INSERT INTO `new_table` (`id`, `name`) VALUES (1, 'your_name');

After this, you can import your code inside created DB;

D:\>mysqldump -u root -p new_test < test.sql

Best regards.

Alex Flores
Alex Flores
7,864 Points

Thanks this was VERY helpful!