数据库记录处理
新建一个表 名字为t3,
属性id为 int类型 主键,给自增,
Name 为 varchar类型 非空约束
Age int 类型,
Sex int类型,非空属性
create table t3(
id int primary key auto_increment,
name varchar(20) not null,
age int ,
sex int not null
);
为表中添加一条记录
insert into t3 (id,name,age,sex)value(1,’张三’,17,1);
为表中添加一条记录,省略字段名
insert into t3 value(2,’赵六’,18,1);
添加指定记录
insert into t3 (id,name,sex)value(3,’刀妹’,2);
为表中天添加多天记录,有age为空值的
insert into t3 value(4,’亚索’,18,1),(5,’男枪’,20,1),
(6,’女枪’,22,2),(7,’男刀’,21,1),(8,’盲僧’,35,2),
(9,’瑞文’,17,2),(10,’蜘蛛’,20,2),(11,’鳄鱼’,18,1);
insert into t3 (id,name,sex)value
(12,’卡牌’,1),(13,’李白’,1),(14,’寒冰’,2),
(15,’卡特’,2),(16,’宫本’,1),(17,’女警’,2),
(18,’静静’,2),(19,’摩达’,1),(20,’发条’,2);
修改数据,把id小于5的sex全部值为2;也可以不写where条件表达式,那样所有的sex值全为2;
update t3 set sex = 1 where id<5;
删除数据:把id为19的删除掉,不写where表达式,数据全部删除掉
delete from t3 where id = 19;
查询数据 可以使用and or,and的优先级高于or,
查询id的数据
select id from t3;
查询多列,查询的顺序没有必要字段名一样,先查name,在查id;
select name,id from t3; 查询结果 name在id前面;
可以用*号代表所有字段
select * from t3;
这里面还可以使用关系运算符
查询名字叫“静静”的人的性别;
select sex from t3 where name=’静静’;
查询性别为女的数据
select * from t3 where sex=2;
in 关键字的使用,相当于集合 可以使用not 相当于取反就是不是他
查询年龄为(18,19,22,)的数据
select * from t3 where age in(18,19,22);
select * from t3 where age not in(18,19,22);
between and 的使用,betwenn x and y,就是从x到y;
select * from t3 where age between 18 and 22;
效果是和
select * from t3 where age>=18 and age<=22;一样的;
查询是空值:is null 可以使用 not;
select * from t3 where age is null;
select * from t3 where age is not null;
distinct 的使用,取出重复的值,查询 性别sex 只会出现1,2两种结果,可用于多个字段
select distinct sex from t3;
like 模糊查询 可以使用not; \转义字符,查询% _就要使用转义字符了 \% _;
%任意长度的字符串包括空字符串;
_下划线通配符,表示一个字符。可以是空字符串
查询名字以寒 开头的数据
select * from t3 where name like ‘寒%’;
查询名字中有张的数据
select * from t3 where name like ‘%张%’
查询名字中不包含 寒 的数据;
select * from t3 where name not like ‘%寒%’;
and or % _
这可以随意组合查询你想查询的东西,这就要看你的智慧喽;