收藏 分享(赏)

MySQL教程:多表操作和批处理方法.doc

上传人:hskm5268 文档编号:7058284 上传时间:2019-05-04 格式:DOC 页数:4 大小:30.50KB
下载 相关 举报
MySQL教程:多表操作和批处理方法.doc_第1页
第1页 / 共4页
MySQL教程:多表操作和批处理方法.doc_第2页
第2页 / 共4页
MySQL教程:多表操作和批处理方法.doc_第3页
第3页 / 共4页
MySQL教程:多表操作和批处理方法.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、MySQL 教程:多表操作和批处理方法多表操作在一个数据库中,可能存在多个表,这些表都是相互关联的。我们继续使用前面的例子。前面建立的表中包含了员工的一些基本信息,如姓名、性别、出生日期、出生地。我们再创建一个表,该表用于描述员工所发表的文章,内容包括作者姓名、文章标题、发表日期。1、查看第一个表 mytable 的内容mysql select * from mytable;+-+-+-+-+| name | sex | birth | birthaddr |+-+-+-+-+| abccs | f | 1977-07-07 | china | mary | f | 1978-12-12 |

2、usa | tom | m | 1970-09-02 | usa |+-+-+-+-+2、创建第二个表 title(包括作者、文章标题、发表日期)mysql create table title(writer varchar(20) not null,- title varchar(40) not null,- senddate date);向该表中填加记录,最后表的内容如下:mysqlselect * from title;+-+-+-+| writer | title | senddate |+-+-+-+| abccs | a1 | 2000-01-23 | mary | b1 | 19

3、98-03-21 | abccs | a2 | 2000-12-04 | tom | c1 | 1992-05-16 | tom | c2 | 1999-12-12 |+-+-+-+5 rows in set (0.00sec)3、多表查询现在我们有了两个表: mytable 和 title。利用这两个表我们可以进行组合查询: 例如我们要查询作者 abccs 的姓名、性别、文章:mysql SELECT name,sex,title FROM mytable,title- WHERE name=writer AND name=abccs;+-+-+-+| name | sex | title

4、|+-+-+-+| abccs | f | a1 | abccs | f | a2 |+-+-+-+上面例子中,由于作者姓名、性别、文章记录在两个不同表内,因此必须使用组合来进行查询。必须要指定一个表中的记录如何与其它表中的记录进行匹配。注意:如果第二个表 title 中的 writer 列也取名为 name(与 mytable 表中的 name列相同)而不是 writer 时,就必须用 mytable.name 和 title.name 表示,以示区别。再举一个例子,用于查询文章 a2的作者、出生地和出生日期:mysql select title,writer,birthaddr,birth

5、 from mytable,title- where mytable.name=title.writer and title=a2;+-+-+-+-+| title | writer | birthaddr | birth |+-+-+-+-+| a2 | abccs | china | 1977-07-07 |+-+-+-+-+修改和备份、批处理有时我们要对数据库表和数据库进行修改和删除,可以用如下方法实现:1、增加一列:如在前面例子中的 mytable 表中增加一列表示是否单身 single:mysql alter table mytable add column single char(

6、1);2、修改记录将 abccs 的 single 记录修改为“y” :mysql update mytable set single=y where name=abccs;现在来看看发生了什么: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

7、 |+-+-+-+-+-+3、增加记录前面已经讲过如何增加一条记录,为便于查看,重复与此:mysql insert into mytable- values (abc,f,1966-08-17,china,n);Query OK, 1 row affected (0.05 sec)查看一下:mysql select * from mytable;+-+-+-+-+-+| name | sex | birth | birthaddr | single |+-+-+-+-+-+| abccs | f | 1977-07-07 | china | y | mary | f | 1978-12-12

8、| usa | NULL | tom | m | 1970-09-02 | usa | NULL | abc | f | 1966-08-17 | china | n |+-+-+-+-+-+4、删除记录用如下命令删除表中的一条记录:mysql delete from mytable where name=abc;DELETE 从表中删除满足由 where 给出的条件的一条记录。再显示一下结果:mysql select * from mytable;+-+-+-+-+-+| name | sex | birth | birthaddr | single |+-+-+-+-+-+| abccs

9、| f | 1977-07-07 | china | y | mary | f | 1978-12-12 | usa | NULL | tom | m | 1970-09-02 | usa | NULL |+-+-+-+-+-+5、删除表:mysql drop table *(表1的名字) ,*表2的名字;可以删除一个或多个表,小心使用。6、数据库的删除:mysql drop database 数据库名;小心使用。7、数据库的备份:退回到 DOS:mysql quitd:mysqlbin使用如下命令对数据库 abccs 进行备份:mysqldump -opt abccsabccs.dbbabccs.dbb 就是你的数据库 abccs 的备份文件。8、用批处理方式使用 MySQL:首先建立一个批处理文件 mytest.sql,内容如下:use abccs; select * from mytable; select name,sex from mytable where name=abccs;在 DOS 下运行如下命令:d:mysqlbin mysql mytest.sql在屏幕上会显示执行结果。文章由 http:/www.vip400.cc 整理

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报