Using MySQL to create databases and database tables-MySQL-mysql tutorial

Source: Internet
Author: User
Tags mysql tutorial
Analysis of using MySQL to create databases and database tables bitsCN.com
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). This indicates that no database table exists in the database you just created. Create a database table mytable: Create a birthday table for your employees. The table contains the employee name, gender, birth date, 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 choose any length from 1 to 255. if you need to change its font length in the future, you can use the alter table statement .); Gender can be expressed by only one character: "m" or "f". Therefore, CHAR (1) is used, and DATE is used for the birth column. After creating a table, we can see the result 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 a record 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 just created has not been recorded. Add a new record: mysql> insert into mytable-> values ('abccs ', 'F', '1977-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. loading data into a database table in text mode is troublesome if data is input one by one. We can add all records to your database table using text files. CREATE an example file named "“mysql.txt". each line contains a record. use the "tab" to separate the values and give them in the order of the columns listed in the create table statement. 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 exported 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; author: Sun Zhenya bitsCN.com

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.