Mysql入门使用(慢慢探索)

Mysql 经过source命令能够直接运行*.sql的脚本,在命令行下不须要在进行复制粘贴什么的了:mysql

mysql>source D:\XX*.sqllinux

 

 

学了一段时间数据库和JSP了,sql

决定用linux+tomcat+JSP+MYSQL给本身作一个记帐本,只为把这些主流功能熟悉,最终目标是较好的实现;shell

其中JSP必定要使用SSH框架,以下降耦合度,为之后扩展作准备;数据库

同时熟悉MYSQL基本的操做,包括存储过程、触发器和视图的使用;tomcat

 

首先先设计数据库:框架

在shell下,进入mysql只需输入>set NAMES utf8;编码

便可改变编码格式,以输入中文命令行

create database accounts character set 'utf8';设计

 

 

    create table accounts.records(

        id INT(10) not null auto_increment,

       date INT(10) not null,

       month INT(10) not null,

       week INT(10) not null,

       income FLOAT(20) not null,

       expend FLOAT(20) not null,

       remark VARCHAR(255),

        primary key (id)

    );

 

 

    create table accounts.weeks(

        id INT(10) not null,

       income FLOAT(20) not null,

       expend FLOAT(20) not null,

        primary key (id)

    );

 

create table accounts.months(

        id INT(10) not null,

       income FLOAT(20) not null,

       expend FLOAT(20) not null,

        primary key (id)

    )

 

create table accounts.days(

        id INT(10) not null,

       income FLOAT(20) not null,

       expend FLOAT(20) not null,

        primary key (id)

    )

 

如下单个条数均成功:

create trigger t1 after insert on records for each row update weeks set weeks.income=weeks.income+NEW.income,weeks.expend=weeks.expend+NEW.expend where weeks.id=NEW.week;

 

create trigger t2 after insert on records for each row update months set months.income=months.income+NEW.income,months.expend=months.expend+NEW.expend where months.id=NEW.month;

 

但不能在一个动做下加两个触发器动做:

参照教材改成:

create trigger t1 after insert on records for each row

begin

update weeks set weeks.income=weeks.income+NEW.income,weeks.expend=weeks.expend+NEW.expend where weeks.id=NEW.week;

update months set months.income=months.income+NEW.income,months.expend=months.expend+NEW.expend where months.id=NEW.month;

end

应该是没错的  但是仍是失败;

 

使用select week as 周,sum(income) as 周收入,sum(expend) as 周支出  from records group by week;语句能够完美得出周汇总

同理select month as 月,sum(income) as 月收入,sum(expend) as 月支出  from records group by month;也能够得出月汇总

 

于是决定采用VIEW(视图的形式)

CREATE VIEW accounts.week

AS

select week as 周,sum(income) as 周收入,sum(expend) as 周支出  from records group by week;

 

CREATE VIEW accounts.month

AS

select month as 月,sum(income) as 月收入,sum(expend) as 月支出  from records group by month;

 

CREATE VIEW accounts.day

AS

select month as 日期,sum(income) as 日收入,sum(expend) as 日支出  from records group by date;

 

 

在相应的使用中  只需将视图做为一个单独的table便可

即:select * from accounts.week;

   select * from accounts.month;

   select * from accounts.day;

 

 

mysql中(视图有所不一样)

看数据库>show databases;

看表 >show tables;

看触发器 >show triggers;

看视图 >show table status where comment='view'; 

相关文章
相关标签/搜索