数据库操纵语言
DML
DML
有三条语句
:insert
、
update
、
delete.
一、insert:插入数据
1
插入一条数据
insert into <
表名
>
[
列名
] values(<
值列表
>)
insert into stuInfo(stuName,stuNo,stuSex,stuAge,stuAddress) values('
张三丰
'
,
's25308'
,
'
男
'
,
24,default)
2
插入多行
(1)
通过
union
关键字合拼数据进入插入
union
用于将两个不同数据库或查询结果组合到一个新的结果集
.
insert into <
表名
>(
[
列名
])
select <
值列表
>
union
select <
值列表
>
union
select <
值列表
n>
insert into myTable(stuName,stuSex,stuAge)
select 'union1','
男
'
,
20 union
select 'union2','
女
'
,
21 union
select 'union3','
男
'
,
22
(2)
通过
insert select
语句将现有表中的数据添加到新表
insert into <
新表名
>(
[
新表列名
])
select <
源表列名
>
from <
源表
>
insert into myTable(stuName,stuSex,stuAge)
select stuName,stuSex,stuAge
from stuInfo
注意
:
新表名必须是已经存在的且表结构与源表名的表结构相同
.
(3)
通过
select into
将现有表中的数据添加到新表
select <
源表列名
>
into <
新表名
>
from <
源表名
>
select stuName,stuSex,stuAge,stuSeat into myTable from stuInfo
注意:
myTable
在执行查询时创建,无须事先创建。
二、
update:
数据更新
update <
表名
>
set<
列名
=
更新值
>
[where<
更新条件
>]
update myTable set stuSex='
女
'
,
stuAge=22 where stuName='
张秋丽
'
三、
delete
:删除数据
delete from <
表名
>
[where<
删除条件
>]
delete from myTable where stuName='
张三丰
'
四、
truncate table
删除数据
truncate table <
表名
>
truncate table myTable
用来删除表中的所有行,但表的结构、列、约束、索引等不会被删除。
功能上和没有带
where
子句的
delete
相同,但是
truncate table
执行速度比
delete
快。