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'; 

相關文章
相關標籤/搜索