First of all, I started to see the feeling of this book ... It's weird. Puzzle ... There is a picture behind each puzzle.
Let's go.
The first puzzle let me know the database class when a sum of knowledge ~ ~ also good ~ ~
1. Let us know the use of date. The date usage for each DBMS is different, and I use SQL SERVER, and I find it wrong when I hit the book. Depressed. such as extract (year from date)
Check out the SQL Server is not at all.
function |
Description |
GETDATE () |
Returns the current date and time |
DATEPART () |
Returns a separate part of a date/time |
DATEADD () |
Add or subtract a specified time interval from a date |
DATEDIFF () |
Returns the time between two dates |
CONVERT () |
Display Date/time in a different format |
GETDATE () |
Returns the current date and time |
DATEPART () |
Returns a separate part of a date/time |
DATEADD () |
Add or subtract a specified time interval from a date |
DATEDIFF () |
Returns the time between two dates |
CONVERT () |
Display Date/time in a different format |
GETDATE () returns the current date time.
DATEPART (part,date) returns a separate part of the date/time; Integer
DATEADD (part,number,date) Adds or subtracts a specified time interval from a date;
DATEDIFF (part,startdate,enddate) returns the number of days between two dates;
Convert (data_type (length), date_to_be_conversed,style); Convert date format
Where part can be:
Years yyyy
month mm
Day DD
Japanese dy in the middle of the year
Weekly WW
Week DW
Hour HH
Min mi
Seconds s
Millisecond MS
Where date is the date type is a string, so enclose in single quotes.
2. The practice of integrity constraints.
Read, but the operation will feel strange.
Integrity constraints are divided into
(1) Primary key (...) Main code
(2) Unique (...) Only, cannot have duplicate
(3) constraint [name] Check (...) When you define a name, you can make a clear mistake.
(4) NOT NULL Non-null
Here is the solution to puzzle 1.
CREATE TABLE fiscalyears (fiscal_year INTEGER NOT null primary key, start_date date not NULL, constraint Start1 check (DAT Epart (yyyy,start_date) = fiscal_year-1), constraint start2 check (DATEPART (mm,start_date) =10), constraint start3 check ( DATEPART (dd,start_date) =01), end_date date not NULL, constraint end1 check (DATEPART (yyyy,end_date) =fiscal_year), Constraint end2 Check (DATEPART (mm,end_date) = on), constraint end3 check (DATEPART (dd,end_date) =, constraint End4 chec K (start_date<end_date), unique (fiscal_year,start_date,end_date)); INSERT into Fiscalyears values (1994, ' 1993-10-01 ', ' 1994-09-30 '); Insert Example
Summarize:
1.date usage
2. Integrity constraint Usage