YouTube2Text

Intermediate SQL/1 — Transcript

by Data Base Management System - IITKGP · 4,659 words · 832 segments · language en · Watch on YouTube

Full transcript

  1. 0:06so
  2. 0:09[Music]
  3. 0:16welcome to module 9
  4. 0:18of
  5. 0:19database management systems
  6. 0:22we have discussed about the introductory
  7. 0:24level of
  8. 0:26sql the structured query language
  9. 0:29in this module and the next we will take
  10. 0:32up
  11. 0:33some more intermediate level features of
  12. 0:36sql
  13. 0:38so these modules are called intermediate
  14. 0:40sql
  15. 0:42so in the last module this is what we
  16. 0:45have done which was
  17. 0:47part of the closing part of the
  18. 0:49introductory
  19. 0:51sql the nested sub queries and
  20. 0:53modifications to the database
  21. 0:56ah today we will
  22. 0:58in this module learn about
  23. 1:00sql expressions for join and views
  24. 1:04and we will take a quick look
  25. 1:07into understanding the transaction
  26. 1:11this is the module outline as it will
  27. 1:13span
  28. 1:15so we start with the join expressions
  29. 1:18in sql
  30. 1:21ah join as
  31. 1:23we have already introduced
  32. 1:26takes two relations and returns a result
  33. 1:30as another relation so it is
  34. 1:33two different
  35. 1:35instances of two schemas
  36. 1:37and we try to connect them according to
  37. 1:41certain properties
  38. 1:43so a join operation is primarily a
  39. 1:46cartesian product
  40. 1:48which
  41. 1:49relates
  42. 1:50tuples
  43. 1:52in two relations under certain
  44. 1:54conditions of match
  45. 1:57it also specifies that after the joining
  46. 2:00has been done
  47. 2:02what are the tuples which will be
  48. 2:04present in the output
  49. 2:07join so join operation typically uses
  50. 2:10sub query
  51. 2:12is used in as sub query in the from
  52. 2:16clause we will see those
  53. 2:18uses later
  54. 2:20so
  55. 2:21if we look into the different types of
  56. 2:23join that
  57. 2:24sql
  58. 2:26support these are the different
  59. 2:27classifications so we have cross join
  60. 2:30we have inner join which specifically
  61. 2:33could be equi join and even more
  62. 2:36specifically natural join
  63. 2:38and we will see
  64. 2:40there are variety of outer join that are
  65. 2:42possible there could be a self join also
  66. 2:45where
  67. 2:46one relation is joined with itself
  68. 2:51cross join
  69. 2:52is just a formal join based name for
  70. 2:56cartesian product of two rows
  71. 2:58so
  72. 2:59you could explicitly do a cross join
  73. 3:02which you can see here or you can
  74. 3:05implicitly also do a cross join but by
  75. 3:09ah
  76. 3:10specifying two or more relations in the
  77. 3:12from clause and taking all the
  78. 3:14attributes from there we have seen
  79. 3:17these kind of cartesian products earlier
  80. 3:19so cross join here is more as
  81. 3:21placeholder in the context of the joint
  82. 3:23semantics that pure cartesian product is
  83. 3:27a cross join
  84. 3:29but what would be more interesting is ah
  85. 3:32when we take different kinds of inner
  86. 3:34and outer joins
  87. 3:36so
  88. 3:37let us start by
  89. 3:38with a simple example to understand the
  90. 3:41issues
  91. 3:42so there is a relation course
  92. 3:44which has four attributes and then this
  93. 3:46particular instance it has three tuples
  94. 3:49three rows
  95. 3:51and there is another relation prereq
  96. 3:54which specifies the prerequisite for
  97. 3:57every course
  98. 3:59so it has two attributes the course id
  99. 4:02and the corresponding prerequisite
  100. 4:04course id it also has three
  101. 4:07ah
  102. 4:08rows three tuples here
  103. 4:10and if you look at the instances
  104. 4:13carefully you will find
  105. 4:15that
  106. 4:16the three courses that are specified in
  107. 4:18the course relation
  108. 4:20all do not
  109. 4:21are not specified in the prereq relation
  110. 4:25ah bio 301 and cs 190 is present
  111. 4:29in prereq but cs315 is not present
  112. 4:33in
  113. 4:34at the same time the prereq has
  114. 4:37one
  115. 4:38particular tuple
  116. 4:41specifying the prerequisite of c s three
  117. 4:43four seven which in turn is not present
  118. 4:46in the course relation
  119. 4:48so the with this observation let us
  120. 4:50start
  121. 4:51trying to see what different joints mean
  122. 4:55so in a join
  123. 4:57is
  124. 4:58ah
  125. 4:59computed
  126. 5:00then
  127. 5:02in terms of the two relations that we
  128. 5:05have
  129. 5:06there are
  130. 5:08there is an attribute course id which is
  131. 5:10common
  132. 5:12so
  133. 5:13once we have taken the cross product
  134. 5:16we will
  135. 5:17from the cross product only retain
  136. 5:20those
  137. 5:21rows where
  138. 5:23the course id in relation course
  139. 5:27and the course id in relation prac
  140. 5:30prerequisite
  141. 5:31are same
  142. 5:33so
  143. 5:34when we do this
  144. 5:36this particular record
  145. 5:39when it gets
  146. 5:40mapped with
  147. 5:41this corresponding record it will
  148. 5:44generate
  149. 5:45the corresponding
  150. 5:47output record
  151. 5:49similarly cs190
  152. 5:52when it is mapped to the cs 190 in the
  153. 5:54prereq it will generate the second
  154. 5:56record
  155. 5:57we have already understood this
  156. 6:00the third record in the courses cs315
  157. 6:04has no match here in prereq so that will
  158. 6:07not feature in the output similarly in
  159. 6:09the prereq cs 347 that exist has no
  160. 6:13match in courses so that also will not
  161. 6:16appear in the output
  162. 6:18and also in the
  163. 6:20output you find that the course id has
  164. 6:23actually featured twice
  165. 6:25this is ah the first
  166. 6:28column course id comes from course so it
  167. 6:31should more formally be called course
  168. 6:33dot course id
  169. 6:34whereas the second one comes from prereq
  170. 6:37so it should that should formally be
  171. 6:38called prereq dot course id
  172. 6:41now if
  173. 6:42in addition to saying that this is an
  174. 6:45inner join if i also specify the word
  175. 6:48natural i can say natural here
  176. 6:51if i say natural
  177. 6:53then
  178. 6:54this second duplicate
  179. 6:57attribute course id will be dropped from
  180. 7:00the output that becomes a natural join
  181. 7:04inner join as the name suggest finds out
  182. 7:08the
  183. 7:09inner part of the two relations so if we
  184. 7:12look at the two relations as a and b
  185. 7:14only these rows records which
  186. 7:17are both
  187. 7:18have have instance in a as well as b in
  188. 7:21terms of equality of this course id
  189. 7:24attribute will come in the output
  190. 7:27so this is the
  191. 7:29ah basic
  192. 7:30type of join inner join which is most
  193. 7:32commonly used
  194. 7:35now we can extend this into
  195. 7:38a different kind of join known as outer
  196. 7:41join
  197. 7:42in the inner join as you have seen that
  198. 7:45courses that exist in the
  199. 7:47course relation but is not there in the
  200. 7:49prereq or the ones that exist in the
  201. 7:52prerequisite is not there in the course
  202. 7:54ah are not featuring in the final inner
  203. 7:57join
  204. 7:58output so there is some loss of
  205. 8:00information in terms of this
  206. 8:03so
  207. 8:04while we are doing this
  208. 8:06we can
  209. 8:07compute and add tuples from one relation
  210. 8:11that may not match
  211. 8:14with the
  212. 8:15with any tuple in the other relation
  213. 8:19and
  214. 8:19if we want to do that then naturally
  215. 8:23for
  216. 8:24the other attributes of that tuple in
  217. 8:26the target relation we will not know the
  218. 8:28values so we will use null values this
  219. 8:31is the basic idea of outer join
  220. 8:33so let us see what it specifically means
  221. 8:37we first talk about left outer join left
  222. 8:39in the sense
  223. 8:41that
  224. 8:42we have
  225. 8:43and this is how it is written
  226. 8:44ah left outer join
  227. 8:47is a
  228. 8:48sequence of commands that you give you
  229. 8:50are also saying it is natural which
  230. 8:52means that the common attribute will not
  231. 8:54feature twice in the output
  232. 8:56and this is the left relation and this
  233. 8:58is the right relation
  234. 9:00so left outer join
  235. 9:02specifies that in the output
  236. 9:05all records of
  237. 9:07the left relation in this case the
  238. 9:09course relation must feature
  239. 9:12so
  240. 9:12naturally when we do the join we will
  241. 9:14get these two records as we have got in
  242. 9:17terms of the inner join
  243. 9:19in terms of course three one five the cs
  244. 9:23cs315 the third course
  245. 9:26there is no instance
  246. 9:28in the prereq
  247. 9:30we will still have that in the output
  248. 9:32but since the prereq value for that the
  249. 9:35prerequisite value is not known the
  250. 9:37prerequisite id will be set to null here
  251. 9:40so left outer join ensure that all
  252. 9:43relations of the left relation all the
  253. 9:46tuples of the left relation will
  254. 9:48necessarily feature in the output and
  255. 9:50that is the reason if you see in the
  256. 9:52venn diagram the whole of this the whole
  257. 9:55of this
  258. 9:56set a is shown
  259. 9:57whereas this part certainly will not
  260. 10:00feature
  261. 10:05now similarly we can have a right outer
  262. 10:07join where the concept is the same
  263. 10:10except that now we ensure that all
  264. 10:13records of the right relation in this
  265. 10:16case the prereq relation will feature
  266. 10:19and therefore
  267. 10:20ah cs2347
  268. 10:23for which there is no entry in the
  269. 10:24course
  270. 10:26relation will also come as a record and
  271. 10:28since we do not know the title
  272. 10:30department name and credits
  273. 10:33for these fields we will put them as
  274. 10:36null
  275. 10:37and this again is a natural one so
  276. 10:39course id is featuring only once
  277. 10:43so you will understand that since we
  278. 10:44have a left version and we have a light
  279. 10:46version we can actually have a full
  280. 10:49version as well so if we look into the
  281. 10:51join relations
  282. 10:53in general it takes two relations and
  283. 10:55returns a result
  284. 10:57and those additional operations are used
  285. 11:00in the sub query in from
  286. 11:03and there is a set of join conditions so
  287. 11:05these are the join conditions that we
  288. 11:07are specifying whether
  289. 11:09it is natural and we will soon see that
  290. 11:12we can actually
  291. 11:14not depend only on the attributes that
  292. 11:17are common we can actually specify that
  293. 11:21which attributes should be used in
  294. 11:24ah computing the join so those are the
  295. 11:26on condition and the using ah clause we
  296. 11:29will just illustrate them soon and
  297. 11:32finally there are four types of join
  298. 11:35that
  299. 11:39can happen that is
  300. 11:42the
  301. 11:42inner join we have seen the left outer
  302. 11:44join right outer join and we will soon
  303. 11:46see the full outer join
  304. 11:52so full outer join as you must have
  305. 11:55guessed will ensure that ah
  306. 11:58you get ah
  307. 11:59certainly the tuples from the inner join
  308. 12:02which is here
  309. 12:04you will get the tuple from the left
  310. 12:07outer join
  311. 12:08that is here that is a tuple which exist
  312. 12:11in course and there is no corresponding
  313. 12:14matching tuple in the prereq
  314. 12:16and you will also get the tuple from the
  315. 12:18right outer join that is for tuple which
  316. 12:21exist in the
  317. 12:23prereq
  318. 12:24relation but there is no corresponding
  319. 12:26tuple in the
  320. 12:27ah course relation and corresponding ah
  321. 12:29missing values are all set to null so
  322. 12:32these three kinds of ah
  323. 12:36outer join are
  324. 12:37possible
  325. 12:39so you can also ah specify join by
  326. 12:42saying that ah explicitly saying what
  327. 12:46attribute we want to join on and if you
  328. 12:48specify that then you are saying its a
  329. 12:51course inner join prereq this part was
  330. 12:54same then you are putting an on clause
  331. 12:56saying in the on clause you will have to
  332. 12:57provide a predicate that is which field
  333. 13:01should equate or match with what field
  334. 13:04so you are saying course dot course id
  335. 13:06is equal to prereq dot course id so this
  336. 13:09result incidentally happens to be same
  337. 13:12as just doing the inner join but we are
  338. 13:14illustrating that on clause can
  339. 13:16explicitly use for example between the
  340. 13:18two relations we have more than one
  341. 13:20common attribute but we may want to
  342. 13:23actually do the inner join
  343. 13:25based on only one of them or equality on
  344. 13:28two of them and so on
  345. 13:32so this is this kind of a join where
  346. 13:35inner join where you
  347. 13:37set two fields to be equal or two or
  348. 13:40more fields to be equal is also known as
  349. 13:42equi join
  350. 13:43and since we have not specified natural
  351. 13:46you can again observe that the course id
  352. 13:48attribute has occurred twice if it was
  353. 13:51said natural then the second course id
  354. 13:53attribute would not have come in the
  355. 13:55result
  356. 13:57this is ah showing the left outer join
  357. 14:00in terms of a
  358. 14:02on clause and we have seen similar
  359. 14:05results and now this can be
  360. 14:08seen in terms of the
  361. 14:10on clause as well and you can see in the
  362. 14:13second course id field ah the
  363. 14:17this entry is null because actually you
  364. 14:21do not have that in the prerequisite set
  365. 14:25and obviously this set will be none this
  366. 14:27field will be null
  367. 14:33so this is another example showing you
  368. 14:36ah the natural right outage joint
  369. 14:39ah this is you showing you full
  370. 14:43outer joint and we are
  371. 14:45showing the use of the using clause you
  372. 14:48can see using and put a set of
  373. 14:51attributes
  374. 14:52and
  375. 14:53the meaning is
  376. 14:54the join will be performed based on
  377. 14:56those attributes so here in this case
  378. 14:59again the join will be based on course
  379. 15:00id
  380. 15:02ok so that was about different kinds of
  381. 15:05join that we can do which we going
  382. 15:07forward we will see that ah
  383. 15:09form a very critical
  384. 15:11as a credit critical place in terms of
  385. 15:13query formulation
  386. 15:15now we take you to a different concept
  387. 15:17known as views
  388. 15:19now we have seen
  389. 15:22that
  390. 15:23so far we have been computing certain
  391. 15:27query results based on one or more
  392. 15:29relations one or more instances
  393. 15:32now in some cases ah we may want
  394. 15:36the
  395. 15:36result to be restrictive in terms of
  396. 15:39based on the user or based on the
  397. 15:42context in which the result should be
  398. 15:44used so we may not want
  399. 15:47all fields of a result to be visible to
  400. 15:50all the users or to the application so
  401. 15:54we may not expose the whole logical
  402. 15:56model and in those cases we introduce a
  403. 16:00view so here we are
  404. 16:03showing one where
  405. 16:05from the instructor relation we are only
  406. 16:09picking up three fields and we are not
  407. 16:11picking up the salary field
  408. 16:13now you would ah
  409. 16:15think that well this is what we can do
  410. 16:18in terms of the normal query and
  411. 16:20certainly then what is the point of
  412. 16:23using this
  413. 16:24now
  414. 16:26what we can do is
  415. 16:28we can create this not just as a query
  416. 16:31but as a view
  417. 16:33one we once we create this as a view it
  418. 16:36actually this ah
  419. 16:39query expression is treated as what is
  420. 16:42known as a view expression
  421. 16:44and every time you want to use that view
  422. 16:48the
  423. 16:48actual tuples in that view are computed
  424. 16:51but this is not actually a relation that
  425. 16:54exist in the database so it is kind of
  426. 16:58can be
  427. 16:59thought of as a kind of
  428. 17:02virtual relation which
  429. 17:04exists which can be seen
  430. 17:07only when you use that
  431. 17:09so
  432. 17:10there is a subtle but very strong
  433. 17:12difference between actually computing a
  434. 17:15result through
  435. 17:17a select query
  436. 17:19and
  437. 17:20defining a view based on a select query
  438. 17:24and then making use of the view as if it
  439. 17:27were actually a relation that existed
  440. 17:30so to do this this is how we go about
  441. 17:34it is
  442. 17:34the syntax is very similar to the create
  443. 17:36table so you do a create view give a
  444. 17:39name and then
  445. 17:41ah you specify ah as is the connective
  446. 17:44and specify the query expression which
  447. 17:45is an sql query which will let you
  448. 17:48compute the view every time you actually
  449. 17:51need it
  450. 17:53so this is a view name once a view is
  451. 17:55defined the view name can be used as a
  452. 17:58virtual relation it can be used exactly
  453. 18:01as we use any of the
  454. 18:04really existing relation the conceptual
  455. 18:07relations that we have created through
  456. 18:09create table
  457. 18:10so
  458. 18:11it is the difference is this is what
  459. 18:14needs to be understood very well the
  460. 18:16view definition is
  461. 18:18not the same as creating a new relation
  462. 18:21once you create the new relation the
  463. 18:23time you have created it you get the
  464. 18:25result and that result is explicitly
  465. 18:28available as a set of tuples as a table
  466. 18:32rather a view is a definition
  467. 18:34which you store in the database as an
  468. 18:37expression so every time you make use of
  469. 18:40that view at that time
  470. 18:43the set of tuples are computed
  471. 18:46it is not existing in the database as
  472. 18:48stored like the real relations
  473. 18:50and based on that computation all the
  474. 18:54rest of the query will actually be
  475. 18:56executed so let us
  476. 18:58take a quick look this is a create view
  477. 19:01we have created the view of a of view
  478. 19:04called faculty from instructor
  479. 19:07relation instructor relation is the real
  480. 19:09one the
  481. 19:10existing one and faculty is a view
  482. 19:12expression being created
  483. 19:15and in that what we have done simply we
  484. 19:17have taken a done a projection we have
  485. 19:20left out the salary field
  486. 19:23now we can make use of that
  487. 19:26view you can see that we are doing from
  488. 19:29faculty so this actually is a view but
  489. 19:32this behaves as if this is valid
  490. 19:35relation so from faculty we are trying
  491. 19:38to find out the name of all those
  492. 19:39faculty who belong to the biology
  493. 19:42department so what will happen when i
  494. 19:44want to execute this query this will
  495. 19:46refer to this view
  496. 19:48so to execute this query it will have to
  497. 19:50first
  498. 19:51execute this query
  499. 19:53get the temporary virtual instance of
  500. 19:56the virtual relation created and based
  501. 19:58on that this query will be computed and
  502. 20:00the results will be given accordingly so
  503. 20:02that is the basic purpose of the view
  504. 20:04that the whole thing the whole view
  505. 20:06expression remains as an abstraction in
  506. 20:09the database and computed whenever it is
  507. 20:11used so this is showing you another view
  508. 20:14which
  509. 20:16shows certain computed information for
  510. 20:18example we are creating a view for
  511. 20:20departmental total salary which will
  512. 20:23show as two fields department name and
  513. 20:25total salary which has been created by
  514. 20:28aggregation so any time we ah make use
  515. 20:32of this this view in a from clause we
  516. 20:35will get we will feel as if
  517. 20:38such a relation really exist where the
  518. 20:40department name
  519. 20:41and the total salary of the instructors
  520. 20:43in that department are stored but it
  521. 20:46really does not exist it is computed
  522. 20:48whenever it is needed whenever it is
  523. 20:50used
  524. 20:53you can actually
  525. 20:54use views to create other views for
  526. 20:56example this is one view which is
  527. 20:59the view of physics fault 2009 which are
  528. 21:02all courses that are
  529. 21:04offered
  530. 21:05in physics from the physics department
  531. 21:08in the semester fall of year 2009 and
  532. 21:11using that
  533. 21:12we can
  534. 21:14create another view see here again we
  535. 21:17are in the from clause we are using this
  536. 21:19view so creating this using this view we
  537. 21:22are creating yet another view which show
  538. 21:25the courses that run in the
  539. 21:27watts and building so views can be used
  540. 21:30as i have already said as any other
  541. 21:33actual relation but they do not really
  542. 21:35exist
  543. 21:36so if you expand out if you just
  544. 21:40put
  545. 21:41the physics fall 2009
  546. 21:44expression within the
  547. 21:47within the view
  548. 21:50definition of physics fault 2009 watson
  549. 21:53this is the your earlier view relation
  550. 21:56so this is known as view expansion so
  551. 21:58this is actually the query that you are
  552. 22:00executing
  553. 22:05so we as we have said views can be
  554. 22:07defined in directly ah from one ah
  555. 22:10relation so these are called ah direct
  556. 22:14dependence or they could be defined in
  557. 22:17terms of a chain of relations v one in
  558. 22:19terms of v two v two in terms of v three
  559. 22:21and so on
  560. 22:22and
  561. 22:23a view relation can be recursive also
  562. 22:26that a view could be in terms of itself
  563. 22:28and that has a lot of
  564. 22:31value lot of power
  565. 22:32ah view expansion is a process that sql
  566. 22:35uses to evaluate a view so
  567. 22:38i would ah request you to study this and
  568. 22:41understand that this process works this
  569. 22:42is pretty much like
  570. 22:44ah pseudo code c program
  571. 22:48now moving to recursive views the views
  572. 22:50where the same relation can be
  573. 22:53used in the view
  574. 22:55to define another view
  575. 22:58we need like every other
  576. 23:00recursive structure we need first a non
  577. 23:04recursive statement which is called the
  578. 23:06seed statement
  579. 23:07we need a recursive statement which can
  580. 23:09recur
  581. 23:10we need a connection operator which can
  582. 23:12connect the
  583. 23:15non recursive and the recursive results
  584. 23:17together put them together
  585. 23:19the only connective that is valid is
  586. 23:21union all that is multiset union
  587. 23:25and we also need some kind of a terminal
  588. 23:28condition to guarantee that the
  589. 23:30recursion really
  590. 23:32terminates it does not go on forever so
  591. 23:36let us take an example so
  592. 23:38this is in context of a relation flights
  593. 23:41where the four fields are as specified
  594. 23:44and there is an instance shown which
  595. 23:45show that different source destination
  596. 23:48of different carriers ah carrying people
  597. 23:51from one source to the other destination
  598. 23:53and what we want to find is all
  599. 23:56destinations that can be reached from
  600. 23:58paris
  601. 23:59now you can see that
  602. 24:00from paris if i can reach detroit
  603. 24:04and from detroit i can reach san jose
  604. 24:06then i can actually reach
  605. 24:09san jose from paris so that is the basic
  606. 24:12reachability so that will necessarily if
  607. 24:15i want to compute that then i will be
  608. 24:18able to compute this ah very easily by
  609. 24:22doing natural join of
  610. 24:26flights with flights ah provided i take
  611. 24:31say
  612. 24:32source
  613. 24:36let us compute it like this flights
  614. 24:41f 1
  615. 24:42join
  616. 24:44flights f two
  617. 24:47and i will have f one dot
  618. 24:50destination
  619. 24:51equal to f two dot source
  620. 24:55so the idea is if something goes from
  621. 24:57paris to detroit
  622. 24:59that is in f1
  623. 25:01and if some flight goes from detroit to
  624. 25:03san jose that is in f2
  625. 25:05then the destination in f1 and the
  626. 25:07source in f2 have to be equated so if we
  627. 25:11do this kind of a self equi joint then
  628. 25:13we will be able to find out ah
  629. 25:17all flights that go from paris to san
  630. 25:20jose or all places that you can reach
  631. 25:22from paris
  632. 25:24in one hop
  633. 25:26naturally once you
  634. 25:28reach
  635. 25:29ah once you do that then
  636. 25:32you may be able to go to another
  637. 25:35destination in two hops
  638. 25:37and once you do that then you may be
  639. 25:39able to reach another yet another
  640. 25:40destination in three hops and so on so
  641. 25:42we do not really know how many hops
  642. 25:45maximum would be required to compute
  643. 25:47this reachability information so that is
  644. 25:50the reason we need to make use of the
  645. 25:52recursion
  646. 25:54and so this is how we express it
  647. 25:57so um if you if you look into this we
  648. 26:00are
  649. 26:00specifying that is a recursive view it
  650. 26:02will happen now with itself this is the
  651. 26:05name and this is what we want to compute
  652. 26:08ah source destination and we take
  653. 26:10another dummy attribute kind of ah which
  654. 26:13specify the depth of recursion so the
  655. 26:17present instance of the
  656. 26:19relation is at depth zero so which
  657. 26:21defines your non recursive seed part
  658. 26:26so say select
  659. 26:27so you have renamed is at flights as
  660. 26:29route you have specified that the it has
  661. 26:32to start from
  662. 26:33paris
  663. 26:34and
  664. 26:35you can find out the source destination
  665. 26:38pair at depth zero
  666. 26:40then you specify the recursive part
  667. 26:43that is the second hop has to be defined
  668. 26:46so he is saying that if you had the
  669. 26:48reachability
  670. 26:50then call lets call it ah
  671. 26:52in one this reachability may be in one
  672. 26:55hop that is at depth zero maybe in two
  673. 26:57hops that is a depth one maybe at three
  674. 26:59hops that is a depth two
  675. 27:01and you take another instance of flight
  676. 27:03as out one and what you need is the
  677. 27:06destination in the first in one
  678. 27:09has to be same as the
  679. 27:11source in the other so that they get
  680. 27:13connected
  681. 27:15and then you output the source
  682. 27:17from the first one destination from the
  683. 27:20second one and naturally the depth has
  684. 27:22got incremented because you have done
  685. 27:24added one more
  686. 27:26hop
  687. 27:27and
  688. 27:28so this is ah the
  689. 27:30and you add another condition saying
  690. 27:32that
  691. 27:33in one dot depth should be less than
  692. 27:35equal to hundred this is as i mentioned
  693. 27:37is a terminal condition which
  694. 27:39make sure that you do not get into
  695. 27:41infinite recursion so
  696. 27:43this view recursive view cannot be used
  697. 27:45to compute
  698. 27:47any reachability which is which has
  699. 27:50more more than 101 hops
  700. 27:53so that is uh to be noted and finally we
  701. 27:56need to connect these two results which
  702. 27:58is the initial start seed and the
  703. 28:02recursive one so this is the connection
  704. 28:04operator so this is ah basically the
  705. 28:07idea of the recursive view ah those of
  706. 28:10you who are
  707. 28:11more familiar with discrete structure
  708. 28:14would have known or i mean relations in
  709. 28:17some more depth you would know that we
  710. 28:18can define a transitive closure of a
  711. 28:21binary relation so this recursive view
  712. 28:23is necessarily computing the transitive
  713. 28:25closure from the fright relation so this
  714. 28:28is the instance
  715. 28:29of the flights and on the final
  716. 28:31computation this is what you get this
  717. 28:34gives you all the destinations that can
  718. 28:36eventually be reached
  719. 28:37from the from the source paris
  720. 28:41so the recursive is very very powerful
  721. 28:44in the sense that without recursion a
  722. 28:49a non-recursive version can only find
  723. 28:51flights up to a certain number of
  724. 28:54hops and whatever
  725. 28:57query you write it is always possible to
  726. 28:59write out a database
  727. 29:01instance which will have more hops and
  728. 29:03your query will necessarily fail
  729. 29:05so ah we make use of the
  730. 29:09recursion here to make sure that
  731. 29:12you can actually
  732. 29:14extend this to whatever depth you want
  733. 29:17and to compute this we ah keep on
  734. 29:21computing till no changes are possible
  735. 29:24and
  736. 29:25in that sense this recursive views are
  737. 29:27said to be monotonic in that every time
  738. 29:30you compute your result necessarily
  739. 29:32becomes larger and that is the reason
  740. 29:34you you for for the purpose of being
  741. 29:37being monotonic
  742. 29:38you are actually making use of the union
  743. 29:42all so that makes it all inclusive
  744. 29:47so
  745. 29:48now if i if we go and this is the
  746. 29:51instance and this you can here i have
  747. 29:53shown that how the iteration actually
  748. 29:56happens in the iteration 0 in the
  749. 29:57flights itself you had 3 destinations
  750. 30:00then you add 2 more in iteration 1 in
  751. 30:02iteration 2 you do not add anything else
  752. 30:04so your result henceforth will not
  753. 30:06change so you have reached a fixed point
  754. 30:08and your computations are over
  755. 30:11you can also update a view you can
  756. 30:13insert a
  757. 30:15record directly into a view but since
  758. 30:17view only is a partial information on
  759. 30:19the relation when you insert into a view
  760. 30:21since view is virtual there will have to
  761. 30:23be an insertion in the real relation and
  762. 30:26in the real relation you may not know
  763. 30:27certain fields so if you are doing this
  764. 30:29insertion into faculty which is a view
  765. 30:32of instructor then the salary field is
  766. 30:34not known so in the actual instructor a
  767. 30:36null will have to get inserted in the ah
  768. 30:39salary field so the salary field needs
  769. 30:42to be nullable kind of field so
  770. 30:45updates on views have certain
  771. 30:47restrictions
  772. 30:48so there are some more instances that i
  773. 30:50have given which you can
  774. 30:52study and
  775. 30:53try to understand that what are the
  776. 30:55difficulties of
  777. 30:57updating on the view so it can be done
  778. 31:00but it has to be done in a restrictive
  779. 31:02sense so these are the different
  780. 31:04conditions that has to happen for views
  781. 31:07to be updated so please ah go through
  782. 31:09these slides to understand what
  783. 31:12are there in terms of the views
  784. 31:15ah finally view is a virtual relation
  785. 31:17but it can be materialized also that is
  786. 31:20materializing is basically computing a
  787. 31:23physical relation ah at the at the
  788. 31:25instance of the view but naturally if
  789. 31:27you materialize then there is a certain
  790. 31:29point of time where you have
  791. 31:30materialized where you have ah made it
  792. 31:33into a physical relation and hence if
  793. 31:35your ah original source data in the view
  794. 31:38changes in future the materialized view
  795. 31:41also need to be updated otherwise your
  796. 31:43data will get bad
  797. 31:46finally
  798. 31:47in in this module
  799. 31:48we
  800. 31:49mentioned that there is something called
  801. 31:51transactions which we will take up at a
  802. 31:54at a later stage in much depth this is
  803. 31:56just to
  804. 31:57get you familiar with the term a
  805. 31:59transaction is a is a unit of work which
  806. 32:01is usually atomic which is either fully
  807. 32:04executed or if it fails it will be
  808. 32:07rolled back
  809. 32:08as if it never occurred
  810. 32:10and
  811. 32:11this is required for isolation in
  812. 32:13concurrent transactions so we will talk
  813. 32:16about this lot more when we take up
  814. 32:18concurrency and related issues so con
  815. 32:21transactions implicitly begin and they
  816. 32:24end by either committing the work that
  817. 32:26they have successfully finished or
  818. 32:28rolling back that this cannot be done
  819. 32:32so
  820. 32:32there are some features
  821. 32:35in the sql
  822. 32:37for
  823. 32:37doing transactions and
  824. 32:40but usually in transactions commit by
  825. 32:43default and they only raise exceptions
  826. 32:46when the rollback is happening and we
  827. 32:48will see more of that later
  828. 32:50so to summarize in this module we have
  829. 32:53learnt about
  830. 32:55two important sql features in terms of
  831. 32:58join and views and we just introduce the
  832. 33:00basic notion of committing transactions

About this transcript

This page contains the full transcript of Intermediate SQL/1 by Data Base Management System - IITKGP, generated from the public captions YouTube serves with the video. The transcript has 4,659 words across 832 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.