We continue to use the previous example. The table previously established contains some basic information about the employee, such as name, sex, date of birth, place of birth. We'll create a table that describes the articles published by the employee, including the author's name, the title of the article, and the date of publication.
1, view the contents of the first table mytable:
Mysql> select * FROM MyTable;
+----------+------+------------+-----------+
| name | sex | Birth | birthaddr |
+----------+------+------------+-----------+
| Abccs |f | 1977-07-07 | Our |
| Mary |f | 1978-12-12 | USA |
| Tom |m | 1970-09-02 | USA |
+----------+------+------------+-----------+
2, create a second table title (including author, article title, Publication date):
Mysql> CREATE TABLE title (writer varchar) NOT NULL,
-> title varchar NOT NULL,
-> senddate date);
Add a record to the table, and the final table reads as follows:
<ccid_nobr>
<table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 2 "
Bordercolorlight = "BLACK" Bordercolordark = "#FFFFFF" align= "Center" >
<tr>
<TD bgcolor= "E6e6e6" "font-size:9pt" >
<pre><ccid_code> 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 make a combination query:
In the example above, because the author's name, sex, and article are recorded in two different tables, you must use a combination to query. You must specify how records in one table match the records in other tables.
Note: If the writer column in the second table title is also named name (the same as the Name column in the MyTable table) instead of write r, it must be represented by Mytable.name and Title.name as a distinction.
Another example is used to query the author, birthplace, and date of birth of the 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 | Our | 1977-07-07 |
+-------+--------+-----------+------------+
Modification and backup, batch processing
Sometimes we have to modify and delete database tables and databases, which can be implemented in the following ways:
1. Add one column:
Add a column in the MyTable table in the previous example to indicate whether single singles:
Mysql> ALTER TABLE mytable add column single char (1);
2, modify the record
Modify the Abccs single record to "Y":
mysql> Update mytable set Single=′y′where name=′abccs′; Now let's see what's going on:
Mysql> select * FROM MyTable;
+----------+------+------------+-----------+--------+
| name | sex | Birth | birthaddr | Single |
+----------+------+------------+-----------+--------+
| Abccs |f | 1977-07-07 | Our | y |
| Mary |f | 1978-12-12 | USA | NULL |
| Tom |m | 1970-09-02 | USA | NULL |
+----------+------+------------+-----------+--------+
3, increase the record
I've already talked about how to add a record to make it easier to see and repeat with this:
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.