You are viewing the article How to Create a Database in MySQL at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 11,334 times.
This is an article on how to create a database in MySQL. To create a database, you must open the “mysql” command line interface and enter database commands while the server is running.
Steps
Open the MySQL command line program
- You can check the status of your server by opening MySQL Workbench, selecting your server, and viewing the information under “Server Status” under the “Administration – Server Status” tab. server state).
- Windows — Copy C:/Program Files/MySQL/MySQL Workbench 8.0 CE/ and replace the name of the last directory with the name of the current MySQL.
- Mac — Copy /usr/local/mysql-8.0.13-osx10.13-x86_64/ and replace the name of the last directory with the name of the current MySQL.
cd C:Program FilesMySQLMySQL Workbench 8.0 CE
mysql - u me - p
- You should see a “MySQL>” tab showing the command line program. From now on, every command line that you enter is handled by the MySQL command line program.
- Learn how to enter MySQL commands. MySQL commands must have a semicolon (;) right after the end of the command line, but you can also type the command, type a semicolon, and press ↵ Enter again.
Create a database
create database Pet_Records ;
- The name of the database cannot contain spaces; if you want to add a space to the name, enter an underscore (for example, “Friends of Mine” becomes “Friends_of_Mine”).
- Each MySQL command must end with a semicolon. If you forget to enter a semicolon, you can type it right next to the currently displayed … and press ↵ Enter again.
show databases ;
use Pet_Records ;
Create Table
- Title — Your title should appear immediately after the “create table” command, and be set according to the same principles as the database name (i.e. no spaces) .
- Cpumn Heading — You can set column headings by entering various names within parentheses (see examples in the next step).
- Cell Length — When setting the character length, you will use “VARCHAR” (flexible character count, which means you can enter from one to a limited number of VARCHAR characters) or “CHAR” (cannot enter more or less than the specified number of characters; for example, CHAR(1) allows only 1 character, CHAR(3) requires 3 characters, etc.).
- Date — If you want to add dates to the table, use the “DATE” command to have the contents of the column formatted with the date. Dates must be entered in the format
YYYY-MM-DD
(year-month-day).
create table name ( cpumn1 varchar ( 20 ), cpumn2 varchar ( 30 ), cpumn3 char ( 1 ), cpumn4 date );
- For example, to create a “Pets” table with 2 VARCHAR columns, 1 CHAR column, and 1 Date column, you would write the following command:
create table Pets ( Name varchar ( 20 ), Breed varchar ( 30 ), Sex char ( 1 ), DOB date );
insert into name values ( 'cpumn1 value' , 'cpumn2 value' , 'cpumn3 value' , 'cpumn4 value' );
- With the “Pets” table example used earlier, your line of information is entered as follows:
insert into Pets values ( 'Fido' , 'Husky' , 'M' , '2017-04-12' );
- You can enter the content of the column as NULL if it is an empty column.
load data local infile '/path/name.txt' into table name lines terminated by 'rn' ;
- With the “Pets” example, you can write the following command:
load data local infile 'C:/Users/name/Desktop/pets.txt' into table Pets lines terminated by 'rn' ;
- On a Mac, you need to use the “lines terminated by” command with 'r' instead of 'rn' .
show databases ; select * from Pet_Records ;
Advice
- Here are some commonly used data types:
- CHAR ( length ) – fixed length character string
- VARCHAR ( length_length ) – character string whose maximum length is length
- TEXT – flexible character string with a maximum length of no more than 64KB
- INT ( length ) – 32-bit integer with a maximum number of digits of length (for negative numbers, the sign ‘-‘ counts as a ‘digit’)
- DECIMAL ( length , decimal ) – Decimal number of characters displayed by length ; the decimal part specifies the maximum number of digits after the comma
- DATE – Date value (year, month, day)
- TIME – Time value (hour, minute, second)
- ENUM (” value1 “, ” value2 “, ….) – List of count values
- Here are some optional parameters:
- NOT NULL – A value must be entered. This field cannot be left blank.
- DEFAULT default_value – the default value that will be entered when you do not enter another value.
- UNSIGNED – With numeric fields you need to make sure the numbers are not negative
- AUTO_INCREMENT – The value will be automatically incremented every time a new line is added to the data table.
Warning
- If the MySQL server is down when you try to log in to the command line program “mysql”, you cannot proceed further.
- When writing code, you need to make sure the lines are spelled correctly and with spaces before pressing the Enter key.
This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.
The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.
This article has been viewed 11,334 times.
This is an article on how to create a database in MySQL. To create a database, you must open the “mysql” command line interface and enter database commands while the server is running.
Thank you for reading this post How to Create a Database in MySQL at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: