Create DATABASE and database table code with Mysql _mysql

Source: Internet
Author: User
Tags create database
1. Use the show statement to find out what databases are currently 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 sensitive to case sensitivity.
3, select the database you created
Mysql> Use Abccs
Database changed
At this point you have entered the database Abccs you have just created.
4. Create a database table
First look at what tables are present in your database:
Mysql> show TABLES;
Empty Set (0.00 sec)
Indicates that there are no database tables in the database you just established. Next, create a database table MyTable:
We are going to create a birthday table for your employees whose contents include the employee's name, sex, date of birth, and city of birth.
Mysql> Create TABLE mytable (name VARCHAR (), Sex CHAR (1),
-> birth DATE, Birthaddr VARCHAR (20));
Query OK, 0 rows Affected (0.00 sec)
Because the column value of name and Birthadd is variable, select varchar, which is not necessarily 20 in length. You can choose any length from 1 to 255, and you can use the ALTER TABLE statement if you want to change its word size later. The gender can be represented by a single character: "M" or "F", so char (1) is selected, and the birth column uses the date data type.
Once we've created a table, we can look at the results we just made and show you what tables are in the database:
Mysql> show TABLES;
+---------------------+
| Tables in Menagerie |
+---------------------+
| Mytables |
+---------------------+
5, show the structure of the table:
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 the 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 you 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)
Then use the Select command above to see what has changed. We can add all employee records to the table in this way, one by one.
7. Loading data into a database table in text mode
If you enter it one by one, it is troublesome. We can add all the records to your database table in the form of a text file. Create a text file "Mysql.txt", each containing a record, separated by a locator (tab), and given in the order of the columns listed in the CREATE TABLE statement, for example:
Abccs F 1977-07-07
Mary F 1978-12-12 USA
Tom M. 1970-09-02 USA
Use the following command to mount the text file "Mytable.txt" to the MyTable table:mysql> load DATA local INFILE "mytable.txt" into table pet;
Use the following command to see if the data has been entered into the database table:mysql> select * FROM MyTable;
Related Article

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.