Notes about First Database assignment

keep updating...

the script and document of this assignment can be downloaded from 点击打开链接

And here are notes about problems in this assignment:


Q2.

what format of data will it return if we compute two timestamp data?


so we know that, timestamp is shown as YYYY(year)MM(month)DD(day)HH(hour)MM(month)SS(second).

so if we compute (endTime - startTime) directly, the result will be (endTime(minute) - startTime(minute)) * 100 + (endTime(second) - endTime(second)).

so we should call time_to_sec to change this type of data to the type we want.

Q6.

Here, i'd like to discuss the usage of function.

this is the format of a simple function:

drop function if exists inZone1; 
DELIMITER $$  
create function inZone1(id1 BIGINT UNSIGNED)
returns boolean
begin   
  if id1 in (select id from station where zone = 1)
  then return true;  
  else return false;
end if;
end $$  

DELIMITER: In SQL we close each statement with a delimiter, which is always a semicolon (;). In a trigger we need to write multiple statements, each ending in a semicolon. To tell MySQL that those semicolons are not the end of our trigger statement, we should temporarily change the delimiter from ; to $$, so MySQL will know that the trigger statement only ends when it econunters a $$.

And the format of function is like this:

delimiter @@[we can use any signals]
create function [name of our function]([name of the parameter1] [type of the parameter1], ...)
returns [type of the return]
begin
statement1
...
end
end @@

In SQL you close each statement with a delimiter, which is by default a semicolon (;). In a trigger you need to write multiple statements, each ending in a semicolon. To tell MySQL that those semicolons are not the end of your trigger statement, you temporarily change the delimiter from ; to @@, so MySQL will know that the trigger statement only ends when it econunters a @@

Q10.

However, i met two problems when i solved this problem.

P1:

I want to select customer who have travel on line 0,1,2,3 , if one have journey on each line, he travels all the lines. In fact, line 0 should not be considered. But i thought that if one has journey that changes from one line to the other, he must have passed line 0. So, i wrote a function to find whether a journey crosses 2 lines:

DELIMITER $$  
create function isLastStation(id1 BIGINT UNSIGNED)
returns boolean
begin   
  if(id1 in (select id from station a 
            where a.name != "City" and
            (a.id >= all(select id from station b
                               where a.line = b.line)))) 
  then return true;  
  else return false;
end if;
end $$  

and the sql sequence is blow:

select username from customer c
where  (id in (select customer as customerOverLine1 from journey   //condition 1
            where  endStation in (select id from station
                                  where line = 1)
			or startStation in (select id from station 
                                where line = 1)
			group by customer))
and (id in (select customer as customerOverLine2 from journey      //condition 2
            where  endStation in (select id from station
                                  where line = 2)
			or startStation in (select id from station 
                                where line = 2)
			group by customer))
and (id in (select customer as customerOverLine3 from journey     //condition 3
            where  endStation in (select id from station
                                  where line = 3)
			or startStation in (select id from station 
                                where line = 3)
			group by customer))
however, when i try to add 
(id in (select customer as customerOverLine0 from journey
where !inSameLine(startStation,endStation)
group by customer))

the result is the carol only:


it's amazing that if i select id follow condition 2 and 3 and 4, there are four result: alice bob carol and dan; and if i select id follow condition 1 only, the result is alice bob carol dan and eve; but when i merge the conditions, there is only 1 result~

P2;

another problem arises when i tried to find another method to deal with the situation that i dont know the number of line and line id. so i have to write another function/procedure , deal with each line in table line. but the error 'missing end' cannot be solved ~ like pic below:


i want to set value to a variable parameter from a select sequence, but it always shows that i miss end after i set the value. if i add end, the body of function ends and another error arises...pity

猜你喜欢

转载自blog.csdn.net/weixin_41879719/article/details/80073195