MySql processes databases and tables, and MySql processes databases.

Source: Internet
Author: User

MySql processes databases and tables, and MySql processes databases.

View Database

It is usually useful to obtain the Database List on the server. Run the show databases command.

Mysql> show databases;


Create a database

Mysql> create database db_test;
Query OK, 1 row affected (0.00 sec)


Use Database

Once a database is created, you can use the use command to specify it as the default working database.

Mysql> use db_test;
Database changed


Delete Database

The method for deleting a database is similar to that for creating a database. You can use the drop command in the mysql client to delete a database, as shown below:

Mysql> drop database db_test;
Query OK, 0 rows affected (0.00 sec)

 

Processing table

This section describes how to create, list, view, delete, and modify a MySQL database table.


Create a table

You can use the create table statement to create a table. Many options and clauses are used during table creation. It is unrealistic to simply summarize them here. Here, we will summarize them most commonly. In the future, we will summarize them separately. The common usage of creating a table is as follows:

mysql> create table tb_test(    -> id int unsigned not null auto_increment,    -> firstname varchar(25) not null,    -> lastname varchar(25) not null,    -> email varchar(45) not null,    -> phone varchar(10) not null,    -> primary key(id));Query OK, 0 rows affected (0.03 sec)

Remember, a table must contain at least one column. In addition, after creating a table, you can always go back and modify the table structure. You can create a table no matter whether or not you are using the target database. You only need to add the target database before the table name. For example:

mysql> create table db_test.tb_test(    -> id int unsigned not null auto_increment,    -> firstname varchar(25) not null,    -> lastname varchar(25) not null,    -> email varchar(45) not null,    -> phone varchar(10) not null,    -> primary key(id));Query OK, 0 rows affected (0.03 sec)


Conditional table Creation

By default, if you try to create an existing table, MySQL will generate an error. To avoid this error, the create table statement provides a sub-statement. If you want to simply exit table creation when the target table already exists, you can use this sub-statement. For example:

mysql> create table if not exists db_test.tb_test(    -> id int unsigned not null auto_increment,    -> firstname varchar(25) not null,    -> lastname varchar(25) not null,    -> email varchar(45) not null,    -> phone varchar(10) not null,    -> primary key(id));Query OK, 0 rows affected, 1 warning (0.00 sec)

The "Query OK" message is displayed when you return to the Command Prompt window whether or not you have created the command.


Copy a table

Creating a new table based on an existing table is an easy task. The following code returns a copy of The tb_test table named tb_test2:

mysql> create table tb_test2 select * from db_test.tb_test;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0

An identical table tb_test2 will be added to the database. In some cases, you may want to create a table only based on several columns of the existing table. You can use the create select statement to specify columns:

mysql> describe tb_test;+-----------+------------------+------+-----+---------+----------------+| Field     | Type             | Null | Key | Default | Extra          |+-----------+------------------+------+-----+---------+----------------+| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment || firstname | varchar(25)      | NO   |     | NULL    |                || lastname  | varchar(25)      | NO   |     | NULL    |                || email     | varchar(45)      | NO   |     | NULL    |                || phone     | varchar(10)      | NO   |     | NULL    |                |+-----------+------------------+------+-----+---------+----------------+5 rows in set (0.01 sec)mysql> create table tb_test2 select id, firstname, lastname, email from tb_test;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> describe tb_test2;+-----------+------------------+------+-----+---------+-------+| Field     | Type             | Null | Key | Default | Extra |+-----------+------------------+------+-----+---------+-------+| id        | int(10) unsigned | NO   |     | 0       |       || firstname | varchar(25)      | NO   |     | NULL    |       || lastname  | varchar(25)      | NO   |     | NULL    |       || email     | varchar(45)      | NO   |     | NULL    |       |+-----------+------------------+------+-----+---------+-------+4 rows in set (0.01 sec)


Create temporary table

Sometimes, when you work on a very large table, you may occasionally need to run many queries to obtain a small subset of a large amount of data, instead of running these queries on the entire table, instead, it is faster for MySQL to locate a few required records and save the records to a temporary table. Then, it queries these temporary tables. You can use the temporary keyword and the create table statement.

mysql> create temporary table emp_temp select firstname, lastname from tb_test;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0

Temporary tables are created in the same way as other tables, but they are stored in the temporary directory specified by the operating system. Temporary tables will exist during your connection to MySQL. When you disconnect, MySQL will automatically delete the table and release all the memory space. Of course, you can also manually use the drop table command to delete a temporary table.


View available tables in the database

You can run the show tables command. For example:

mysql> show tables;+-------------------+| Tables_in_db_test |+-------------------+| tb_test           || tb_test2          |+-------------------+2 rows in set (0.00 sec)


View table structure

You can use the describe statement to view the table structure, for example:

mysql> describe tb_test;+-----------+------------------+------+-----+---------+----------------+| Field     | Type             | Null | Key | Default | Extra          |+-----------+------------------+------+-----+---------+----------------+| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment || firstname | varchar(25)      | NO   |     | NULL    |                || lastname  | varchar(25)      | NO   |     | NULL    |                || email     | varchar(45)      | NO   |     | NULL    |                || phone     | varchar(10)      | NO   |     | NULL    |                |+-----------+------------------+------+-----+---------+----------------+5 rows in set (0.00 sec)

In addition, use the show command to get the same results, for example:

mysql> show columns in tb_test;+-----------+------------------+------+-----+---------+----------------+| Field     | Type             | Null | Key | Default | Extra          |+-----------+------------------+------+-----+---------+----------------+| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment || firstname | varchar(25)      | NO   |     | NULL    |                || lastname  | varchar(25)      | NO   |     | NULL    |                || email     | varchar(45)      | NO   |     | NULL    |                || phone     | varchar(10)      | NO   |     | NULL    |                |+-----------+------------------+------+-----+---------+----------------+5 rows in set (0.00 sec)


Delete table

The drop table statement is used to delete a table. Its syntax is as follows:

drop [temporary] table [if exists] tbl_name [, tbl_name, ...]


Change table structure

We will find that we often modify and improve the table structure, especially in the early stages of development. However, you do not have to delete the table before creating a new table each time you modify it. Instead, you can use the alter statement to modify the table structure. With this statement, you can delete, modify, and add columns if necessary. Like create table, alter table provides many clauses, keywords, and options. Here we will only talk about some simple usage. For example, insert a column in the tb_demo table to indicate email. The Code is as follows:

mysql> alter table tb_demo add column email varchar(45);Query OK, 0 rows affected (0.14 sec)Records: 0  Duplicates: 0  Warnings: 0

The new column is placed at the end of the table. However, you can also use appropriate keywords (including first, after, and last) to control the positions of new columns. If you want to modify the table, such as the newly added email address, I want to add a not null control. The code can be like this:

mysql> alter table tb_demo change email email varchar(45) not null;Query OK, 0 rows affected (0.11 sec)Records: 0  Duplicates: 0  Warnings: 0

If you think this email column is unnecessary, you can use the following code to delete it, for example:

mysql> alter table tb_demo drop email;Query OK, 0 rows affected (0.09 sec)Records: 0  Duplicates: 0  Warnings: 0

Mysql

MySQL uses the following statement to query the field information of a specified table.

Mysql> SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
-> FROM
-> INFORMATION_SCHEMA.COLUMNS
-> WHERE
-> Table_name = 'test _ main'
-> AND table_schema = 'test'
-> //
+ ------------- + ----------- + ------------- + ---------------- +
| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT |
+ ------------- + ----------- + ------------- + ---------------- +
| Id | int | NO | 0 |
| Value | varchar | YES | NULL |
+ ------------- + ----------- + ------------- + ---------------- +
2 rows in set (0.00 sec)

If the owner knows the field, he wants to know the table. Modify the SQL

SELECT
Table_name
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
Table_schema = 'your database name is also use or something'
AND COLUMN_NAME = 'user'

The preceding SQL
All table names with the user field should be listed.

If the number of tables is small, query the specific table.

How to store images and tables in a mysql database (or other databases)

Because both word and html are structured documents, you need to parse these documents for data acquisition. For html, you can use the html parsing class of the java xml parsing package to complete the parsing, after parsing, you can read the data of the started table element and convert the data to the database. For word files, I remember a java package that generates the word file, however, I have never used it. There is also a simple way for you to go to Baidu online. If you do not need to process and analyze their data, you can directly save these documents in the database and use blob fields. mysql supports blob fields, which are read from blob when you need to view reports, you can view the original post by directly displaying the generated File>

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.