1. 创建数据库表
创建数据库:create table 表名( _id integer primary key autoincrement,//主键id自增长 name varchar(40), age integer(20), birthday datetime //最后一行不用写 逗号(,) );
2. 修改字段名
遇到创建表的时候字段写错了,需要修改字段名称: alter table 表名 change 旧的字段名 新的字段名 colType; 例子: alter table person change age sex varchar(20); 将person表中年龄的字段改为性别的
3. 修改字段类型
如果主键本来是int,现在要改成varchar类型: 方法一:alter table tableName change colName colName colType[(size)]; 例子: alter table person change age age varchar(20); 方法二:alter table tableName modify colName colType[(size)]; 例子: alter table person modify age varchar(20);
4. 添加新字段
如果实体对象有新属性了,需要添加表字段: alter table tableName add newColName colType[(size)]; 例子: alter table person add sex varchar(20); 添加了性别字段
5. 删除字段
如果字段属性不需要了,应该删除掉: alter table tableName drop colName; 例子: alter table person drop sex; 删除性别字段
1. 插入数据
向数据表中添加新数据,使用关键字:insert 方式一:insert into 表名 values("001","zhangsan","23"); 例子: insert into person values ("001","张三","23"); 添加了一条数据id为001,姓名张三,年龄23 方式二:insert into 表名(name,age) values("lisi","24"); 例子: insert into person(name,age) values("李四","24");
2. 修改数据
对已经存在于数据表中的数据进行修改操作,使用关键字:update update 表名 set colName1=newValue1,colName2=newValue2... where 条件 例子:update person set name="张三",age="23" where name="李四"
3. 删除数据
对于数据表中废弃没有用的数据需要清理删除,使用关键字:delete delete from 表名 where 条件; 例子: delete from person where name="张三"; 删除name为张三的数据,没有where表示删除所有数据
4.查询数据
查询是使用会最频繁的操作,使用关键字:select
select * from 表名 where 条件; select 字段列表 from 表名 where 条件; 例子: select * from person; 查询表中所有数据
1). < > <= >= <> 类型
分别表示小于 大于 小于等于 大于等于 不等于
select * from person where age>23; 查询person表中年龄大于23的
2). 在一组数据中 in
select * from person where age in (22,33,55); 查询年龄在22,33,55的所有数据
3). 条件并列 and
select * from person where age>33 and id in ('2222','3333','4444'); 查询年龄大于33 并且 id在2222,3333,4444中的所有数据
4). 在两者之间 between … and …
select * from person where age between '23' and '33'; 查找age在 23 和 33之间的所有信息
5). 模糊查询:like 配合 %使用,%代表位置的字符串部分
select * from person where name like '%张%'; 查询name包含 张 的数据 select * from person where name like '张%'; 查询name 以 张开头 的数据
6). 查询补充:如果想查询字段为null的 或者 字段不为null的怎么判断?
查询为null的: select * from person where age is null; select * from person where isnull (age); 查询不为null的: select * from person where age <> "null"; select * from person where age is not null;
5. 排序、 聚集函数、分组
1). 排序
对查询的结果根据某一列进行排序,顺序、倒序,使用关键字 order by desc/asc
select * from 表名 order by 字段 asc/desc; 例子: select * from person order by age desc; 根据年龄倒序排列 select * from person where age>22 order by age desc; 加上where条件, 年龄大于22的倒序排列 注意order by 应该放查询语句最后
2). 聚集函数 这些函数是sql给我们提供好的函数,用于各种运算
(1). count():统计记录数,count(*)表的总记录数,count(字段)对应字段不为NULL的记录数
select count(*) from person;
person表中的总数
select count(age) from person;
person表中age不为null的总数
(2). sum():求和函数统计列数值的总和
select sum(age) from person;
person表中age的总和
select sum(age)/count(*) from person;
使用count()和sum()计算平均年龄: person表中平均年龄
(3). avg():计算平均数
select avg(age) from person;
person表中平均年龄
(4). max()最大值、 min()最小值
select max(age) from person;
最大年龄
select min(age) from person;
最小年龄
3). 分组操作 将同一类型的数据分成一组查看,使用关键字 group by
(1). 将按照age年龄分组,
select age, count(*) from person group by age;
查看每个年龄有多少人
(2). 如果在分组时需要聚集函数 作为查询条件,就不能使用where 子句了,前面有演示,需要使用 having关键字来代替
select sum(age) from person having sum(age)>20;
年龄大于20的人年龄总和为
4). 分页查询 limit
如果想查询某几条连续的数据,从第几条开始,查询多少条,使用关键字:limit
select * from 表名 limit 记录数
例子: select * from person limit 3;
查询前3条记录
select * from 表名 limit 开始位置,记录数
例子: select * from person limit 1,3;
查询 limit 1,3 : 说明记录是从1开始计算的,3的位置结束
以上是对数据库和数据库表的一些命令操作,有错误还望指正!