Professional Documents
Culture Documents
part of a company's intranet that is extended to users outside the company, usually via the Internet.
Internet:
The Internet is a global system of interconnected computer networks that use the standard Internet Protocol Suite
(TCP/IP) to serve billions of users worldwide.
Internet
1. Internet is wide network of computers and is open for all.
2. Internet itself contains a large number of intranets.
3. The number of users who use internet is Unlimited.
4. The Visitors traffic is unlimited.
5. Internet contains different source of information and is available for all.
Intranet
1. Intranet is also a network of computers designed for a specific group of users.
2. Intranet can be accessed from Internet but with restrictions.
3. The number of users is limited.
4. The traffic allowed is also limited.
5. Intranet contains only specific group information.
Circuit Switching:
1.) In circuit switching, there are various nodes used in the network through which the signals are passed from one
system to another.
2.) In this a number of nodes are available between two devices and hence multiple paths are available to pass a
signal.
3.) This concept is mainly used in telephony systems.
4.) This is best used for transmission of audio signals and not suitable for data transmission.
5.) This type of switching is connection oriented and may be connectionless also.
6.) Due to its old version, this technique is less popular and more expensive.
Packet Switching:
1.) In packet switching, the data is sent over the network in the form of packets i.e. a large unit of data items
wrapped into a single bigger unit.
2.)In these, the packets follows the same path that is defined for them before passing into the network.
3.) It can be used for telephony, DSL services and other data transmission services.
4.) It is best used for sending data over the network and audio and video signals can also be sent over the network
in the form of packets.
5.) It is usually a connection less service.
6.) It is a new technology and economic than the circuit switching approach.
Differences between circuit switching and packet switching
Circuit switching concept is used in Telephony networks where a dedicated line is assigned to particular
connection, the connection in this case is permanent during the connection.Considerable amount of bandwidth is
wasted in this process and at a time only one way communication is possible. Whereas in case of packet switching
packet are send on different path based on the address(Virtual or IP or MAC).Packet switching is more flexible as
well as two communication of packets is possible. Packet switching is less reliable than circuit switching as in
second case connection is fixed during operation.
Database Administrator [DBA]
Definition
Centralized control of the database is exerted by a person or group of persons under the supervision of a high-level
administrator. This person or group is referred to as the database administrator (DBA). They are the users who are
most familiar with the database and are responsible for creating, modifying, and maintaining its three levels.
Database Administrator is responsible to manage the DBMSs use and ensure that the
database is functioning properly.
DBA administers the three levels of database and consultation with the overall user community, sets up the
definition of the global view of the various users and applications and is responsible the definition and
implementation of the internal level, including the storage structure and access methods to be used for the optimum
performance of the DBMS.
DBA is responsible for granting permission to the users of the database and stores the
profile of each user in the database.
Responsibilities of DBA
Deciding the information content of the database
It is the DBAs job to decide exactly what information is to be held in the database - in other
words, to identify the entities of interest to the enterprise and to identify the information to
be recorded about those entities. Having done this, the DBA must then define the content
of the database by writing the conceptual schema.
Liaising with the users
It is the business of the DBA to liaise with users, to ensure that the data they require is
available, and to write the necessary external schemas. In addition, the mapping between
any given external schema and the conceptual schema must also be specified. In practice
the external DDL will probably include the means for specifying the mapping, but the
schema and the mapping should be clearly distinguishable.
Defining authorization checks and validation procedures
Authorization checks and validation procedures may be considered as logical extensions of
the conceptual schema. The conceptual DDL will include facilities for specifying such
checks and procedures.
Defining a strategy for backup and recovery
Once an enterprise is committed to a database, it become critically dependent on the
successful operation of that system. In the event of damage to any portion of the database
caused by human error, say, or a failure in the hardware or supporting operating system
it is essential to be able to repair the data concerned with a minimum of delay and with as
little effect as possible on the rest of the system.
Monitoring performance and responsibilities to changes in requirements
The DBA is responsible for so organizing the system as to get the performance that is best
for the enterprise and for making the appropriate adjustments change. Any change to
details of storage and access must be accompanied by a corresponding change to the
definition of the mapping to storage, so that the conceptual schema may remain constant.
Static Allocation means, that the memory for your variables is automatically allocated, either on the Stack or in
other sections of your program. You do not have to reserve extra memory using them, but on the other hand, have
also no control over the lifetime of this memory. E.g: a variable in a function, is only there until the function
finishes.
void func() {
int i; /* `i` only exists during `func` */
}
Dynamic memory allocation is a bit different. You now control the exact size and the lifetime of these memory
locations. If you don't free it, you'll run into memory leaks, which may cause your application to crash, since it, at
some point cannot allocation more memory.
int* func() {
int* mem = malloc(1024);
return mem;
}
int* mem = func(); /* still accessible */
In the upper example, the allocated memory is still valid and accessible, even though the function terminated.
When you are done with the memory, you have to free it:
free(mem);
Dynamic memory allocation
Is memory allocated at runtime using calloc(), malloc() and friends. It is sometimes also referred to as
'heap' memory, although it has nothing to do with the heap data-structure ref.
int * a = malloc(sizeof(int));
Heap memory is persistent until free() is called. In other words, you control the lifetime of the variable.
Automatic memory allocation
This is what is commonly known as 'stack' memory, and is allocated when you enter a new scope (usually when a
new function is pushed on the call stack). Once you move out of the scope, the values of automatic memory
addresses are undefined, and it is an error to access them.
int a = 43;
Note that scope does not necessarily mean function. Scopes can nest within a function, and the variable will be inscope only within the block in which it was declared. Note also that where this memory is allocated is not
specified. (On a sane system it will be on the stack, or registers for optimisation)
Static memory allocation
Is allocated at compile time, and the lifetime of a variable in static memory is the lifetime of the program.
In C, static memory can be allocated using the static keyword. The scope is the compilation unit only.
Things get more interesting when the extern keyword is considered. When an extern variable is defined the
compiler allocates memory for it. When an extern variable is declared, the compiler requires that the variable be
defined elsewhere. Failure to declare/define extern variables will cause linking problems, while failure to
};
private:
double length;
return 0;
When the above code is compiled and executed, it produces the following result:
Object is being created
Length of line : 6
Parameterized Constructor:
A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps
you to assign initial value to an object at the time of its creation as shown in the following example:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line(10.0);
// get initially set length.
cout << "Length of line : " << line.getLength() <<endl;
// set line length again
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Object is being created, length = 10
Length of line : 10
Length of line : 6
Using Initialization Lists to Initialize Fields:
In case of parameterized constructor, you can use following syntax to initialize the fields:
Line::Line( double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
Above syntax is equal to the following syntax:
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
If for a class C, you have multiple fields X, Y, Z, etc., to be initialized, then use can use same syntax and separate
the fields by comma as follows:
C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
....
}
The Class Destructor:
A destructor is a special member function of a class that is executed whenever an object of it's class goes out of
scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor
can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program
like closing files, releasing memories etc.
As shown in the figure 1, if user-A wants to use the network; it need to first ask for the request to obtain the one
and then user-A can communicate with user-C. During the connection phase if user-B tries to call/communicate
with user-D or any other user it will get busy signal from the network.
Packet Switching
In packet switching network unlike CS network, it is not required to establish the connection initially. The
connection/channel is available to use by many users. But when capacity or number of users increases then it will
lead to congestion in the network. Packet switched networks are mainly used for data and voice applications
requiring non-real time scenarios.
As shown in the figure 2, if user-A wants to send data/information to user-C and if user-B wants to send data to
user-D, it is simultaneously possible. Here information is padded with header which contains addresses of source
and destination. This header is sniffed by intermediate switching nodes to determine their route and destination.
In packet switching, station breaks long message into packets. Packets are sent one at a time to the network.
Packets are handled in two ways, viz. datagram and virtual circuit.
In datagram, each packet is treated independently. Packets can take up any practical route. Packets may arrive out
of order and may go missing.
In virtual circuit, preplanned route is established before any packets are transmitted. The handshake is established
using call request and call accept messages. Here each packet contains virtual circuit identifier(VCI) instead of the
destination address. In this type, routing decisions for each packet are not needed.
Comparison between CS vs. PS networks
As shown above in Packet switched (PS) networks quality of service (QoS) is not guaranteed while in circuit
switched (CS) networks quality is guaranteed.
PS is used for time insensitive applications such as internet/email/SMS/MMS/VOIP etc.
In CS even if user is not talking the channel cannot be used by any other users, this will waste the resource capacity
at those intervals.
The example of circuit switched network is PSTN and example of packet switched network is GPRS/EDGE.
Following table summarizes difference between circuit switching and packet switching of type datagram and
virtual circuit.
Packet Switching(Datagram
Packet Switching(Virtual Circuit type)
type)
Dedicated path
No Dedicated path
No Dedicated path
Path is established for entire
Route is established for each
Route is established for entire conversation
conversation
packet
call setup delay as well as packet transmission
Call setup delay
packet transmission delay
delay
Overload may block call setup and increases
Overload may block call setup
Overload increases packet delay
packet delay
Fixed bandwidth
Dynamic bandwidth
Dynamic bandwidth
No overhead bits after call setup overhead bits in each packet
overhead bits in each packet
Circuit Switching
Message Switching
Figure-1 depicts message switching operation. As shown in the figure, each router waits until it receives the entire
message. Once it receives the complete message it transmits the same over the next link and so on. All the routers
over the router does the same.
10
11
Message switching overhead is lower compare to packet switching. Fig-3 depicts that single datagram is
transmitted in message switching. As mentioned, message is appended with header before transmission. In packet
switching message is divided into smaller packets amd each packet is appended with header before transmission.
Overhead in message switching = header/(header+message)
Overhead in packet switching = [n*header/(n*header+message)],
Where, n = [message/packet_size]
Message switching has higher reliability and lower complexity. As in message switching, one single datagram is
either received or lost. One single network path is used for the same.
In packet switching, many packets generated by same node and belonging to the same destination may take
different paths. The packets received out of order will need to be sequenced using sequence number embedded in
the header part. May lose or corrupt a subset of the message but do not discard the entire message as in message
switching. Hence based on overall corrupt message received, what could be correct message can be interpreted.
Due to this reason, sometimes in the real time scenarios such as voice, message switching is not possible.
As explained above message switching takes more time compare to packet switching as entire message will be
stored at each of the hop points till it is completely received.
12
Database Keys
Keys are very important part of Relational database. They are used to establish and identify relation between tables.
They also ensure that each record within a table can be uniquely identified by combination of one or more fields
within a table.
Super Key
Super Key is defined as a set of attributes within a table that uniquely identifies each record within a table. Super
Key is a superset of Candidate key.
Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected. It is an attribute or set of
attribute that can act as a primary key for a table to uniquely identify each record in that table.
Primary Key
Primary key is a candidate key that is most appropriate to become main key of the table. It is a key that uniquely
identify each record in a table.
13
Composite Key
Key that consist of two or more attributes that uniquely identify an entity occurance is called Composite key. But
any attribute that makes up the Composite key is not a simple key in its own.
14
kept to minimum.
Data Sharing
The integration of the whole data in an organization leads to the ability to produce more information from a given
amount of data.
Enforcing Integrity Constraints
DBMSs should provide capabilities to define and enforce certain constraints such as data type, data uniqueness, etc.
Restricting Unauthorised Access
Not all users of the system have the same accessing privileges. DBMSs should provide a security subsystem to
create and control the user accounts.
Data Independence
System data (Meta Data) descriptions are separated from the application programs. Changes to the data structure is
handled by the DBMS and not embedded in the program.
Transaction Processing
The DBMS must include concurrency control subsystems to ensure that several users trying to update the same data
do so in a controlled manner. The results of any updates to the database must maintain consistency and validity.
Providing multiple views of data
A view may be a subset of the database. Various users may have different views of the database itself. Users may not
need to be aware of how and where the data they refer to is stored.
Providing backup and recovery facilities
If the computer system fails in the middle of a complex update process, the recovery subsystem is responsible for
making sure that the database is restored to the stage it was in before the process started executing.
Managing information
Managing information means taking care of it so that it works for us, and is useful for the work we are doing. The
information we collect is no longer subject to accidental disorganization and becomes more easily accessible and
integrated with the rest of our work. Managing information using a database allows us to become strategic users of
the data we have.
Disadvantage of DBMS
Although there are many advantages of DBMS, the DBMS may also have some minor disadvantages. These are:
A processor with high speed of data processing and memory of large size is required to run the DBMS software. It
means that you have to up grade the hardware used for file-based system. Similarly, DBMS software is also very
costly,.
When a computer file-based system is replaced with database system, the data stored into data file must be
converted to database file. It is very difficult and costly method to convert data of data file into database. You have
to hire database system designers along with application programmers. Alternatively, you have to take the services
of some software house. So a lot of money has to be paid for developing software.
Most database management system are often complex systems so the training for users to use the DBMS is required.
Training is required at all levels, including programming, application development, and database administration. The
organization has to be paid a lot of amount for the training of staff to run the DBMS.
15
The trained technical persons such as database administrator, application programmers, data entry operations etc. are
required to handle the DBMS. You have to pay handsome salaries to these persons. Therefore, the system cost
increases.
Database Damage
In most of the organization, all data is integrated into a single database. If database is damaged due to electric failure
or database is corrupted on the storage media, the your valuable data may be lost forever.
Router vs Bridge
Basically, a router determines the fastest way possible, which is also usually the shortest way possible, in a particular
network. It has the capability to route the packets through the most effective determined route.
Routers have the ability to allow hosts that arent practically on the same logical network, to be able to communicate
with each other. Every router can receive chunks of data, which are called packets, on an interface. It will then
forward the data packets to the intended location in the most efficient manner. The directing, or routing, of packets is
based on the routing table, by allowing routers to know where a particular network is found.
Aside from being a device, a router can be software in a computer. Routers should be, at least, connected to two
networks. It is sort of a gateway to another network. Functionally, it is capable of generating traffic between
logically separated networks.
The third layer, which is the network layer of the OSI model, is where routers operate. Understanding the OSI model
is the key to figuring out differences between routers, gateways and bridges. The network layer is responsible for
moving packets from a particular port to another. It is based on addresses (L3) such as, IPv4, IPv6, and IPX, or
Appletalk, addresses.
A bridge, or network bridge, is a device that can also connect networks, but unlike a router, its operation does not
include the network layer of the OSI model. Only the one and two layers are included in a bridges operation the
physical layer and the data link layer respectively. It can only connect existing networks that you can access. It is
basically not concerned with, and is unable to distinguish networks, unlike a router. They can only be used when you
intend to connect networks of same type.
In bridging mode, the process does not bother to understand network communications protocol, such as IP
addresses. It only recognizes and considers the physical means, like the Media Access Control (MAC) address,
which is usually an Ethernet. Thus, traffic will only exist in a bridged network if the networks concerned are
logically the same.
In terms of practicality, routers are more favored, because routing is more efficient and offers easier call
management. Bridging is required for cases where you cannot subnet an IP network, and for cases where you need
to utilize non-routable protocols, like DECnet or NetBIOS.
A router, or routing, is considered more intelligent than a bridge, or bridging, because they make smarter decisions.
It only sends a packet to its intended destination, eliminating unnecessary traffic. With regards to routers, there is
improved call management, while for bridging, call management and performance is sacrificed, as packets are
automatically broadcasted to all the computers on a network.
Summary:
16
6 );
Before there were window functions, aggregations were made only with GROUP BY. A typical question that we
could ask our database using SQL is: What are the top 3 average government debts in percent of the GDP for those
countries whose GDP per capita was over 40000 dollars in every year in the last four years Whew. Some
(academic) business requirements.
In SQL (PostgreSQL dialect), we would write:
1 select code, avg(govt_debt)
2 from countries
3 where year > 2010
4 group by code
5 having min(gdp_per_capita) >= 40000
6 order by 2 desc
17
7 limit 3
Or, with inline comments
1 -- The average government debt
2 select code, avg(govt_debt)
3
4 -- for those countries
5 from countries
6
7 -- in the last four years
8 where year > 2010
9
10 -- yepp, for the countries
11 group by code
12
13 -- whose GDP p.c. was over 40'000 in every year
14 having min(gdp_per_capita) >= 40000
15
16 -- The top 3
17 order by 2 desc
18 limit 3
The result being:
code
avg
-----------JP
193.00
US
91.95
DE
56.00
Remember the 10 easy steps to a complete understanding of SQL:
1. FROM generates the data set
2. WHERE filters the generated data set
3. GROUP BY aggregates the filtered data set
18
select 1
from countries
5 );
And we would get, again:
answer
-----t
but lets focus on the plain HAVING clause.
Not everyone knows that HAVING can be used all by itself, or what it even means to have HAVING all by itself.
Already the SQL 1992 standard allowed for the use of HAVING without GROUP BY, but it wasnt until the
introduction of GROUPING SETS in SQL:1999, when the semantics of this syntax was retroactively
unambiguously defined:
7.10 <having clause>
19
20
Now, we cannot understand the grand total (empty GROUP BY ( ) clause), without having a short look at the
SQL:1999 standard GROUPING SETS. Some of you may have heard of CUBE() or ROLLUP() grouping
functions, which are just syntactic sugar for commonly used GROUPING SETS. Lets try to answer this question in
a single query:
What are the highest GDP per capita values per year OR per country
In SQL, well write:
1 select code, year, max(gdp_per_capita)
2 from countries
3 group by grouping sets ((code), (year))
Which yields two concatenated sets of records:
code
year
max
-----------------------NULL
2009
46999.00 <- grouped by year
NULL
2010
48358.00
NULL
2011
51791.00
NULL
2012
52409.00
CA
NULL
52409.00 <- grouped by code
DE
NULL
44355.00
FR
NULL
42578.00
GB
NULL
38927.00
IT
NULL
36988.00
JP
NULL
46548.00
RU
NULL
14091.00
US
NULL
51755.00
Thats kind of nice, isnt it? Its essentially just the same thing as this query with UNION ALL
1 select code, null, max(gdp_per_capita)
2 from countries
3 group by code
4 union all
5 select null, year, max(gdp_per_capita)
6 from countries
7 group by year;
In fact, its exactly the same thing, as the latter explicitly concatenates two sets of grouped records i.e. two
GROUPING SETS. This SQL Server documentation page also explains it very nicely.
And the most powerful of them all: CUBE()
21
Now, imagine, youd like to add the grand total, and also the highest value per country AND year, producing four
different concatenated sets. To limit the results, well also filter out GDPs of less than 48000 for this example:
1
select
from countries
(),
(code),
(year),
10
(code, year)
11 )
12 order by grp desc;
This nice-looking query will now produce all the possible grouping combinations that we can imagine, including the
grand total, in order to produce:
code
year
max
grp
--------------------------------NULL
NULL
52409.00
3 <- grand total
NULL
NULL
NULL
2012
2010
2011
52409.00
48358.00
51791.00
CA
US
NULL
NULL
52409.00
51755.00
US
2010
48358.00
0 <- group by code and year
CA
2012
52409.00
0
US
2012
51755.00
0
CA
2011
51791.00
0
US
2011
49855.00
0
And because this is quite a common operation in reporting and in OLAP, we can simply write the same by using the
CUBE() function:
1 select
2
22
4 from countries
5 where gdp_per_capita >= 48000
6 group by cube(code, year)
7 order by grp desc;
The CC and BCC fields when sending email work similarly. CC stands for carbon copy, while BCC stands for
blind carbon copy. While these terms may have been immediately obvious when email was invented, theyre
antiquated today.
CC and BCC are both ways of sending copies of an email to additional people. However, you can also send copies
of an email to additional people by specifying multiple addresses in the To field.
Carbon Copying Explained
The abbreviation CC comes from carbon copy. By placing a sheet of carbon paper between two pieces of paper,
the pressure from writing on the first piece of paper will push the ink from the carbon paper down onto the second
piece of paper, producing an additional copy of the document. Like a physical carbon copy, a CC is a way of sending
additional copies of an email to other people. Some people refer to CC as courtesy copy, which better describes
what a CC actually is. CC is often used as a verb, as in I CCd him on the email.
CC vs. BCC
When you CC people on an email, the CC list is visible to all other recipients. For example, if you CC
bob@example.com and jake@example.com on an email, Bob will know that Jake also received the email, while
Jake will know that Bob also received the email.
BCC stands for blind carbon copy. Unlike with CC, no one can see the list of recipients on the BCC list. For
example, if you have bob@example.com and jake@example.com in the BCC list, Bob wont know that Jake
received the email, and Jake wont know that Bob received the email.
Someone on the BCC list can see everything else, including the CC list and the contents of the email. However, the
BCC list is secret no one can see this list. (If a person is on the BCC list, theyll see only their own email on the
BCC list.)
To vs. CC
The To and CC fields work similarly. Whether you put four email addresses in the To field or put one email address
in the To field and three in the CC field, the four people will all receive the same email. Theyll also be able to see
every other recipients email address.
When it comes to email etiquette, the To field is generally used for the main recipients of your email. The CC field is
used to send a copy to other interested parties for their information. This isnt a concrete rule, and usage of To and
CC can vary.
For example, lets say your boss wants you to email a customer in response to a complaint. Youd put the customers
email address in the To field and your bosss email address in the CC field, so your boss would receive a copy of the
23
email. (If you didnt want the customer to see your bosss email address, youd put your bosss address in the BCC
field instead.)
When to Use CC and BCC
CC is useful when:
You want someone else to receive a copy of an email, but they arent one of the primary recipients.
You want someone else to receive an email, but you dont want the primary recipients of the email to see
youve sent this other person a copy. For example, if youre having a problem with a fellow employee, you
might send them an email about it and BCC the human resources department. HR would receive a copy for
their records, but your fellow employee wouldnt be aware of this.
You want to send a copy of an email to a large amount of people. For example, if you have a mailing list
with a large amount of people, you could include them in the BCC field. No one would be able to see
anyone elses email address if you CCd these people instead, you would be exposing their email
addresses and theyd see a long list of CCd emails in their email program. You could even put your own
email address in the To field and include every other address in the BCC field, hiding everyones email
address from each other.
Peer-to-peer (P2P) computing or networking is a distributed application architecture that partitions tasks or work
loads between peers. Peers are equally privileged, equipotent participants in the application. They are said to form a
peer-to-peer network of nodes.
Peers make a portion of their resources, such as processing power, disk storage or network bandwidth, directly
available to other network participants, without the need for central coordination by servers or stable hosts. [1] Peers
are both suppliers and consumers of resources, in contrast to the traditional client-server model in which the
consumption and supply of resources is divided. Emerging collaborative P2P systems are going beyond the era of
peers doing similar things while sharing resources, and are looking for diverse peers that can bring in unique
resources and capabilities to a virtual community thereby empowering it to engage in greater tasks beyond those that
can be accomplished by individual peers, yet that are beneficial to all the peers.
Natural join
More formally the semantics of the natural join are defined as follows:
where Fun is a predicate that is true for a relation r iff it is also true for relation s. It is usually
required that R and S must have at least one common attribute, but if this constraint is
omitted, and R and S have no common attributes, then the natural join becomes exactly the
Cartesian product.
Differences Between Structures and Classes
some of those differences to make the issue clear.
24
1. Structures are value types and the classes are reference types. Before proceeding to
the next point, let explain the difference between the two types. Imagine this is the
memory within the machine:
The value types are stored in the stack but the reference types are not. In fact, what
could be really stored in the stack for the reference types is a pointer that targets an
address at the heap level.
Then the type of structure objects are stored in the stack exactly like any value type,
say an integer, a double or a float. Meanwhile, memory locations could be reserved
for reference types in the heap. Defining the heap and stack and the difference
between them is beyond the scope of this article but nevertheless I propose this
excellent Matthew article to understand the memory mechanisms.
1.Classes are usually used for large amounts of data, whereas structs are usually
used for smaller amounts of data.
2. Classes can be inherited whereas structures not.
3. A structure couldn't be null like a class.
4. A structure couldn't have a destructor such as a class.
5. A structure can't be abstract, a class can.
6. You cannot override any methods within a structure except the following belonging to
the type object:
Equals()
GetHashCode()
GetType()
ToString()
25
7. Declared events within a class are automatically locked and then they are thread
safe, in contrast to the structure type where events can't be locked.
8. A structure must always have the default parameter less constructor defined as
public but a class might have one, so you can't define a private parameter-less
constructor as in the following:
struct Me
{
private Me()// compile-time error
{
}
}
class Me
{
private Me()// runs Ok{
}
9. A static constructor is triggered in the case of a class but not in the case of a
structure as in the following:
struct myStructure
{
static myStructure()
{
Console.WriteLine("This is me a structure");
}
}
class myClass
{
static myClass()
{
Console.WriteLine("This is me a class");
}
}
class Program
{
static void Main(string[] args)
{
myStructure s =new myStructure();//Nothing happen
myClass c =new myClass();//Will out put This is me a class
Console.Read();
}
26
10. The strucutre can't conatain a volatile field whereas the class can
11. You can't use sizeof with classes but you can with structures
12. Fields are automatically initialized with classes to 0/false/null wheatheras strucutres
are not
13. Fields can't be directley instantiated within structures but classes allow such
operations as in the following:
struct myStructure
{
publicstring x = 2;//Not allowed
}
class myClass
{
publicstring x = 2;//Allowed
}
14. Structures and classes don't adopt the same aproach for the System.Object.Equals()
method.
Assume the following strucutre and class:
struct StructurePerson
{
publicstring FirstName;
publicstring LastName;
}
class ClassPerson
{
publicstring FirstName;
publicstring LastName;
}
Now, try this code:
class Program
{
static void Main(string[] args)
{
StructurePerson strX =new StructurePerson();
strX.LastName = "Bejaoui";
strX.FirstName = "Bechir";
StructurePerson strY =new StructurePerson();
strY.LastName = "Bejaoui";
strY.FirstName = "Bechir";
27
if (strX.Equals(strY))
{
Console.WriteLine("strX = strY");
}
else
{
Console.WriteLine("strX != strY");
}//This code displays strX = strY
ClassPerson clsX =new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY =new ClassPerson();
clsY.LastName = "Bejaoui";
clsY.FirstName = "Bechir";
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else
{
Console.WriteLine("clsX != clsY");
}//This code displays clsX != clsY
Console.Read();
}
}
In the first strucutre the two objects are value types, they are compared depending
on their values like int I = 5 and int J = 5 so I=J because they have the same value. In
the contrast, in the class case of two different and distinct references, to make clsX =
clsY you should use the following code:
ClassPerson clsX =new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY = clsX;
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else
{
Console.WriteLine("clsX != clsY");
}//This code displays clsX = clsY
28
29
30
31
| Douglas Livingstone |
21 |
2 | 10.6667 |
32 |
| Mongane Wally Serote |
8 |
2 | 4.3333 |
13 |
+----------------------+------+------+---------+------+
2 rows in set (0.00 sec)
mysql> SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer WHERE copies_in_stock > 5 GROUP BY poet;
+----------------------+------+------+---------+------+
| poet
| max | min | avg
| sum |
+----------------------+------+------+---------+------+
| Douglas Livingstone |
21 |
9 | 15.0000 |
30 |
| Mongane Wally Serote |
8 |
8 | 8.0000 |
8 |
+----------------------+------+------+---------+------+
If you are unclear about why this is the case, take some time to reflect on the differences. The first query (with the
condition in the HAVING clause) applies the condition after all records have been returned. Since both of the
records have a max greater than 5, there is effectively no limiting of the results. The results are the same as they
would be without any conditions, as below:
mysql> SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer GROUP BY poet;
+----------------------+------+------+---------+------+
| poet
| max | min | avg
| sum |
+----------------------+------+------+---------+------+
| Douglas Livingstone |
21 |
2 | 10.6667 |
32 |
| Mongane Wally Serote |
8 |
2 | 4.3333 |
13 |
+----------------------+------+------+---------+------+
The second query, with the condition in the WHERE clause, applies the condition before starting to calculate the
results of the functions. Therefore, for Douglas Livingstone, only two results match the condition, and the
functions then go to work on this limited set of results. Have a look at the following results, which illustrate how
the WHERE clause determines the output.
mysql> SELECT * FROM writer WHERE copies_in_stock > 5;
+----------------------+-----------------------------------------+-----------------+
| poet
| anthology
|
copies_in_stock |
+----------------------+-----------------------------------------+-----------------+
| Mongane Wally Serote | No Baby Must Weep
|
8 |
| Douglas Livingstone | The Skull in the Mud
|
21 |
| Douglas Livingstone | Giovanni Jacopo Meditates on the High-IQ |
9 |
+----------------------+-----------------------------------------+-----------------+
32
The AVG(), for example, of the two Douglas Livingstone results is 15, and the MIN() is 9, matching with the
results in the earlier query.
Here are two more examples, showing the difference between HAVING and WHERE:
mysql> SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer GROUP BY poet HAVING poet > 'E';
+----------------------+------+------+--------+------+
| poet
| max | min | avg
| sum |
+----------------------+------+------+--------+------+
| Mongane Wally Serote |
8 |
2 | 4.3333 |
13 |
+----------------------+------+------+--------+------+
1 row in set (0.00 sec)
mysql> SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer WHERE poet > 'E' GROUP BY poet;
+----------------------+------+------+--------+------+
| poet
| max | min | avg
| sum |
+----------------------+------+------+--------+------+
| Mongane Wally Serote |
8 |
2 | 4.3333 |
13 |
+----------------------+------+------+--------+------+
Note that they both return the same results. The poet column appears in the list of columns, so poets with a name
starting after 'E' are returned. In this case, it makes no difference to the results which way you apply the condition the same records are always excluded. However, the positioning of the condition does make a difference to the
optimization. Since the WHERE clause is carried out first, while the HAVING clause is carried out last, after all
optimizations, it usually makes more sense to place a condition in the WHERE clause, and save the HAVING
clause for conditions that are applied to fields,
Changes to the HAVING clause in MYSQL 5.0.2
Older versions of MySQL allowed the HAVING clause to refer to any field listed after the SELECT statement.
The SQL standard requires the HAVING clause to also accept fields mentioned in the GROUP BY column, even if
they are not mentioned in the SELECT expression. For example, the following query works in MYSQL 5.0.2 and
beyond, but not in earlier versions:
mysql> SELECT SUM(copies_in_stock) sum GROUP BY poet HAVING poet > 'E';
Type of Functional Dependence (FD)
A functional dependency is an association between two attributes of the same relational database table.
One of the attributes is called the determinant and the other attribute is called the determined. For each
value of the determinant there is associated one and only one value of the determined.
If A is the determinant and B is the determined then we say that A functionally determines B and
graphically represent this as A -> B. The symbols A B can also be expressed as B is functionally
33
determined by A.
Example
Since for each value of A there is associated one and only one value of B.
Example
34
Sname
Supplier name
City
Status
- Status of the city e.g. A grade cities may have status 10, B grad cities
may have status 20 and so on.
Here, Sname is FD on Sno. Because, Sname can take only one value for the given value of Sno (e.g. S 1)
or in other words there must be one Sname for supplier number S1.
FD is represented as:
Sno Sname
35
Pno
Qty
In this case Qty is FD on combination of Sno, Pno because each combination of Sno and Pno results only
for one Quantity.
SP (Sno, Pno) --> SP.QTY
Dependency Diagrams
A dependency diagram consists of the attribute names and all functional dependencies in a given table.
The dependency diagram of Supplier table is.
36
Sname
Sname
Sno
Sno
City
Sno
Status
Sname
City
Sname
Status
City
Status
Pno - Pname
Pno - Color
Pno - Wt
37
Here Y is FD on X, but X has two proper subsets Sno and Status; city is FD .on one proper subset .of X
i.e. Sno
Sno City
According to 'FFD definition Y must not be FD .on any proper subset of X, but here City is FD in one
38
Qty
Y
39
dependencies:
Description
A B A key attribute functionally determines a non-key attribute.
A B A non-key attribute functionally determines a non-key attribute.
A B A non-key attribute functionally determines a key attribute.
A partial functional dependency is a functional dependency where the determinant consists of key
attributes, but not the entire primary key, and the determined consist~ of non-key attributes.
A transitive functional dependency is a functional dependency where the determinant consists of
non-key attributes and the determined also consists of non-key attributes.
A Boyce-Codd functional dependency is a functional dependency where the determinant consists
of non-key attributes and the determined consists of key attributes.
A Multi-Value Dependency (MVD) occurs when two or more independent multi valued facts about
the same attribute occur within the same table. It means that if in a relation R having A, Band C as
attributes, B and Care multi-value facts about A, which is represented as A B and A C ,then multi
value dependency exist only if B and C are independent on each other.
A Join Dependency exists if a relation R is equal to the join of the projections X Z. where X, Y, Z
projections of R.
sno Sname
cno came
40
sno address
cno instructor
Instructor office
Let these dependencies be denoted by F. The closure of F, denoted by F +, includes F and all functionaldependencies that are implied by F.
To determine F+, we need rules for deriving all functional dependencies that are implied: by F. A set of
rules that may be used to infer additional dependencies was proposed by Armstrong in 1974. These rules
(or axioms) are a complete set of rules in that all possible functional dependencies may be derived from
them. The rules are:
The reflexivity rule is the simplest (almost trivial) rule. It states that each subset of X is functionally
dependent on X. In other words trivial dependence is defined as follows:
Trivial functional dependency: A trivial functional dependency is a functional dependency of an
attribute on a superset of itself.
For example: {Employee ID, Employee Address} {Employee Address} is trivial, here {Employee
Address} is a subset of {Employee ID, Employee Address}.
41
42
normalizing relations.
Although we could follow the present procedure and compute the closure of F to find all the functional
dependencies, the computation requires exponential time and the list of dependencies is often very large
and therefore not very useful. There are two possible approaches that can be taken to avoid dealing with
the large number of dependencies in the closure. 'One' is to deal with one attribute or a set of attributes
at a time and find its closure (i.e. all functional dependencies relating to them). The aim of this exercise
is to find what attributes depend on a given set of attributes and therefore ought to be together. The
other approach is to find the minimal covers.
In discussing the concept of equivalent FDs, it is useful to define the concept of minimal functional
dependencies or minimal cover which is useful in eliminating necessary functional dependencies so that
only the minimal numbers of dependencies need to be enforced by the system. The concept of minimal
cover of F is sometimes called irreducible Set of F.
A functional depending set S is irreducible if the set has three following properties:
Each right set of a functional dependency of S contains only one attribute.
Each left set of a functional dependency of S is irreducible. It means that reducing anyone attribute from
left set will change the content of S (S will lose some information).
Reducing any functional dependency will change the content of S.
Sets of functional dependencies with these properties are also called canonical or minimal.
Types of Relationships
Before you begin to establish relationships between tables in the database, you must know what types of
relationships can exist between a given pair of tables. Knowing how to identify them properly is an invaluable skill
for designing a database successfully.
There are three specific types of relationships that can exist between a pair of tables: one-to-one, one-to-many, and
many-to-many. The tables participate in only one type of relationship at any given time. (You'll rarely need to
change the type of relationship between a pair of tables. Only major changes in either of the table's structures could
cause you to change the relationship.)
Note
The discussion for each type of relationship begins with a generic example of the relationship. Learning how to
visualize a relationship generically enables you to understand the principle behind the relationship itself. Once you
43
understand how and why the relationship works, you'll be able to determine whether it exists between a given pair of
tables quite easily.
Each discussion also includes an example of how to diagram the relationship. I provide special instructions
pertaining to the diagramming process where appropriate and explain the symbols incorporated within the diagram
as necessary. This allows you to learn the diagramming method at a reasonable pace and keeps you from having to
memorize the entire set of diagram symbols all at once.
Figure 10.2 shows the first symbols you will use to diagram a table relationship.
One-to-One Relationships
A pair of tables bears a one-to-one relationship when a single record in the first table is related to only one record in
the second table, and a single record in the second table is related to only one record in the first table. Figure 10.3
shows a generic example of a one-to-one relationship.
As you can see, a single record in TABLE A is related to only one record in TABLE B, and a single record in
TABLE B is related to only one record in TABLE A. A one-to-one relationship usually (but not always) involves a
subset table. Figure 10.4 shows an example of a typical one-to-one relationship that you might find in a database for
an organization's human resources department. This example also illustrates a situation where neither of the tables is
a subset table.
44
Although the fields in these tables could be combined into a single table, the database designer chose to place the
fields that can be viewed by anyone in the organization in the EMPLOYEES table and the fields that can be viewed
only by authorized personnel in the COMPENSATION table. Only one record is required to store the compensation
data for a given employee, so there is a distinct one-to-one relationship between a record in the EMPLOYEES table
and a record in the COMPENSATION table.
A one-to-one relationship usually (but not always) involves a subset table. (Indeed, neither of the tables in Figure
10.4 is a subset table.) Figure 10.5 shows a generic example of how you create a relationship diagram for a one-toone relationship
dedicated server
A dedicated server is a single computer in a network reserved forserving the needs of the
network. For example, some networks require that one computer be set aside to manage
communications between all the other computers. A dedicated server could also be a
computer that manages printer resources. Note, however, that not all servers are dedicated.
In some networks, it is possible for a computer to act as a server and perform other
functions as well.
In the Web hosting business, a dedicated server is typically a rented service. The user rents the server, software and
an Internet connection from the Web host.
45
The line that appears between the tables in the diagram indicates the type of relationship, and there is a particular
line that you use for each type. Later in this chapter, you'll learn how to modify the line to show the characteristics
of the relationship as well. Figure 10.6 shows the relationship diagram for the EMPLOYEES and
COMPENSATION tables in Figure 10.4. (Note that a Data Table symbol represents each table.)
Figure 10.6. The relationship diagram for the EMPLOYEES and COMPENSATION tables.
One-to-Many Relationships
A one-to-many relationship exists between a pair of tables when a single record in the first table can be related to
one or more records in the second table, but a single record in the second table can be related to only one record in
the first table. Let's look at a generic example of this type of relationship.
Say you're working with two tables, TABLE A and TABLE B, that have a one-to-many relationship between them.
Because of the relationship, a single record in TABLE A can be related to one or more records in TABLE B. Figure
10.7 shows the relationship from the perspective of TABLE A.
Figure 10.7. A one-to-many relationship from the perspective of TABLE A.
Conversely, a single record in the TABLE B can be related to only one record in TABLE A. Figure 10.8 shows the
relationship from the perspective of TABLE B.
46
This is by far the most common relationship that exists between a pair of tables in a database, and it is the easiest to
identify. It is crucial from a data-integrity standpoint because it helps to eliminate duplicate data and to keep
redundant data to an absolute minimum. Figure 10.9 shows a common example of a one-to-many relationship that
you might find in a database for a video rental store.
Figure 10.9. A typical example of a one-to-many relationship.
A customer can check out any number of videos, so a single record in the CUSTOMERS table can be related to one
or more records in the CUSTOMER RENTALS table. A single video, however, is associated with only one customer
at any given time, so a single record in the CUSTOMER RENTALS table is related to only one record in the
CUSTOMERS table.
Figure 10.10 shows a generic example of how you create a relationship diagram for a one-to-many relationship.
Figure 10.10. Diagramming a one-to-many relationship.
Note that the crow's foot symbol is always located next to the table on the "many" side of the relationship. Figure
10.11 shows the relationship diagram for the CUSTOMERS and CUSTOMER RENTALS tables in Figure 10.9.
47
Figure 10.11. The relationship diagram for the CUSTOMERS and CUSTOMER RENTALS tables.
Many-to-Many Relationships
A pair of tables bears a many-to-many relationship when a single record in the first table can be related to one or
more records in the second table and a single record in the second table can be related to one or more records in the
first table.
Assume once again that you're working with TABLE A and TABLE B and that there is a many-to-many relationship
between them. Because of the relationship, a single record in TABLE A can be related to one or more records (but
not necessarily all) in TABLE B. Conversely, a single record in the TABLE B can be related to one or more records
(but not necessarily all) in TABLE A. Figure 10.12 shows the relationship from the perspective of each table.
Figure 10.12. A many-to-many relationship from the perspective of both TABLE A and TABLE
B.
This is the second most common relationship that exists between a pair of tables in a database. It can be a little more
difficult to identify than a one-to-many relationship, so you must be sure to examine the tables carefully. Figure
10.13 shows a typical example of a many-to-many relationship that you might find in a school database, which
happens to be a classic example of this type of relationship (no pun intended!).
48
A student can attend one or more classes during a school year, so a single record in the STUDENTS table can be
related to one or more records in the CLASSES table. Conversely, one or more students will attend a given class, so
a single record in the CLASSES table can be related to one or more records in the STUDENTS table.
Figure 10.14 shows a generic example of how you create a relationship diagram for a many-to-many relationship.
Figure 10.14. Diagramming a many-to-many relationship.
In this case, there is a crow's foot symbol located next to each table. Figure 10.15 shows the relationship diagram for
the STUDENTS and CLASSES tables in Figure 10.13.
49
Figure 10.15. The relationship diagram for the STUDENTS and CLASSES tables.
It will be tedious and somewhat difficult to retrieve information from one of the tables.
There are two common methods that novice and inexperienced developers use in a futile attempt to address this
situation. I'll demonstrate how you might apply these methods using the STUDENTS and CLASSES tables in
Figure 10.16 as examples.
Figure 10.16. Structures of the STUDENTS and CLASSES tables.
Note
As this example unfolds, keep in mind that every many-to-many relationship you encounter will exhibit these same
issues.
50
As you can see, there is no actual connection between the two tables, so you have no way of associating records in
one table with records in the other table. The first method you might use to attempt to establish a connection
involves taking a field from one table and incorporating it a given number of times within the other table. (This
approach usually appeals to people who are accustomed to working with spreadsheets.) For example, you could take
the STUDENT ID field from the STUDENTS table and incorporate it within the CLASSES table structure, creating
as many copies of the field as you need to represent the maximum number of students that could attend any class.
Figure 10.17 shows the revised version of the CLASSES table structure.
Figure 10.17. Incorporating STUDENT ID fields within the CLASSES table structure.
This structure is likely to be problematic, so you might try taking the CLASS ID field from the CLASSES table and
incorporating it within the STUDENTS table structure instead. Figure 10.18 shows the revised version of the
STUDENTS table structure.
Figure 10.18. Incorporating CLASS ID fields within the STUDENTS table structure.
51
Do these structures look (vaguely) familiar? They should. All you've done using this method is introduce a
"flattened" multivalued field into the table structure. In doing so, you've also introduced the problems associated
with a multivalued field. (If necessary, review Chapter 7.) Although you know how to resolve a multivalued field,
this is not a good or proper way to establish the relationship.
The second method you might attempt to use is simply a variation of the first method. In this case, you take one or
more fields from one table and incorporate a single instance of each field within the other table. For example, you
could take the CLASS ID, CLASS NAME, and INSTRUCTOR ID fields from the CLASSES table and incorporate
them into the STUDENTS table in order to identify the classes in which a student is currently enrolled. This may
seem to be a distinct improvement over the first method, but you'll see that there are problems that arise from such
modifications when you load the revised STUDENTS table with sample data.
Figure 10.19 clearly illustrates the problems you'll encounter using this method.
The table contains unnecessary duplicate fields. You learned all about unnecessary duplicate fields and the
problems they pose back in Chapter 7, so you know that using them here is not a good idea. Besides, it is
very likely that the CLASS NAME and INSTRUCTOR ID fields are not appropriate in the STUDENTS
tablethe CLASS ID field identifies the class sufficiently, and it is really all you need to identify the classes a
student is taking.
There is a large amount of redundant data. Even if you remove the CLASS NAME and INSTRUCTOR ID
fields from the STUDENTS table, the CLASS ID field will still produce a lot of redundant data.
It is difficult to insert a new record. If you enter a record in the STUDENTS table for a new class (instead
of entering it in the CLASSES table) without also entering student data, the fields pertaining to the student
will be nullincluding the primary key of the STUDENTS table (STUDENT ID). This will automatically
trigger a violation of the Elements of a Primary Key because the primary key cannot be null; therefore, you
cannot insert the record into the table until you can provide a proper primary key value.
It is difficult to delete a record. This is especially true if the only data about a new class has been recorded
in the particular student record you want to delete. Note the record for Diana Barlet, for example. If Diana
decides not to attend any classes this year and you delete her record, you will lose the data for the
"Introduction to Database Design" class. That might not create a serious problemunless someone neglected
to enter the data about this class into the CLASSES table as well. Once you delete Diana's record, you'll
have to re-enter all of the data for the class in the CLASSES table.
52
Fortunately, you will not have to worry about any of these problems because you're going to learn the proper way to
establish a many-to-many relationship.
Self-Referencing Relationships
This particular type of relationship does not exist between a pair of tables, which is why it isn't mentioned at the
beginning of this section. It is instead a relationship that exists between the records within a table. Ironically, you'll
still regard this throughout the design process as a table relationship.
A table bears a self-referencing relationship (also known as a recursive relationship) to itself when a given record in
the table is related to other records within the table. Similar to its dual-table counterpart, a self-referencing
relationship can be one-to-one, one-to-many, or many-to-many.
One-to-One
A self-referencing one-to-one relationship exists when a given record in the table can be related to only one other
record within the table. The MEMBERS table in Figure 10.20 is an example of a table with this type of relationship.
In this case, a given member can sponsor only one other member within the organization; the SPONSOR ID field
stores the member identification number of the member acting as a sponsor. Note that Susan McLain is Tom
Wickerath's sponsor.
53
One-to-Many
A table bears a self-referencing one-to-many relationship to itself when a given record in the table can be related to
one or more other records within the table. Figure 10.22 shows an example in which a given customer can refer
other customers to the organization. The REFERRED BY field stores the customer identification number of the
customer making the referral. Note that Paul Litwin referred both Andy Baron and Mary Chipman.
Many-to-Many
A self-referencing many-to-many relationship exists when a given record in the table can be related to one or more
other records within the table and one or more records can themselves be related to the given record. This may
sound somewhat confusing at first, but the example in Figure 10.24 should help clarify the matter.
54
In this case, a particular part can comprise several different component parts, and it can itself be a component of
other parts. For example, a clamp assembly (Part ID 704) is composed of a fastening bolt (Part ID 703), a bottom
clamp (Part ID 702), and a top clamp (Part ID 701). Additionally, the clamp assembly is itself a component of a seat
assembly (Part ID 707) and a frame assembly (Part ID 711). Figure 10.25 shows how you diagram this type of
relationship.
Note
Before you begin to work through the examples in the remainder of the chapter, now is a good time to remember a
principle I presented in the introduction:
Focus on the concept or technique and its intended results, not on the example used to illustrate it.
There are, without a doubt, any number of ways in which you can relate the tables in these examples (and in the case
study as well), depending on each table's role within a given database. The manner in which I use the examples here
is not important; what is important are the techniques I use to identify and establish relationships between tables.
Once you learn these techniques, you can identify and establish relationships for any pair of tables within any
context you may encounter.
Now that you've learned about the various types of table relationships, your next task is to identify the relationships
that currently exist among the tables in the database.
55
56