Skip to content

Business Scenario AIrline#33

Open
jacobglanz wants to merge 4 commits into
CPU-Code-School:mainfrom
jacobglanz:main
Open

Business Scenario AIrline#33
jacobglanz wants to merge 4 commits into
CPU-Code-School:mainfrom
jacobglanz:main

Conversation

@jacobglanz
Copy link
Copy Markdown

I chose the airline example. Please review my work and let me know if there are any fixes needed.

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work! See comments, fix and resubmit.

Comment thread 1 table.sql Outdated
create table dbo.Booking(
BookingId int not null identity primary key,
FlightNum char(6) not null
constraint ck_Booking_flight_number_must_start_with_Fly_then_fllowed_by_3_numbers check(FlightNum like 'Fly[0-9][0-9][0-9]'),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flight number should not be FLY000

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed constraint
I have now 1 constraint that enforces that FilghtNum Should:

  1. begin with fly
  2. Then be followed by 3 digits
  3. that is not 000 (I wouldn't use between since 000 is the only wrong input here)

Since all those constraints are formatting rules for FlightNum is it better to keep it in one big constraint so I would get all the instructions/errors the first time rather then keep getting errors until all's god?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only have one of each type of constraint on each column. Example if you have multiple check constraints on single column, you should always combine them. That's why sql gives error when you have multiple check constraints. Splitting them with commas makes them multi column constraints.

Comment thread 1 table.sql Outdated
PassportNum varchar(9) null
constraint ck_Booking_passport_num_cannot_start_with_0 check(PassportNum not like '0%'),
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When having multiple check constraints. Instead of making them multi column constraints, combine them together with and/or.

And you don't need first constraint, last is constraining it not to allow starting with 0.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please help me understand how the last constraint is enforcing the first digit should not be a 0?

And here I got the answer to my question for the previous comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, mistake.

Comment thread 1 table.sql Outdated
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
PassportIssueDate date null,
PassportExpiryDate as case
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case should be a constraint. It doesn't make sense to have expiry date as computed column.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have constraints making errors when you can have it work automatic and save future work potential constraints/errors? Since expiry date is entirely based on issue date and passenger's DOB.
I guess I'm asking in general, what's the best practice?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because expiry date is data you add to the ticket. And it has to do with the passport not with the ticket. It's not necessarily based on the other dates.

On a side note. If you have a passport that's still valid, and you make a new one, you will have extra dates on your passport but if it's > 10 years old at time of travel, you won't be able to travel even if it's still valid.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 1 table.sql
constraint ck_Booking_passport_issue_date_cannot_be_before_passenger_dob check(PassengerDOB <= PassportIssueDate),
constraint ck_Booking_passport_booked_date_cannot_be_before_passport_issue_date check(PassportIssueDate <= BookedDate),
constraint ck_Booking_checked_in_time_cannot_be_before_booked_date check((BookedDate <= CheckedInTime) or CheckedInTime is null),
constraint ck_Booking_passenger_age_must_be_between_16_and_90
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use datediff() between...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to consider it a full year when the day and month have also passed
According to SQL select datediff(year, '2022-12-31', '2023-01-01') is 1 year apert when in reality it's 1 day apert

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it.

Comment thread 1 table.sql Outdated

--All or non constraints
constraint ck_Booking_passport_num_passport_issue_date_passport_nationality_and_checked_in_time_must_all_either_be_completed_or_null
check((PassportNum is null and PassportIssueDate is null and PassportNationality is null and CheckedInTime is null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have passport details even if not checked in yet.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Aren't they working hand in hand? According to the spec, 'When you book the flight you would only need to provide your name, DOB, and address, but in order to be checked in and travel, we need the passport details.' So, I understood that when you check in, you give your passport details, and it's one process."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. You can add the passport details before. At the time you check in, you must have the details.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 1 table.sql Outdated
or (PassportNum is not null and PassportIssueDate is not null and PassportNationality is not null and CheckedInTime is not null)),

--Unique Constraints
constraint u_Booking_one_passenger_cannot_book_2_tickets_on_the_same_flight unique (FlightNum, PassengerName, PassengerDOB, PassengerAddress),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flight num is same on multiple days.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it by adding DepartureTime to the unique constraint, so now Passenger Info FlightNum & DepartureTime must be unique

Comment thread 2 reports.sql
group by b.FlightNum, b.DepartureAirport, b.DepartureTime, b.ArrivalAirport

--2) Who isn't checked in for flights departing in the next week, in order to send them reminders to check in.
select *
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For flights departing next week.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added datediff(day, getdate(), b.DepartureTime) between 0 and 7 to the where clause

Comment thread 2 reports.sql Outdated
group by convert(date, b.DepartureTime, 23)

--4) How many flights are departing per destination, and num of passengers we have on those flights, to know what route is attracts most people.
select b.DepartureAirport, Flights = b.FlightNum, Passengers = count(*)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per destination.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 2 reports.sql

--6) How many flights does each person have (to know for frequent flyer status), as: last name, first name, number of flights.
select LastName = substring(b.PassengerName,charindex(' ', b.PassengerName)+1, len(b.PassengerName)),
FirstName = substring(b.PassengerName, 1, charindex(' ', b.PassengerName)-1),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to do substring() maybe consider splitting first and last name in data

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate what mean by "splitting first and last name in data"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having full name as one column, use 2 columns one for first name and one for last name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how my code was initially

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why did you change it? You should always have first and last name split.

Copy link
Copy Markdown
Author

@jacobglanz jacobglanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review my comments, and i have committed my changes up until here.

Comment thread 1 table.sql Outdated
create table dbo.Booking(
BookingId int not null identity primary key,
FlightNum char(6) not null
constraint ck_Booking_flight_number_must_start_with_Fly_then_fllowed_by_3_numbers check(FlightNum like 'Fly[0-9][0-9][0-9]'),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed constraint
I have now 1 constraint that enforces that FilghtNum Should:

  1. begin with fly
  2. Then be followed by 3 digits
  3. that is not 000 (I wouldn't use between since 000 is the only wrong input here)

Since all those constraints are formatting rules for FlightNum is it better to keep it in one big constraint so I would get all the instructions/errors the first time rather then keep getting errors until all's god?

Comment thread 1 table.sql Outdated
PassportNum varchar(9) null
constraint ck_Booking_passport_num_cannot_start_with_0 check(PassportNum not like '0%'),
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please help me understand how the last constraint is enforcing the first digit should not be a 0?

And here I got the answer to my question for the previous comment

Comment thread 1 table.sql Outdated
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
PassportIssueDate date null,
PassportExpiryDate as case
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have constraints making errors when you can have it work automatic and save future work potential constraints/errors? Since expiry date is entirely based on issue date and passenger's DOB.
I guess I'm asking in general, what's the best practice?

Comment thread 1 table.sql
constraint ck_Booking_passport_issue_date_cannot_be_before_passenger_dob check(PassengerDOB <= PassportIssueDate),
constraint ck_Booking_passport_booked_date_cannot_be_before_passport_issue_date check(PassportIssueDate <= BookedDate),
constraint ck_Booking_checked_in_time_cannot_be_before_booked_date check((BookedDate <= CheckedInTime) or CheckedInTime is null),
constraint ck_Booking_passenger_age_must_be_between_16_and_90
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to consider it a full year when the day and month have also passed
According to SQL select datediff(year, '2022-12-31', '2023-01-01') is 1 year apert when in reality it's 1 day apert

Comment thread 1 table.sql Outdated

--All or non constraints
constraint ck_Booking_passport_num_passport_issue_date_passport_nationality_and_checked_in_time_must_all_either_be_completed_or_null
check((PassportNum is null and PassportIssueDate is null and PassportNationality is null and CheckedInTime is null)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Aren't they working hand in hand? According to the spec, 'When you book the flight you would only need to provide your name, DOB, and address, but in order to be checked in and travel, we need the passport details.' So, I understood that when you check in, you give your passport details, and it's one process."

Comment thread 1 table.sql Outdated
or (PassportNum is not null and PassportIssueDate is not null and PassportNationality is not null and CheckedInTime is not null)),

--Unique Constraints
constraint u_Booking_one_passenger_cannot_book_2_tickets_on_the_same_flight unique (FlightNum, PassengerName, PassengerDOB, PassengerAddress),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it by adding DepartureTime to the unique constraint, so now Passenger Info FlightNum & DepartureTime must be unique

Comment thread 2 reports.sql
group by b.FlightNum, b.DepartureAirport, b.DepartureTime, b.ArrivalAirport

--2) Who isn't checked in for flights departing in the next week, in order to send them reminders to check in.
select *
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added datediff(day, getdate(), b.DepartureTime) between 0 and 7 to the where clause

Comment thread 2 reports.sql Outdated
group by convert(date, b.DepartureTime, 23)

--4) How many flights are departing per destination, and num of passengers we have on those flights, to know what route is attracts most people.
select b.DepartureAirport, Flights = b.FlightNum, Passengers = count(*)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 2 reports.sql

--6) How many flights does each person have (to know for frequent flyer status), as: last name, first name, number of flights.
select LastName = substring(b.PassengerName,charindex(' ', b.PassengerName)+1, len(b.PassengerName)),
FirstName = substring(b.PassengerName, 1, charindex(' ', b.PassengerName)-1),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate what mean by "splitting first and last name in data"

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! See comments, fix and resubmit.

Comment thread 1 table.sql Outdated
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
PassportIssueDate date null,
PassportExpiryDate as case
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because expiry date is data you add to the ticket. And it has to do with the passport not with the ticket. It's not necessarily based on the other dates.

On a side note. If you have a passport that's still valid, and you make a new one, you will have extra dates on your passport but if it's > 10 years old at time of travel, you won't be able to travel even if it's still valid.

Comment thread 1 table.sql Outdated

--All or non constraints
constraint ck_Booking_passport_num_passport_issue_date_passport_nationality_and_checked_in_time_must_all_either_be_completed_or_null
check((PassportNum is null and PassportIssueDate is null and PassportNationality is null and CheckedInTime is null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. You can add the passport details before. At the time you check in, you must have the details.

Comment thread 1 table.sql
constraint ck_Booking_passport_issue_date_cannot_be_before_passenger_dob check(PassengerDOB <= PassportIssueDate),
constraint ck_Booking_passport_booked_date_cannot_be_before_passport_issue_date check(PassportIssueDate <= BookedDate),
constraint ck_Booking_checked_in_time_cannot_be_before_booked_date check((BookedDate <= CheckedInTime) or CheckedInTime is null),
constraint ck_Booking_passenger_age_must_be_between_16_and_90
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it.

Comment thread 1 table.sql Outdated
create table dbo.Booking(
BookingId int not null identity primary key,
FlightNum char(6) not null
constraint ck_Booking_flight_number_must_start_with_Fly_then_fllowed_by_3_numbers check(FlightNum like 'Fly[0-9][0-9][0-9]'),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only have one of each type of constraint on each column. Example if you have multiple check constraints on single column, you should always combine them. That's why sql gives error when you have multiple check constraints. Splitting them with commas makes them multi column constraints.

Comment thread 1 table.sql Outdated
PassportNum varchar(9) null
constraint ck_Booking_passport_num_cannot_start_with_0 check(PassportNum not like '0%'),
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, mistake.

Comment thread 2 reports.sql

--6) How many flights does each person have (to know for frequent flyer status), as: last name, first name, number of flights.
select LastName = substring(b.PassengerName,charindex(' ', b.PassengerName)+1, len(b.PassengerName)),
FirstName = substring(b.PassengerName, 1, charindex(' ', b.PassengerName)-1),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having full name as one column, use 2 columns one for first name and one for last name.

Comment thread 2 reports.sql

--5) How many people booked but didn't actually travel in the end.
select count(*)
from Booking b
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add column name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Copy Markdown
Author

@jacobglanz jacobglanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed Mistakes

Comment thread 1 table.sql Outdated
constraint ck_Booking_passport_num_length_must_be_9 check(len(PassportNum) = 9),
constraint ck_Booking_passport_num_can_only_be_numbers check(PassportNum not like '%[^0-9]%'),
PassportIssueDate date null,
PassportExpiryDate as case
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 1 table.sql Outdated

--All or non constraints
constraint ck_Booking_passport_num_passport_issue_date_passport_nationality_and_checked_in_time_must_all_either_be_completed_or_null
check((PassportNum is null and PassportIssueDate is null and PassportNationality is null and CheckedInTime is null)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 2 reports.sql

--5) How many people booked but didn't actually travel in the end.
select count(*)
from Booking b
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread 2 reports.sql

--6) How many flights does each person have (to know for frequent flyer status), as: last name, first name, number of flights.
select LastName = substring(b.PassengerName,charindex(' ', b.PassengerName)+1, len(b.PassengerName)),
FirstName = substring(b.PassengerName, 1, charindex(' ', b.PassengerName)-1),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how my code was initially

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! All looks good. See one comment regarding first and last names.

Comment thread 2 reports.sql

--6) How many flights does each person have (to know for frequent flyer status), as: last name, first name, number of flights.
select LastName = substring(b.PassengerName,charindex(' ', b.PassengerName)+1, len(b.PassengerName)),
FirstName = substring(b.PassengerName, 1, charindex(' ', b.PassengerName)-1),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why did you change it? You should always have first and last name split.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant