Data Operation - CRUD

Data Operation - CRUD

Simple Query

select * from 表名
例:查询所有学生数据
select * from students

adding data

Add a row of data

Formats: all fields set value, the order of columns in the table corresponds to the value of

  • Description: growing primary key column is automatically required when inserting placeholder, typically using default or null to zero or placeholder, after successful insertion actual data subject
insert into 表名 values(...)

Example: Insert a student, set all the fields of information

insert into students values(0,'亚瑟',22,177.56)

Format II: part of the field settings, and the order of the fields is given a value corresponding to

insert into 表名(字段1,...) values(值1,...)

Example: Insert a student, setting only the name

insert into students(name) values('老夫子')

Adding multiple rows of data

One way: write multiple insert statements separated by semicolons between statements

insert into students(name) values('老夫子2');
insert into students(name) values('老夫子3');
insert into students values(0,'亚瑟2',23,167.56)

Second way: Write an insert statement, providing a plurality of data, separated by commas between the data

格式一:insert into 表名 values(...),(...)...
例:插入多个学生,设置所有字段的信息
insert into students values(0,'亚瑟3',23,167.56),(0,'亚瑟4',23,167.56)
格式二:insert into 表名(列1,...) values(值1,...),(值1,...)...
例:插入多个学生,只设置姓名
insert into students(name) values('老夫子5'),('老夫子6')

modify

格式:update 表名 set 列1=值1,列2=值2... where 条件

Example: Modify the id student data 5, the name was changed to Di Renjie, age changed to 20

update students set name='狄仁杰',age=20 where id=5

delete

格式:delete from 表名 where 条件

Example: Delete the id data for students 6

delete from students where id=6

Tombstone: For critical data, can not easily execute delete statement delete Once deleted, data can not be restored, then you can logically deleted.

1, add a field to the table, whether the deleted data representing generally named isdelete, 0 represents an deletion represents to delete the default value is 0

2. When you want to delete a data, only need to set this data field is 1 isdelete

3, later in the query data, just check out the isdelete data 0

例:
1、给学生表添加字段(isdelete),默认值为0,如果表中已经有数据,需要把所有数据的isdelete字段更新为0
update students set isdelete=0
2、删除id为1的学生
update students set isdelete=1 where id=1
3、查询未删除的数据
select * from students where isdelete=0
Published 240 original articles · won praise 77 · views 80000 +

Guess you like

Origin blog.csdn.net/dpl12/article/details/104196851