数据的基本操作:简单的增、删、改、查

1 插入实体

插入实体(数据行)的语法: insert [into] <表名> [列名] values <值列表>。

插入实体的SQL语句示例:

insert into Students (StudentName, Gender, Birthday, Age,
 StudentIdNo, PhoneNumber, StudentAddress, ClassId) 
values('马小李','男','1990-02-07',21,120223199002078915,
'022-8888888','天津曙光路79号',4)

注意事项:

  • 列名个数等于对应值的个数。
  • 非值类型的数据,必须放在单引号内。
  • 数据值的类型必须与定义的字段类型一致。

2 查询实体

查询全部实体: select * from <表名>。
条件查询:

select StudentId, StudentName, Gender from Students where Age>=22

数据库常用的运算符号如下:
在这里插入图片描述


3 更新实体

更新实体语法: update <表名> set <列名=更新值> [where<更新条件>]。
更新实体的SQL语句示例:

update Students set StudentAddress='天津市东丽区',PhoneNumber='1234567' where StudentId=100003

注意: 使用update语句时,一定要注意where条件的配合使用。


4 删除实体

删除数据表中数据语法:

  • delete from <表名> [where <删除条件>]
  • truncate table <表名>

删除实体的SQL语句示例:

delete from Students where StudentId=100002 
-- delete删除数据时,要求改记录不能被外键引用,删除后标识列继续增长

delete from Students -- 删除整张表

truncate table Students 
-- 删除整张表,truncate删除数据时,要求删除的不能有外键约束;删除后重新条件
-- 数据标识列重新编排;truncate比delete执行速度快,而且使用的系统资源和事物日志资源更少。

注意: 使用delete删除语句时,一定要注意where条件的配合使用。

猜你喜欢

转载自blog.csdn.net/SlowIsFastLemon/article/details/86701298