友情提示:本文共有 2059 个字,阅读大概需要 5 分钟。
一、增:有2种方法;
1.使用insert插入单行数据:
语法:insert [into] <表名> [列名] values <列值>;
例:insert into Strdents (姓名,性别,出生日期) values ("王伟华","男","1983/6/15")
注意:如果省略表名,将依次插入所有列;
2.使用insert,select语句将现有表中的 数据添加到已有的新表中;
语法:insert into <已有的新表> <列名> select <原表列名> from <原表名>
例:insert into addressList ("姓名","地址","电子邮件")select name,address,email
from Strdents
注意:查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致
二、删:有2种方法;
1.使用delete删除数据某些数据;
语法:delete from <表名> [where <删除条件>]
例:delete from a where name="王伟华"(删除表a中列值为王伟华的行)
注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名"
2.使用truncate table 删除整个表的数据;
--增删改查CRUD
select *from table_class --查询table_class所有数据
select *from table_student
--插入数据(C)
--单条数据的插入
-- 列的数目=值的数目 数据需要满足约束 数据类型保持一致(存在默认转换)
-- 自增列不能手动赋值
insert into table_class(class_name,create_time) values("计110班",default)
insert into table_student(stu_number,stu_name,stu_age,birthday,class_id)
values("1000000001","james",20,"1996-06-06",null)
--批量插入数据
insert into table_class(class_name)
select "光101班"union
select "光102班"union
select "光103班"union
select "光104班"
insert into table_class(class_name)
values("光105班"),("光106班"),("光107班"),("光108班").
一个实例.
create table table_test
id int,
name varchar(20).
--1.先查询不重复的数据,将数据保存到一张新表.
select distinct * into table_new from table_test .
--2.删除table_test的数据.
delete from table_test.
--3.将新表中的数据迁移回table_test.
insert into table_test(id,name) select *from table_new.
--4.删除那张新表.
drop table table_new.
SQL常用语句一览;
(1)数据记录筛选:
sql="select*from数据表where字段名=字段值orderby字段名[desc]"
sql="select*from数据表where字段名like"%字段值%"orderby字段名[desc]"
sql="selecttop10*from数据表where字段名orderby字段名[desc]"
sql="select*from数据表where字段名in("值1","值2","值3")"
sql="select*from数据表where字段名between值1and值2"
(2)更新数据记录:
sql="update数据表set字段名=字段值where条件表达式"
sql="update数据表set字段1=值1,字段2=值2……字段n=值nwhere条件表达式"
(3)删除数据记录:
sql="deletefrom数据表where条件表达式"
sql="deletefrom数据表"(将数据表所有记录删除);
(4)添加数据记录:
sql="insertinto数据表(字段1,字段2,字段3…)values(值1,值2,值3…)";
sql="insertinto目标数据表select*from源数据表"(把源数据表的记录添加到目标数据表);
SQL SELECT DISTINCT 语句:在表中,可能会包含重复值萊垍頭條
这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词 DISTINCT 用于返回唯一不同的值。INSERT INTO 语句:INSERT INTO 语句用于向表格中插入新的行。Update 语句:Update 语句用于修改表中的数据。DELETE 语句:DELETE 语句用于删除表中的行。
本文如果对你有帮助,请点赞收藏《sql增删改查基本语法》,同时在此感谢原作者。