管理表结构:
1.插入
mysql> insert into db1.user values(22,"bob","x","3000,3000","test user","/home/bob","/bin/bash");
添加一条记录,给所有字段赋值
mysql> insert into db1.user(name,uid,gid) values("tom","4003,4003");
添加一条记录,指定字段赋值
ps:
字段值要与字段类型相匹配 字符类型的字段,要用""号括起来
依次给所有字段赋值时,字段名可以省略
只给部分字段赋值时,必须明确写出对应的字段名称
没有赋值的字段使用默认值或自增长赋值
2.查询
mysql> select name,uid,gid from user ;
mysql> select * from user;
mysql> select name,uid from user where id<=3;
查询表记录,*表示所有字段 查看当前库表记录时库名可以省略 字段列表决定显示列个数
条件决定显示行的个数。
3.更新表记录 update
mysql> update 库名.表名 set 字段名=值,字段名=值;
mysql> update user set password="a",comment="student";
mysql> update user set password="x",comment="root" where id=1;
字段值要与字段类型相匹配 对于字符类型的字段,值要用双引号括起来
若不使用where限定条件,会更新所有记录字段值
限定条件时,只更新匹配条件的记录的字段值
4.删除表记录 delete
mysql> delete from db1.user;
mysql> delete from db1.user where id>=22;
不添加条件删除表中的所有行
匹配条件:
单独说一下mysql的匹配条件
1.基本匹配条件 适用于select、update、delete
数值对比,字段必须是数值:
= 等于
大于
= 大于等于
<= 小于等于
!= 不等于
mysql> select * from user where uid=2;
mysql> select name,uid,gid from user where uid=gid;
mysql> select name,uid,gid from user where uid!=gid;
2.字段对比,必须是字段的类型
= 等于
!= 不等于
is null 空
is not null 非空
mysql> select name from user where name="mysql";
mysql> select name,shell from user where shell!="/bin/bash";
mysql> select id,name from user where name is null;
mysql。 select id,name from user where name is not null;
3.逻辑匹配
多个判断条件时使用and 逻辑与多个判断条件必须同时成立
or 逻辑或多个判断某个条件成立即可
!或not逻辑非取反
mysql> select name,uid,gid,shell from user where name="root" or uid=100 or gid=100;
4.在范围内匹配|去重查询
in 在 里
not in 不在里
between and 在 之间
distinct 字段名
mysql> select name,uid from user where uid in (1,10,34);
mysql> select name from usere where name in ("mysql","bin","root");
mysql> select name,shell from user where shell not in ("/bin/bash","/sbin/nologin");
mysql> select * from user where id between 10 and 30;
mysql> select distinct gid from user;
网友评论