<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

    業精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
      397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
    Constraints設置練習
    ?
    ??? 對于報表的創建來說,難點無非有這樣幾個:1、考慮是否需要分區、建簇等;2、設置表的存儲空間參數;3、設置完整性約束。其中第1、2點一般都針對大型的數據庫,而第3點是最常用到的。所以最近學習一下如何使用constraints來實現一些約束,直接在源頭杜絕數據錯誤。
    ?
    1 . FISCAL YEAR TABLES
    ?
    ??? Let's write some CREATE TABLE statements that are as complete aspossible. This little exercise is important because SQL is a declarativelanguage and you need to learn how to specify things in the databaseinstead of in the code.
    ??? The table looks like this:
    ??? CREATE TABLE FiscalYearTable1
    ??? (fiscal_year INTEGER,
    ???? start_date DATE,
    ???? end_date ?? DATE);

    ?
    ??? It stores date ranges for determining what fiscal year any given datebelongs to. For example, the federal government runs its fiscal year fromOctober 1 until the end of September. The scalar subquery you woulduse to do this table lookup is:
    ??? (SELECT F1.fiscal_year
    ?????? FROM FiscalYearTable1 AS F1
    ????? WHERE outside_date BETWEEN F1.start_date AND F1.end_date)

    ?
    ??? Your assignment is to add all the constraints you can think of to thetable to guarantee that it contains only correct information.
    ?
    ??? While vendors all have different date and time functions, let's assumethat all we have is the SQL-92 temporal arithmetic and the function:
    ??? EXTRACT ([YEAR | MONTH | DAY] FROM <date expression>), whichreturns an integer that represents a field within a date.
    ?
    ?
    Answer #1
    ?
    ??? 1. First things first; make all the columns NOT NULL since thereis no good reason to allow them to be NULL.
    ?
    ??? 2. Most SQL programmers immediately think in terms of addinga PRIMARY KEY, so you might add the constraint ?PRIMARYKEY (fiscal_year, start_date, end_date) because the fiscal year isreally another name for the pair (start_date, end_date). This isnot enough, because it would allow this sort of error:
    ??? (1995, '1994-10-01', '1995-09-30')
    ??? (1996, '1995-10-01', '1996-08-30') <== error!
    ??? (1997, '1996-10-01', '1997-09-30')
    ??? (1998, '1997-10-01', '1997-09-30')

    ??? You could continue along the same lines and fix some problemsby adding the constraints UNIQUE (fiscal_year),UNIQUE (start_date), and UNIQUE (end_date), since we donot want duplicate dates in any of those columns.
    ?
    ??? 3. The constraint that almost everyone forgets to add because it isso obvious is:
    ??? CHECK (start_date < end_date) or CHECK (start_date <=end_date), as is appropriate.
    ?
    ??? 4. A better way would be to use the constraint PRIMARY KEY(fiscal_year) as before, but then since the start and end datesare the same within each year, you could use constraints onthose column declarations:
    ??? CREATE TABLE FiscalYearTable1
    ??? (fiscal_year INTEGER NOT NULL PRIMARY KEY,
    ???? start_date DATE NOT NULL,
    ???? CONSTRAINT valid_start_date
    ?????? CHECK ((EXTRACT (YEAR FROM start_date) = fiscal_year - 1)
    ???????? AND (EXTRACT (MONTH FROM start_date) = 10)
    ???????? AND (EXTRACT (DAY FROM start_date) = 01)),
    ???? end_date ?? DATE NOT NULL,
    ???? CONSTRAINT ?valid_end_date
    ?????? CHECK ((EXTRACT (YEAR FROM end_date) = fiscal_year)
    ???????? AND (EXTRACT (MONTH FROM end_date) = 09)
    ???????? AND (EXTRACT (DAY FROM end_date) = 30)));

    ??? You could argue for making each predicate a separate constraintto give more detailed error messages. The predicates onthe year components of the start_date and end_date columnsalso guarantee uniqueness because they are derived from theunique fiscal year.
    ?
    ??? 5. Unfortunately, this method does not work for all companies.Many companies have an elaborate set of rules that involve takinginto account the weeks, weekends, and weekdays involved.They do this to arrive at exactly 360 days or 52 weeks in theiraccounting year. In fact, there is a fairly standard accountingpractice of using a “4 weeks, 4 weeks, 5 weeks” quarter withsome fudging at the end of the year; you can have a leftoverweek between 3 and 11 days. The answer is a FiscalMonthtable along the same lines as this FiscalYears example.
    ?
    ??? A constraint that will work surprisingly well for such cases is:
    ??? CHECK ((end_date - start_date) = INTERVAL 359 DAYS)
    ?
    ??? where you adjust the number of days to fit your rules (i.e., 52 weeks * 7?days = 364 days). If the rules allow some variation in the size of the fiscalyear, then replace the equality test with a BETWEEN predicate.
    ??? Now, true confession time. When I have to load such a table in adatabase, I get out my spreadsheet and build a table using the built-intemporal functions. Spreadsheets have much better temporal functionsthan databases, and there is a good chance that the accountingdepartment already has the fiscal calendar in a spreadsheet.
    ?
    ?
    ?
    posted on 2009-03-20 21:21 decode360 閱讀(216) 評論(0)  編輯  收藏 所屬分類: 05.SQL
    主站蜘蛛池模板: 亚洲JIZZJIZZ中国少妇中文| 亚洲AV无码精品色午夜在线观看| 免费国产va在线观看| 亚洲gv白嫩小受在线观看| 美女视频黄a视频全免费| 成人a毛片视频免费看| 久久国产精品亚洲一区二区| 成人毛片18女人毛片免费96 | 亚洲一区二区三区播放在线| 国产性生交xxxxx免费| 免费国产成人午夜在线观看| 亚洲高清一区二区三区电影| 亚洲国产a∨无码中文777| 天天摸夜夜摸成人免费视频 | 91亚洲一区二区在线观看不卡| 日韩成人免费aa在线看| 久章草在线精品视频免费观看| 亚洲 欧洲 自拍 另类 校园| 伊人婷婷综合缴情亚洲五月| 久久不见久久见免费影院| 亚洲精品偷拍视频免费观看| 亚洲综合欧美色五月俺也去| 久热综合在线亚洲精品| 日韩精品成人亚洲专区| 青青草免费在线视频| 免费在线观看一级片| 一级一级毛片免费播放| 亚洲一区二区三区国产精华液| 久久精品国产亚洲AV麻豆不卡| 免费一看一级毛片| 成年性午夜免费视频网站不卡| 国产99视频精品免费专区| 日韩大片免费观看视频播放| 亚洲日韩一区精品射精| 亚洲日韩国产精品无码av| 精品久久久久久亚洲| 亚洲无码高清在线观看| 国产一区在线观看免费| 99视频在线精品免费观看6| 18禁无遮挡无码国产免费网站| 中文字幕视频在线免费观看|