java基础 第十五章(数据库)

一、创建表

    格式:create table 表名(

                   字段名 字段类,……

               )

    例:create table student(

                id int primary key auto_increment,

                name varchar(6)

            )

    补充:字段类型

                varchar , int , double , text……

二、删除表

    格式:drop table 表名

三、插入表数据

    格式:insert into 表名(字段名,……) values(值,……)

    例:insert into student(id,name) values(1,'hello')

四、删除表数据

    格式:delete from 表名 where 条件

    例:delete from student where id = 2

五、关系运算符

    (1)> , >= , < , <= , <>

    (2)between and , not between and

             例:select * from student where id between 1 and 3

    (3)in() , not in()

             例:select * from student where id in(1,2,3)

    (4)like  通配符:% ——>代表多个字符    _  ——>代表一个字符

             例:select * from student where name like 'h%'

     (5)is null , is not null

              例:select * from student where id is not null

六、更新数据

    格式:update 表名 set 字段名 = 新值 ,…… where 条件

    例:update student set id = id + 1

七、聚合函数

    (1)count() 别名 ——>记录数

             例:select count(*) 数量 from student

    (2)sum() 别名 ——> 总和

             例:select sum(id) 总和 from student

    (3)avg() 别名  ——> 平均值

             例:select avg(id) 平均值 from student

    (4)max() 别名 ——> 最大值

             例:select max(id) 最大ID from student

    (5)min() 别名 ——> 最小值

             例:select min(id) 最小ID from student

      注意:如果有分组必定要有聚合函数

八、查询

    格式:select 字段名,……(*) from 表名 where 条件 [group by 分组] [having 分组条件] [order by 排序 desc降序/asc默认升序] 

九、多表操作

    (1)一对多:创建外键约束 (student表,class表)

             格式:alter table 表名 add constraint 别名 foreign key() references 表名()

             例:alter table student add constraint sc foreign key(classId) references class(classId)

    (2)多对多:创建一个中间表(student表,course表,sc表)

             格式:alter table 表名 add constraint 别名 primary key()

             例:alter table sc add constraint ssc primary(sid,cid)

    (3)两表连接

             格式:select  字段名 from 表1 别名 inner join 表2 别名 on 条件

             例:select * from student  inner join class on student.classid = class.classId

    

             

原文地址:https://www.cnblogs.com/catcoffer/p/9138774.html