mysql数据库第四版_MySQL数据库(四)

操作数据库表的内容:

-- 向表中插入数据:

insert into table_name values(now(),'a');

insert into student (id,name,sex) values(1,'zhangsan','nan');

-- 向表中插入多条记录

insert into student (name,sex) values('xiaoming','nan'),('xiaohong','nv');

-- 使用insert….select插入结果,在insert语句中使用select子句可以将源表的查询结果添加到目标表中

语法格式如下。

insert into 目标表名[(字段列表1)]

select 字段列表2 from 源表 where 条件表达式

-- 使用replace插入新记录

replace语句的语法格式有三种语法格式。

语法格式1:

replace into 表名 [(字段列表)] values (值列表)

语法格式2:

replace [into] 目标表名[(字段列表1)] select (字段列表2) from 源表 where 条件表达式

语法格式3:

replace [into] 表名

set 字段1=值1, 字段2=值2

replace语句的功能与insert语句的功能基本相同,不同之处在于:使用replace语句向表插入新记录时,如果新纪录的主键值或者唯一性约束的字段值与已有记录相同,则已有记录先被删除(注意:已有记录删除时也不能违背外键约束条件),然后再插入新记录。

-- 删除表中的数据

delete from table_name where 删除条件;

-- 使用truncate清空表记录

truncate table用于完全清空一个表,语法格式如下。

truncate [table] 表名

-- 更新表中的数据

update table_name set 列名称=新值 where 更新条件;

若省略了where子句,则表示修改表中的所有记录。

-- 使用谓词distinct过滤结果集中的重复记录

SELECT DISTINCT 列名称 FROM 表名称

-- 使用谓词limit查询某几行记录

select 字段列表

from 数据源

limit [start,] length;

-- 使用from子句指定数据源

from 表名1 [ 连接类型 ] join 表名2 on 表1和表2之间的连接条件

连接类型分为:

inner连接(内连接)和outer连接(外连接)

内连接分为三种:自然连接、等值连接、非等值连接。

外连接又分为left(左外连接,简称为左连接)、right(右外连接,简称为右连接)以及full(完全外连接,简称完全连接)。

内连接

-- 自然连接

select * from course natural join grade;

-- 等值连接 :使用等于=比较连接列的列值,在查询结果中列出接连表中的所有列,包括其中的重复列:

select * from R join S where R.C = S.C;

或者

select * from R inner join S where R.C = S.C;

-- 非等值连接 :在连接条件中,可以使用其他比较运算符,比较被连接列的列值,如:、!=等。

外连接

-- 左连接(left join)on和后面的条件不能省略

select * from 表1 left join 表2 on 表1和表2之间的连接条件

查询结果集中须包含表1的全部记录,然后表1按指定的连接条件与表2进行连接,若表2中没有满足连接条件的记录,则结果集中表2相应的字段填入NULL。

-- 右连接的语法格式 right join / right outer join?

select * from R right join S on R.C = S.C;

查询结果集中须包含表2的全部记录,然后表2按指定的连接条件与表1进行连接,若表1中没有满足连接条件的记录,则结果集中表1相应的字段填入NULL。

-- 全连接

select * from course full join grade;

-- 多表连接

select * from R join S on R.C = S.C join T on S.C=T.C;

为表或字段取别名

字段名 [AS] 别名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 1224 pages Data: September 8, 2008 Description: The unexpected pleasure of reading books about databases is that they are often written by authors with highly organized minds. Paul DuBois and his editors at New Riders have assembled MySQL with a clarity and lucidity that inspires confidence in the subject matter: a (nearly) freely redistributable SQL-interpreting database client/server primarily geared for Unix systems but maintained for Windows platforms as well. What isn't "free" about MySQL (the application) is its server's commercial use; all clients and noncommercial server use are free. DuBois's tome isn't free either, but its list price is modest in light of its value and the value of its namesake. The volume is superbly organized into 12 chapters and 10 appendices and contains a concise table of contents and a comprehensive 50-page index. It is peppered with references to the online HTML documentation that comes with the source and binary distributions (which are available and easy to install in stable rpm and tar releases.) The first third of MySQL is an excellent instruction tool for database newbies; the second third is a detailed reference for MySQL developers; and the last third consists of clearly annotated appendices, including C, Perl (but not Python), and PHP interfaces. Perhaps as an indication of the collective will of the developers of MySQL, DuBois does not separate Windows 95/98/NT design or development specifics from its main discussions. Platform-independent design is a goal, not a reality, and users will have to rely on newsgroups and mailing lists for details. Moreover, security issues are addressed in a mere 18 pages, a large part of which is devoted to standard Unix file and network-access permissions. Next to nothing is mentioned about defense against common hacking strategies, the use of secure shell interfaces, or access encryption. Although it is nearly 800 pages in length, DuBois's book is thankfully not encyclopedic. It is a valuable précis of the MySQL database, and its easy-to-skim look and feel will make it an excellent browse for database experts who want to know what is and is not possible within MySQL, the application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值