|
| 1 | + |
| 2 | +Syntax |
| 3 | + |
| 4 | +Problem - 1 : |
| 5 | + |
| 6 | +create database exam; |
| 7 | + |
| 8 | +use exam; |
| 9 | + |
| 10 | +create table students(ID int primary key, Name varchar(30)); |
| 11 | + |
| 12 | +create table friends (ID int, friend_ID int, primary key(ID), foreign key (friend_ID) references students(ID)); |
| 13 | + |
| 14 | +create table packages(ID int primary key, salary double, foreign key (ID) references students(ID)); |
| 15 | + |
| 16 | +insert into students values(1,"ashley"); |
| 17 | +insert into students values(2,"samantha"); |
| 18 | +insert into students values(3,"julia"); |
| 19 | +insert into students values(4,"scarlet"); |
| 20 | + |
| 21 | +insert into friends values(1, 2); |
| 22 | +insert into friends values(2, 3); |
| 23 | +insert into friends values(3, 4); |
| 24 | +insert into friends values(4, 1); |
| 25 | + |
| 26 | +insert into packages values (1, 15.20); |
| 27 | +insert into packages values (2, 10.06); |
| 28 | +insert into packages values (3, 11.55); |
| 29 | +insert into packages values (4, 12.12); |
| 30 | + |
| 31 | +select students.Name as higher_package_students, friends.friend_ID from students join friends on students.ID = friends.ID where (select salary from packages where ID = students.ID) > (select salary from packages where friends.friend_ID = students.ID); |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +problem 2: |
| 36 | + |
| 37 | + 3NF : |
| 38 | + * NF nf is normalization form, we are doing normalization to eliminate data redundency in our |
| 39 | + table. |
| 40 | + * It increases performance while manipulating data present in table; |
| 41 | + * Also helps in seggregating the data. |
| 42 | + |
| 43 | + Rule : |
| 44 | + |
| 45 | + * to perform 3NF the table should be in 2NF. |
| 46 | + * there should not be any transitive dependency of non primary key on super key. |
| 47 | + |
| 48 | + Before table: |
| 49 | + |
| 50 | + schema :- id int, totalMark int, prize varchar(30) |
| 51 | + |
| 52 | + After table: |
| 53 | + |
| 54 | + schema (1st table) :- id int, totalMark int |
| 55 | + schema (1st table) :- id int, prize varchar(30) |
| 56 | + |
| 57 | + |
| 58 | +problem 3: |
| 59 | + ACID properties : |
| 60 | + |
| 61 | + Transaction is a servise provided by the Entity Factory Manager, using transaction we can bind queries that are to be excecuted at same time. (for example, credit and debit) both action should be inside transaction to avoid anomaly. transaction should have following properties, |
| 62 | + |
| 63 | + * Atomic - transaction is atomic, queries inside it act toghether as a single unit and excecute simultaneously when it is commited. |
| 64 | + |
| 65 | + * consistant - it should be consistant at after the end of the transaction. |
| 66 | + |
| 67 | + * Isolated - it should not be interdependant, queries inside transaction should be isolated it should not depend upon any other query or transaction. |
| 68 | + |
| 69 | + * Durable - it should be durable, after firing the query there should not be any anomaly in the future while manupulating. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +Problem 4: |
| 74 | + |
| 75 | +create table person(id int primary key, lastName varchar(30), firstName varchar(30)); |
| 76 | + |
| 77 | +create table Address(addressId int primary key, personId int, city varchar(20), state varchar(20)); |
| 78 | + |
| 79 | +insert into person values(1, "wang", "allen"); |
| 80 | +insert into person values(2, "alice", "bob"); |
| 81 | + |
| 82 | +insert into Address values(1, 2, "New york city", "New york"); |
| 83 | +insert into Address values(2, 3, "Leetcode", "Clifornia"); |
| 84 | + |
| 85 | +select person.firstName, person.lastName, Address.city, Address.state from person left join Address on person.id = Address.personId; |
0 commit comments