MySQL multi-Table operations and batch processing (1)

Source: Internet
Author: User

Multi-Table operations

Multiple tables may exist in a database, which are associated with each other. We will continue to use the previous example. The preceding table contains basic information about an employee, such as name, gender, date of birth, and place of birth. Create another table to describe the articles published by employees, including the author's name, article title, and publication date.

1. view the contents of mytable in the first table.

mysql> select * from mytable;
+----------+------+------------+-----------+
|   name   | sex  |    birth   | birthaddr |
+----------+------+------------+-----------+
|   abccs  |   f  | 1977-07-07 |   china   |
|   mary   |   f  | 1978-12-12 |    usa    |
|    tom   |   m  | 1970-09-02 |    usa    |
+----------+------+------------+-----------+

2. Create the second table title, including the author, article title, and publication date)

mysql> create table title(writer varchar(20) not null,
-> title varchar(40) not null,
-> senddate date);

Add a record to the table. The contents of the last table are as follows:

mysql>
select * from title;
+--------+-------+------------+
| writer | title |  senddate  |
+--------+-------+------------+
| abccs  |   a1  | 2000-01-23 |
|  mary  |   b1  | 1998-03-21 |
| abccs  |   a2  | 2000-12-04 |
|   tom  |   c1  | 1992-05-16 |
|   tom  |   c2  | 1999-12-12 |
+--------+-------+------------+
5 rows in set (0.00sec)

3. Multi-Table query

Now we have two tables: mytable and title. Using these two tables, we can perform a Combined Query: for example, we want to query the name, gender, and Article of the author abccs:

mysql> SELECT name,sex,title FROM mytable,title
-> WHERE name=writer AND name=′abccs′;
+-------+------+-------+
|  name |  sex | title |
+-------+------+-------+
| abccs |   f  |   a1  |
| abccs |   f  |   a2  |
+-------+------+-------+

In the above example, because the author's name, gender, and Article record are in two different tables, you must use a combination for query. You must specify how records in a table match those in other tables.

Note: if the name of the writer column in the title of the second table is the same as that in the mytable Table), instead of the writer column, you must use mytable. name and title. name to indicate the difference.

Another example is used to query the author, place of birth, and date of birth of article a2:

mysql> select title,writer,birthaddr,birth from mytable,title
-> where mytable.name=title.writer and title=′a2′;
+-------+--------+-----------+------------+
| title | writer | birthaddr |    birth   |
+-------+--------+-----------+------------+
|   a2  |  abccs |   china   | 1977-07-07 |
+-------+--------+-----------+------------+

Modification and backup, batch processing

Sometimes we need to modify and delete database tables and databases, which can be implemented as follows:

1. Add a column:

For example, in the mytable table in the previous example, adding a column to indicate whether the table is single or not:

mysql> alter table mytable add column single char(1);

2. Modify records

Modify the single record of abccs to "y ":

mysql> update mytable set single=′y′ where name=′abccs′;

Now let's see what happened:

mysql> select * from mytable;
+----------+------+------------+-----------+--------+
|   name   |  sex |    birth   | birthaddr | single |
+----------+------+------------+-----------+--------+
|   abccs  |   f  | 1977-07-07 |   china   |    y   |
|    mary  |   f  | 1978-12-12 |    usa    |  NULL  |
|    tom   |   m  | 1970-09-02 |    usa    |  NULL  |
+----------+------+------------+-----------+--------+


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.