表的增减改查
desc tablename;查看表结构
drop table name;删除表
字段增改删:格式
alter table tablename add column name varchar(20);
alter table tablename modify column name varchar(20);
alter table tablename drop column name;
alter table tablename change column oldname newname varchar(10)*;
数据的增删改
增加数据
insert into tablename VALUES(1,'张三','男',20); 插入字段:insert into
插入部分字段
insert into tablename (id,NAME) value(2,'李四');
注意:
tablename(sid,ssex)
values(2,'小明') 括号里是对应的
查询所有:
select * from tablename;
修改数据:格式: update tablename set 列名='改的内容' where id=x;
update tablename set name="小红" where id=2;
删除
删除整个表:delete from tablename;
带条件删除:delete from tablename where id=2;
彻底删除:truncate table tablename
delete 和truncate区别d可以还原 t彻底删除
pm:
查询(举例)
1.1查询所有列:select * from tablename;
1.2查询指定列:select 字段,字段 from tablename;
1.3查询时添加常量列:select 字段,字段,'java0322' AS 'class' fromtablename;
查询时给字段起别名:select sid AS '编号',sname AS '姓名',ssex FROM student;
1.4查询时合并列:select sname,(字段+字段) AS '总成绩' from 表名;
注意:合并列只能数值类型字段
1.5查询去除重复记录:select distinct 字段 from 表名;
注:字段 ssex 和 (ssex) 是一样的
1.6条件查询:条件(where) 逻辑条件:and(与) or(或)
select * from tablename where 字段=条件 and 字段=条件;
select * from tablename where 字段=条件 or 字段=条件;
1.6.1比较条件&#xff1a;> <>&#61; <&#61; &#61; <>(不等于) between and (不等于>&#61; 且<&#61;)
查询jsp成绩<60的&#xff1a;select * from tablename where jsp<60;
查询jsp<60且html>60:select * from tablename where jsp<60 andhtml>60;
jsp成绩不等于100的同学:select * from tablename where jsp<>100
查询html 60到70&#xff1a;
select * from tablename where html>&#61;60 and html<70;
select * from tablename where html between 60 and 70;
1.7 判断条件&#xff08;null 空字符串&#xff09;: is null / is not null / &#61;&#39;&#39; /<>&#39;&#39;
IS NULL
is not null
字符串就是空 &#61;&#39;&#39;
sname <>&#39;&#39;
1.8 模糊查询&#xff1a;
模糊条件&#xff1a;like % 表示任意字符 _:表示一个字符
查询姓‘张’的学生&#xff1a;select * from tablename where name like &#39;张%&#39;;
查询姓张&#xff0c;且只有两个字: 把 &#39;张%&#39; 改成 &#39;李_&#39;;