数据库之基本操作

假设表中有四个字段  (列名1,列名2,列名3,列名4)

1.增(add):

添加语句的语法格式

insert into table_name(你所操作的表名) (列名1,列名2,列名3,列名4)values (值1,值2,值3,值4);

或者

insert into table_name values (值1,值2,值3,值4);

还有一个就是批量插入(假设要插入三条信息)

insert into table_name(你所操作的表名)  (列名1,列名2,列名3,列名4)values (值1,值2,值3,值4),

(值1,值2,值3,值4),(值1,值2,值3,值4)

注意:必须严格按照表里字段的顺序还有括号里的值的数量要与

表里字段数量一致,也就是一 一对应,一般开发采用第一种。

2.删(delete):

    delete from 表名 where 条件;

3.改(update):

      update 表名 set 列1=值1,...列n=列2 where 条件 and 条件 ;

      update 表名 set 列1=值1,...列n=列2 where 条件 or 条件 ;

    update 表名 set 列1=值1,...列n=列2 where id in(1,2,3);id=1,id=2,id=3的记录都被改掉

 update 表名 set 列1=值1,...列n=列2 where id not in(1,2,3);id=1,id=2,id=3的记录都不改掉

条件的写法

例如 

id == n 这是非法的 在数据没有这样的写法

id <> n id不等与n 

与 && 用 and 表示

或 || 用or表示

非 ! 用not来表示    

注意:更新语句必须有条件,不然表里所有的记录都会被更新

4.查():

    查询所有的字段 

select * from 表名;

sql语句:select * from users; *意味着全部


条件查询:

select 列名,列名,...,列名 from 表名 where 列名=‘值’;

例如:查询性别是女的记录的用户名和密码

sql语句:select username,pwd from user where gender = '女';

猜你喜欢

转载自blog.csdn.net/pannubi/article/details/80368133
今日推荐