Step By Step Create a MySQL Database Via Command Line on Windows.
Create a MySQL Database. This guide below will show you how to Create a MySQL Database Via Command Line on Windows. It's easy to create a database, we must know how to enter SQL statements. By creating a database on your computer, you will be able to collect, manage, add, find the data stored on your computer. Why we use MySQL on this tutorial to create a database. Because MySQL software is Open Source. And MySQL is the most popular Open Source SQL database management system, is developed, distributed, and supported by Oracle Corporation. Also, The MySQL Database Server is very fast, reliable, scalable, and easy to use. MySQL Server works in client/server or embedded systems. And many more reasons why we use MySQL to create a database. Now, let we start our guide.
Database Programming.
- These instructions below are intended for creating a MySQL database on a Windows operating system computer.
- Remember, each command must end with a semicolon ( ;)
1. XAMPP
- Download and install Xampp server on your computer.
- Now, open Xampp control panel.
- Then start MySQL.
|
XAMPP control panel |
- Now, run Shell
2. Log In to the MySQL server.
- For the first, we must log in to the MySQL server,
- To do this, we must enter a statement to the command line with the following command.
- In this case, we have specified the user is root with u flag.
- If you have specified a password at your MySQL server, you must add p flag at the end of command line, example.
- which **** character assign your password.
- At the other case you want to connect MySQL server to another host, you must add h flag at the end of command line.
- If you successfully log in to MySQL server, You should see MySQL prompt that looks very similar to this:
3. Create Database.
- To create a database with the name customers type the following command:
- create database customers;
- If your database is successfully created, you will see the following message at MySQL prompt.
- Query OK, 1 row affected (0.00 sec)
- If a database of the same name already exists, then a new database will not be created and you’ll receive this error:
- ERROR 1007 (HY000): Can't create database 'customers'; database exists
4. Show Databases.
- To view the database you’ve successfully created simply issue the following command:
- The result will similar to this.
|
show databases result |
5. Use Data Base.
- The use command is used when you have more than one database on a MySQL server and need to switch between them. Statement : use [DatabaseName]; for example;
- The result will similar to this.
6. Create Table.
- At this sections will show you how to create a table inside customers database.
- Now we will create a customers table. the table contains data showed on the image below.
|
table content - create MySQL table |
- Type the following command at MySQL prompt;
- create table customers (customerNumber int(11), customerName varchar(50), phone varchar(50), addressLine1 varchar(50), city varchar(50), state varchar(50), postalCode varchar(15), country varchar(50));
- If the table successfully created, the SQL prompt should return with this result.
- Query OK, 0 rows affected (0.28 sec)
- Do the same steps to create another table.
7. Desc Table.
- DESC is short for DESCRIBE, this function shows the table structure.
- the statement should see like this; ; desc table name;
- Example ;
- SQL prompt should return with this result.
- +------------------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +------------------+-------------+------+-----+---------+-------+
- | customerNumber | int(11) | YES | | NULL | |
- | customerName | varchar(50) | YES | | NULL | |
- | phone | varchar(50) | YES | | NULL | |
- | addressLine1 | varchar(50) | YES | | NULL | |
- | city | varchar(50) | YES | | NULL | |
- | state | varchar(50) | YES | | NULL | |
- | postalCode | varchar(15) | YES | | NULL | |
- | country | varchar(50) | YES | | NULL | |
- +------------------+-------------+------+-----+---------+-------+
- 8 rows in set (0.03 sec)
8. Alter Table.
- Alter table used for change the table structure, there are many options to use alter command.
- Now we will show add a primary key at the customers table.
- Now add a primary key at the customerNumber field.
- type the following command at MySQL Prompt;
- alter table customers add primary key (customerNumber);
- SQL prompt should return with this result.
- Query OK, 0 rows affected (0.63 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- Now, back to the desc table command.
- SQL prompt should return with this result.
|
alter table - create a primary key |
- You should see a PRI under the key column.
9. Input Data Into Table.
- This sections will explain to you how to input data to the table.
- The statements should look like this;
- insert into table name (field1, field2, field3, ....fieldx) values (values1, values2, values3... valuesx);
- example.
- insert into customers (customerNumber, customerName, phone, addressLine1, city, state, postalCode, country) values (1, 'Andy', '+62438999', 'royal street', 'kudus', 'central java', '59388', 'Indonesia');
- SQL prompt should return with this result.
- Query OK, 1 row affected (0.04 sec)
- Remember, to enter data must correspond to the data type, for example.
- data type: Varchar = data must be enclosed in quotation marks.
- data type: Int (numeric) = Data can be directly written.
- data type : date = type with the following format (yyyy-mm-dd)
10. Show Table Data.
- How to show data which is inserted into the table.
- the statement is ;
- example ;
- SQL prompt should return with this result.
|
show table data - MySQL table |
- How to show specified table data
- the statement is ;
- select field1, field2...filedx from table name;
- example
- select customerName, city, country from customers;
- SQL prompt should return with this result.
|
show specified table data |
- Finish
0 Response to "Step By Step Create a MySQL Database Via Command Line on Windows."
Post a Comment