MySQL getting started (3)

Source: Internet
Author: User

MySQL getting started (3)
-- Learning

After learning some basic operation commands, let's learn how to create a database and a database table.

1. Use the show statement to find out the current database on the server:

Mysql> show databases;
+ ---------- +
| Database |
+ ---------- +
| MySQL |
| Test |
+ ---------- +
3 rows in SET (0.00 Sec)

2. Create a database, abccs
Mysql> Create Database abccs;
Note that different operating systems are case sensitive.

3. Select the database you created
Mysql> Use abccs
Database changed
Now you have entered the database abccs you just created.

4. Create a database table
First, check what tables exist in your database:
Mysql> show tables;
Empty set (0.00 Sec)
It indicates that no database table exists in the Database just created. Create a database table mytable:

We want to create a birthday table for employees in your company. The table contains the employee name, gender, date of birth, and city of birth.
Mysql> Create Table mytable (name varchar (20), sex char (1 ),
-> Birth date, birthaddr varchar (20 ));
Query OK, 0 rows affected (0.00 Sec)

Because the column values of name and birthadd change, varchar is selected and its length is not necessarily 20. You can select
Any length from 1 to 255. If you need to change its font length later, you can use the alter table statement .);
Gender can be expressed by only one character: "M" or "F". Therefore, char (1) is used );
The birth column uses the date data type.

After creating a table, we can look at the results just now and use show tables to show which tables are in the database:
Mysql> show tables;
+ --------------------- +
| Tables in menagerie |
+ --------------------- +
| Mytables |
+ --------------------- +

5. display table structure:
Mysql> describe mytable;
+ ------------- + ------ + ----- + --------- + ------- +
| FIELD | type | null | key | default | extra |
+ ------------- + ------ + ----- + --------- + ------- +
| Name | varchar (20) | Yes | null |
| Sex | char (1) | Yes | null |
| Birth | date | Yes | null |
| Deathaddr | varchar (20) | Yes | null |
+ ------------- + ------ + ----- + --------- + ------- +
4 rows in SET (0.00 Sec)

6. Add records to the table
We first use the SELECT command to view the data in the table:
Mysql> select * From mytable;
Empty set (0.00 Sec)
This indicates that the table created just now has no records.

Add a new record:
Mysql> insert into mytable
-> Values ('abccs ', 'F', '2017-07-07', 'China ');
Query OK, 1 row affected (0.05 Sec)
Use the SELECT command above to check what has changed.

We can add records of all employees to the table one by one using this method.

7. load data into a database table in text mode
It is troublesome to input data one by one. We can add all records to your database table using text files.
Create a file named "mymysql.txt". Each line contains a record. Use the "tab" to separate the values and
The column order listed in the create table statement is given, for example:

Abccs F 1977-07-07 China
Mary F 1978-12-12 USA
Tom M 1970-09-02 USA

Use the following command to load the parent file named mytable.txt to the mytable table:
Mysql> load data local infile "mytable.txt" into Table PET;

Run the following command to check whether the data has been input to the database table:
Mysql> select * From mytable;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.