数据库update的用法

数据库update的用法,第1张

数据库update的用法的用法你知道吗?下面我就跟你们详细介绍下数据库update的用法的用法,希望对你们有用。

数据库update的用法的用法如下:

SQL语句中的更新语句update是最常用的语句之一,下面将为您介绍update语句的三种使用方法,供您参考,希望对您有所帮助。

一、环境:

MySQL-5.0.41-win32

Windows XP professional

二、建立测试环境:

DROP TABLE IF EXISTS t_test

CREATE TABLE t_test (

bs bigint(20) NOT NULL auto_increment,

username varchar(20) NOT NULL,

password varchar(20) default NULL,

remark varchar(200) default NULL,

PRIMARY KEY  (bs)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk

INSERT INTO t_test VALUES (1,'lavasoft','123456',NULL)

INSERT INTO t_test VALUES (2,'hello',NULL,NULL)

INSERT INTO t_test VALUES (3,'haha',zz,tt)

三、测试

1、set一个字段

在表t_test中设置第二条记录(bs为2)的password为'***'。

update t_test t

set t.password = '***'

where t.bs = 2

2、set多个字段

在表t_test中设置第一条记录(bs为1)的password为'*'、remark为'*'。

update t_test t

set t.password = '*', t.remark = '*'

where t.bs = 1

3、set null值

在表t_test中设置第三条记录(bs为3)的password为null、remark为null。

update t_test t

set t.password = null, t.remark = null

where t.bs = 3

这个是按照标准语法写的,在不同的数据库系统中,update还有更多的写法,但是标准写法都是支持的。以上三个例子为了说明情况,每次都更新一行。在实际中,可以通过where语句约束来控制更新行数。

当表A中的的一列是另外一个表B的外键(foreign key)时,删除表A的列引起表B的变化

1.delete cascade : 删除子表中所有的相关记录

2.delete set null : 将所有相关记录的外部码字段值设置为NULL

例:

1.alter table students add constraint fk_classid foreign key(class_id) references classes(class_id) on delete cascade

2.alter table students add constraint fk_classid foreign key(class_id) references classes(class_id) on delete set null


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9882999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存