search
HomeDatabaseMysql TutorialMySQL中多表操作和批处理详细介绍(1)

MySQL中多表操作和批处理详细介绍(1)

Jun 07, 2016 pm 04:05 PM
mysqlintroduceBatch processingoperatedetailed

多表操作 在一个数据库中,可能存在多个表,这些表都是相互关联的。我们继续使用前面的例子。前面建立的表中包含了员工的一些基本信息,如姓名、性别、出生日期、出生地。我们再创建一个表,该表用于描述员工所发表的文章,内容包括作者姓名、文章标题、发表

多表操作

在一个数据库中,可能存在多个表,这些表都是相互关联的。我们继续使用前面的例子。前面建立的表中包含了员工的一些基本信息,如姓名、性别、出生日期、出生地。我们再创建一个表,该表用于描述员工所发表的文章,内容包括作者姓名、文章标题、发表日期。

1、查看第一个表mytable的内容

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

2、创建第二个表title(包括作者、文章标题、发表日期)

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

向该表中填加记录,最后表的内容如下:

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

3、多表查询

现在我们有了两个表: mytable 和 title。利用这两个表我们可以进行组合查询: 例如我们要查询作者abccs的姓名、性别、文章:

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

上面例子中,由于作者姓名、性别、文章记录在两个不同表内,因此必须使用组合来进行查询。必须要指定一个表中的记录如何与其它表中的记录进行匹配。

注意:如果第二个表title中的writer列也取名为name(与mytable表中的name列相同)而不是writer时,就必须用mytable.name和title.name表示,以示区别。

再举一个例子,用于查询文章a2的作者、出生地和出生日期:

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

修改和备份、批处理

有时我们要对数据库表和数据库进行修改和删除,可以用如下方法实现:

1、增加一列:

如在前面例子中的mytable表中增加一列表示是否单身single:

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

2、修改记录

将abccs的single记录修改为“y”:

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

现在来看看发生了什么:

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools