数据操作主要包括三方面:增,删,改。
###概述
DML语句主要有以下三个功能:插入新数据(insert into) ,修改已有数据(update),删除数据(delete from)
create table tml
(
tml_id int auto_increament,
tml_name varchar(255) not null,
primary key(tml_id)
);
creat table student
(
stu_id int auto_increment primary key,
stu_name varchar(255),
#指定java_tml 参照到tml_id列
java_tml int ,
constraint student_tml_fk foreign key (java_tml) references tml_table (tml_id) //为外键约束指定名称
); 对此表进行操作
insert into
# 只有建立数据表才可插入数据,仍然以tml表为依据进行操作
insert into tml_table (tml_name) values ('xyz'); //在tml表中的tml_name字段添加一个值为xyz的数据
# 若不指定列,则需要为所有列指定值,若某列值不确定,则需要分配一个null值
insert into tml_table values(null,'abc');
#若主表中有记录,则可以在从表中添加纪录
insert into student_table values(null,'abc',2);
#同时插入多组值
insert into tml_table values(null,‘yeeku’),(null,'tttt');
#使用子查询语句插入
insert into student_table(student_name)
select tml_name from tml_table; //一次插入多条记录,只要求一列对应一列和数据类型匹配
###update
#只修改tml_id>1的记录
update tml_table
set tml_name='猪八戒'
where tml_id>1 //若无where关键字,所有记录都将被更改
###delete from
使用该语句不需要指定列名,因为通常该语句是整行删除数据记录
delete from tml_table
where tml_id>2; //如果没有where条件,则删除整个表的数据,如同truncate一样