数据库操作-基础

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43138570/article/details/102566989

数据库

数据库,存数据,能进行增删改查

Oracle 大型网站、大的集团、大数据
SQL Server 大中型企业 偏慢 windows
mysql 中小型网站,大小中企业
Informix 大型网站, 大小中企业
DB2 中小企业
sybase 中小企业
access 单机版 windows
sqlite 手机或移动设备
excel

库: 即数据库,用于管理所有的表,里面包含表
表:即数据表,根据数据性质,定义多个数据表,有时表与表之间会有关联
视图:通过sql语句,检索出来虚拟的表格,可以再次通过sql语句对视图进行检索
事件:也称为触发器,是用sql语句编写的函数,当数据表中有增、删、改时自动被调用
存储过程: 是用sql语句编写的函数,可以通过sql语句来调用

linux下操作mysql数据库
1.启动和停止mysqld服务
service mysqld start
service mysqld stop

2.创建管理帐号
mysqladmin -u root password 123456

3.进入mysql数据库工具
mysql -uroot -p123456

在mysql数据库中,每操作一个命令,都是;号结尾的

4.查看当前建立的数据库
mysql> show databases;

5.创建和删除一个数据库
其中schoolinfo是数据库名称
创建数据库
mysql> create database schoolinfo;
删除数据库
mysql> drop database schoolinfo;

6.进入数据库

mysql> use schoolinfo;

7.查看数据表
mysql> show tables

8.退出
exit 保存退出
quit 不保存退出

创建表(开发时用)***、更改表(升级时用)、删除表(升级时用)
(一)、创建表格

1.建数据库

2.建数据表
表格的每一列都有个名称,称为字段名(结构体的成员)
字段必须指定 名称、类型、长度、是否为空、默认值、是否为主键、是否值为自增。这些统称为约束

  主键:用于唯一识别一条记录(即一行),通常用整型做主键,不可以重复。两种方式填写:人工、自动
  类型:整数、小数、字符串、日期和时间、二进制数据、文本数据
        通用数据类型
        int 
        float
        double
        char    (n)    //长度固定
        数据库常用类型
        integer  ,     //32 位的整数  主键通常使用该类型
        smallint ,	   //16 位的整数
        tinyint  ,     //8位
        decimal(9,2),  //p精确值和 s 大小的十进位整数,p代表10进制小数和整数的整体位数,s代有小数的10进制位数
                       1234567.89
        varchar (n),   //长度不固定,最大长度为 n 的字串,不能超过 4096
        text           //文本
          binary         //二进制数据
        date     日期类型 2000-2-2 4:4:4
          time     时间类型 4:4:4
     
  主键:即每一行数据的唯一标识    //primary key  
  默认值:当填写任何一个字段值时,该字段自动填写的值  //default 值
  是否允许为空:空和空串不同  NULL空   ""空串     //not null 
        char *p=NULL; 没内存地址
        char *p="";   有地址值,指向占1字节
  自动增长值:即整型自动的填写的不重复数据 //autoincrement(sqlite) 或 auto_increment(mysql)

*** 用sql语句建表
格式:
create table 表名(
字段名 字串类型(长度) 约束,
字段名 其它类型 约束
)

  例:
    create table student(
    	id integer not null primary key auto_increment,
    	name varchar(20) not null,
    	sex char(6) default '男',
    	age int default 18
    )      
    
  所有的sql语句,在工具中执行 ";" 代表语句结束,可执行。但在c语句,所有的语句不可以有";"      
      
  删除表格
  格式:drop table 表名
  
  例:drop table student

(二)、sql语句
增、删、改、查
select * from 表名

1、增
insert into 表名(字段列表) values (数值列表)

例:
insert into student(name,sex,age) values(‘张三’,‘男’,23);
insert into student(name,sex,age) values(‘李四’,‘男’,12);
insert into student(name,sex,age) values(‘王五’,‘女’,22);
insert into student(name,sex,age) values(‘赵六’,‘女’,23);
insert into student(name,sex,age) values(‘钱七’,‘男’,18);
insert into student(name,sex,age) values(‘孙九’,‘男’,23);
insert into student(name,sex,age) values(‘李十’,‘男’,23);

select * from student;

2、删
delete from 表名 where 条件
where部分可省略,代表删除表中的所有数据


  > >= < <=  = !=    age > 10
  like                name like '王%'  %代表所有字符
  in                  age in (23,12,3) 即符合在()内的值,被列出
  between .. and      age between 12 and 23   从12到23
  
  and   就是 &&
  or    就是 ||
  
  在...之间           age >12 and age <23      

例:
delete from student where age between 10 and 22;
delete from student where age >=10 and age <=22;
delete from student where name in (‘张三’,‘王五’) and age=18;
delete from student where (name=‘张三’ or name =‘王五’) and age=18;
delete from student where name like ‘张%’;

  条件的组合 使用and 和 or

3、改
update 表名 set 字段名=值[,字段名=值… ] where 条件

例:
update student set name=‘武六’,sex=‘女’
update student set name=‘武六’,sex=‘女’,age=34 where name like ‘李%’ ;

4、查

1) 简单查询
select 字段名 from 表名 where 条件 [order by 字段 [ desc]]
字段名
* 代表所有字段
name,age 直接指出了字段
表名.name,表名.age 通过表名指出了字段名
表名.name as newname 给字段名重写命名(不是改写数据库,是临时的)
order by 字段名1,字段名2 用于排序 从小到大 可省略
desc 让排序从大到小排序 可省略

例:
select * from student;
select * from student order by age;
select * from student order by age ,sex desc;
select * from student where name like ‘孙%’;
select name,age from student where name like ‘孙%’;
select name,age as a from student where name like ‘孙%’;
select name,student.age as a from student where name like ‘孙%’;

字段的运算,+ - * / % >> << & | ^

例:
select id+age as bits from student;
select age>>1 as bits from student;

分页查询
limit start,num //从第start行开始,读num行
limit num //从头读取num行

 SELECT * FROM table LIMIT 5,10; //从第6行开始,读取10行
 SELECT * FROM table LIMIT 95,-1; //从第96行开始,读取到最后
 SELECT * FROM table LIMIT 5; //读取前5行

2)组合查询
用途是多个表之间一起查数据

建表的原则:凡事在数据表中,有重复记录的数据,就需要重新建一个表,只将id记录在前一个表中
    例如 学生表,教师的信息要单独建表
    
    drop table student;
    create table student(
    	id integer not null primary key auto_increment,
    	name varchar(20) not null,
    	sex char(6) default '男',
    	age int default 18              
    ) ;
    create table teacher(
     	id integer not null primary key auto_increment,
    	name varchar(20) not null,
    	sex char(6) default '男',
    	age int default 18
    );  
    create table objects(
        id integer not null primary key auto_increment,
    	name varchar(20) not null,
            teacher_id int
    );
    create table scores(
           id integer not null primary key auto_increment,
           student_id int,
           objects_id int,
           score double
    );

数据
   insert into student(name,sex,age) values('张三','男',23);
   insert into student(name,sex,age) values('李四','男',12);
   insert into student(name,sex,age) values('王五','女',22);
   insert into student(name,sex,age) values('赵六','女',23);
   insert into student(name,sex,age) values('钱七','男',18);
   insert into student(name,sex,age) values('孙九','男',23);
   insert into student(name,sex,age) values('李十','男',23);


   insert into teacher(name,sex,age) values('张老师','男',23);
   insert into teacher(name,sex,age) values('李老师','男',12);
   insert into teacher(name,sex,age) values('王老师','女',22);
   insert into teacher(name,sex,age) values('赵老师','女',23);
   insert into teacher(name,sex,age) values('钱老师','男',18);


   insert into objects(name,teacher_id) values('数学',1);
   insert into objects(name,teacher_id) values('物理',1);
   insert into objects(name,teacher_id) values('化学',2);
   insert into objects(name,teacher_id) values('语文',3);
   insert into objects(name,teacher_id) values('英语',4);
   insert into objects(name,teacher_id) values('生物',5);

   insert into scores(student_id,objects_id,score) values(1,1,60);
   insert into scores(student_id,objects_id,score) values(1,2,80);
   insert into scores(student_id,objects_id,score) values(1,3,98);
   insert into scores(student_id,objects_id,score) values(1,4,88);
   insert into scores(student_id,objects_id,score) values(1,5,99);
   insert into scores(student_id,objects_id,score) values(1,6,38);
   insert into scores(student_id,objects_id,score) values(2,1,66);
   insert into scores(student_id,objects_id,score) values(2,2,55);
   insert into scores(student_id,objects_id,score) values(2,3,98);
   insert into scores(student_id,objects_id,score) values(2,4,77);
   insert into scores(student_id,objects_id,score) values(2,5,69);
   insert into scores(student_id,objects_id,score) values(2,6,98);
   insert into scores(student_id,objects_id,score) values(3,1,54);
   insert into scores(student_id,objects_id,score) values(3,2,67);
   insert into scores(student_id,objects_id,score) values(3,3,89);
   insert into scores(student_id,objects_id,score) values(3,4,97);
   insert into scores(student_id,objects_id,score) values(3,5,69);
   insert into scores(student_id,objects_id,score) values(3,6,88);
   insert into scores(student_id,objects_id,score) values(4,1,84);
   insert into scores(student_id,objects_id,score) values(4,2,87);
   insert into scores(student_id,objects_id,score) values(4,3,99);
   insert into scores(student_id,objects_id,score) values(4,4,87);
   insert into scores(student_id,objects_id,score) values(4,5,99);
   insert into scores(student_id,objects_id,score) values(4,6,98);
   insert into scores(student_id,objects_id,score) values(5,1,77);
   insert into scores(student_id,objects_id,score) values(5,2,87);
   insert into scores(student_id,objects_id,score) values(5,3,66);
   insert into scores(student_id,objects_id,score) values(5,4,87);
   insert into scores(student_id,objects_id,score) values(5,5,88);
   insert into scores(student_id,objects_id,score) values(5,6,98);
   insert into scores(student_id,objects_id,score) values(6,1,77);
   insert into scores(student_id,objects_id,score) values(6,2,87);
   insert into scores(student_id,objects_id,score) values(6,3,66);
   insert into scores(student_id,objects_id,score) values(6,4,87);
   insert into scores(student_id,objects_id,score) values(6,5,88);
   insert into scores(student_id,objects_id,score) values(6,6,98);    



scores-> 可以找到学生,找到学科 ->可以找到老师

1.查找学生的每科分数是什么,要求显示任课教师

方法一: select 字段 from 表1,表2 where 表1.字段=表2.字段
注意:该方法没有进行关联的数据,不会显示
1)确定谁是主表?
分数是主表,其它表为从表
2)确定表与表之间的关系
scores.student_id = student.id and scores.object_id=objects.id and objects.teacher_id =teacher.id

select student.id,student.name,student.sex,student.age,objects.name as objname,teacher.name as teaname,score from scores,student,objects,teacher where scores.student_id = student.id and scores.objects_id=objects.id and objects.teacher_id =teacher.id;

方法二 select 字段列表 from 表名1 left join 表名2 on 表名1.字段 = 表名2.主键 where 条件 
   主表中,所关联的某一id没有填写数据,则主表也依然显示
   left 左侧为主表, right 右侧为主表
   1)需要将哪一个表的数据全部显示,哪一个表就当主表
         学生表当主表
   2)确定表的关系

select * from objects left join scores on scores.objects_id=objects.id left join student on scores.student_id = student.id left join teacher on objects.teacher_id =teacher.id

3)复合查询
用于实现将检索的结果当做另一个数据表的检索的条件
[1] select 字段列表 from 表名 where 字段名 in (值1,值2)
select 字段列表 from 表名 where 字段名 in (select 字段名 from 表2 where 条件)

select * from student where id in (1,3,5,7) //在()中是指定的集合
复合查询是把select检索的结果当成集合
//如果()检索有结果,则显示符合该结果的id的所有学生
select * from student where id in (select id from student where name like ‘李%’)

[2] select 字段列表 from 表1 where exists (select * from 表2 where 条件)
当表2中没有检索出数据,则不检索前面的表1

//如果分数表有数据则显示学生的所有信息,否则不显示任何学生信息
select * from student where exists(select id from scores )
select * from student where exists(select * from information_schema.TABLES where TABLE_NAME ='student')

4)整合查询
用途:计算一个表中的数据。

用于实现获取记录总数(COUNT)、求和(SUM)、求平均值(AVG)、最大值(MAX)、最小值(MIN)等

select countsum,avg,max,min as 新的名称,分组用的字段名 from 表名 where 条件 Group by 分组用的字段名 order by 字段名 desc

Group by 按指定的字段分组进行计算数据,如不使用group by 则将数据表中所有数据进行计算

//记算总的数据条数
select count() as recodes,objects_id from scores
//每个科目,的分数各有多少条记录 按科目分组
select count(
) as recodes,objects_id from scores group by objects_id
//计算最大、最小、平均、和的值
select max(age) as maxage,min(age) as minage,avg(age) as avgage,sum(age) as sumage from student
//计算每个学生的平均分,和总分 按学生分组
select student.id,student.name,avg(score) as avgscore,sum(score) as sumscore from student,scores where student.id=scores.student_id group by student.id
select student.id,student.name,avg(score) as avgscore,sum(score) as sumscore from student left join scores on student.id=scores.student_id group by student.id
//计算每个老师的教学水平,根据学生的分数来计算 按教师分组
select teacher.id,teacher.name,objects .name as objects ,avg(score) as avgscore,sum(score) as sumscore from objects left join scores on objects.id=scores.objects_id left join teacher on objects.teacher_id=teacher.id group by teacher.id,objects.id
//计算每科的平均分和总分
select objects.id,objects.name,avg(score) as avgscore,sum(score) as sumscore from objects left join scores on objects.id=scores.objects_id where score>=60 group by objects.id

猜你喜欢

转载自blog.csdn.net/weixin_43138570/article/details/102566989