Computer Troubleshooting & Maintenance Viva
Computer Troubleshooting & Maintenance Viva
EXPERIMENT NO:-1
4. THEORY :- Tables are the basic structure where data is stored in the database.
Given that in most cases, there is no way for the database vendor to know
ahead of time what your data storage needs are, chances are that you will need
to create tables in the database yourself. Many database tools allow you to
create tables without writing SQL, but given that tables are the container of all
the data, it is important to include the CREATE TABLE syntax.
4. THEORY :- SQL, there are essentially basically two ways to INSERT data into
a table: One is to insert it one row at a time, the other is to insert multiple rows
at a time. Let's take a look at each of them individually:
The syntax for inserting data into a table one row at a time is as follows:
Table Store_Information
and now we wish to insert one additional row into the table representing the sales data for
Los Angeles on January 10, 1999. On that day, this store had $900 in sales. We will
hence use the following SQL script:
7. OBSERVATIONS :- This command to create table will only make the structure
of the table.
4. THEORY :- Once a table is created in the database, there are many occasions
where one may wish to change the structure of the table. In general, the SQL
syntax for ALTER TABLE is
Let's look at the example. Assuming our starting point is the "customer" table created in
the CREATE TABLE section:
Table customer
First_Name char(50)
Last_Name char(50)
Address char(50)
Country char(25)
Birth_Date date
5. CODING (IF REQUIRED) :- Our goal is to add a column called "Gender". To do this,
we key in:
MySQL:
ALTER TABLE customer ADD Gender char(1);
Oracle:
ALTER TABLE customer ADD Gender char(1);
SQL Server:
ALTER TABLE customer ADD Gender char(1);
6. PROGRAM INPUTS & OUTPUT :-Input to this code is written in coding statement .
Resulting table structure:
Table customer
Note that the new column Gender becomes the last column in the customer table
4. THEORY :- Once there's data in the table, we might find that there is a need to
modify the data. To do so, we can use the UPDATE command. The syntax for
this is
UPDATE "table_name"
SET "column_1" = [new value]
WHERE {condition}
Table Store_Information
and we notice that the sales for Los Angeles on 01/08/1999 is actually $500 instead of
$300, and that particular entry needs to be updated. To do so, we use the following SQL
query:
Table Store_Information
Table Store_Information
And we decide not to keep any information on Los Angeles in this table. To accomplish
this, we type the following SQL:
6. PROGRAM INPUTS & OUTPUT :-Input to this code is written in coding statement .
Now the content of table would look like,
Table Store_Information
Let's use a simple example to illustrate. Say we have the following table:
and we want to create a view called V_Customer that contains only the First_Name,
Last_Name, and Country columns from this table, we would type in
6. PROGRAM INPUTS & OUTPUT :-Input to this code is written in coding statement .
We can also use a view to apply joins to two tables. In this case, users only see one view
rather than two tables, and the SQL statement users need to issue becomes much simpler.
Let's say we have the following two tables:
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
and we want to build a view that has sales by region information. We would issue the
following SQL statement:
This gives us a view, V_REGION_SALES, that has been defined to store sales by region
records. If we want to find out the content of this view, we type in,
Result:
REGION SALES
East $700
West $2050
4. THEORY :- Since we have started dealing with numbers, the next natural
question to ask is if it is possible to do math on those numbers, such as summing
them up or taking their average. The answer is yes! SQL has several arithematic
functions, and they are:
For example, if we want to get the average of all sales from the following table,
Table Store_Information
AVG(Sales)
$687.5
$687.5 represents the average of all Sales entries: ($1500 + $250 + $300 + $700) / 4.
SELECT COUNT("column_name")
FROM "table_name"
For example, if we want to find the number of store entries in our table,
Table Store_Information
we'd key in
Result:
Count(store_name)
4
COUNT and DISTINCT can be used together in a statement to fetch the number of
distinct entries in a table. For example, if we want to find out the number of distinct
stores, we'd type,
Result:
3. MAX FUNCTION - SQL uses the MAX function to find the maximum value in a
column. The syntax for using the MAX function is,
For example, if we want to get the highest sales from the following table,
Table Store_Information
we would type in
SELECT MAX(Sales) FROM Store_Information
Result:
MAX(Sales)
$1500
$1500 represents the maximum value of all Sales entries: $1500, $250, $300, and $700.
4. MIN FUNCTION -SQL uses the MIN function to find the maximum value in a
column. The syntax for using the MIN function is,
For example, if we want to get the lowest sales from the following table,
Table Store_Information
we would type in
SELECT MIN(Sales) FROM Store_Information
Result:
MIN(Sales)
$250
$250 represents the minimum value of all Sales entries: $1500, $250, $300, and $700.
5. SUM FUNCTION -The SUM function is used to calculate the total for a column. The
syntax is,
For example, if we want to get the sum of all sales from the following table,
Table Store_Information
we would type in
Result:
SUM(Sales)
$2750
$2750 represents the sum of all Sales entries: $1500 + $250 + $300 + $700.
4. THEORY :- You can place constraints to limit the type of data that can go into a
table. Such constraints can be specified when the table when the table is first
created via the CREATE TABLE statement, or after the table is already
created via the ALTER TABLE statement.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is
specified.
UNIQUE Constraint: Ensures that all values in a column are different.
CHECK Constraint: Makes sure that all values in a column satisfy certain
criteria.
Primary Key Constraint: Used to uniquely identify a row in the table.
Foreign Key Constraint: Used to ensure referential integrity of the data.
By default, a column can hold NULL. If you not want to allow NULL value in a column,
you will want to place a constraint on this column specifying that NULL is now not an
allowable value.
CREATE TABLE Customer (SID integer NOT NULL, Last_Name varchar (30)
NOT NULL, First_Name varchar(30));
Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can
include NULL.
will result in an error because this will lead to column "SID" being NULL, which violates
the NOT NULL constraint on that column.
Even though we didn't specify a value for the "Score" column in the INSERT INTO
statement, it does get assigned the default value of 80 since we had already set 80 as the
default value for this column.
3. UNIQUE Constraint-The UNIQUE constraint ensures that all values in a column are
distinct.
column "SID" has a unique constraint, and hence cannot include duplicate values. Such
constraint does not hold for columns "Last_Name" and "First_Name". So, if the table
already contains the following rows:
Please note that a column that is specified as a primary key must also be unique. At the
same time, a column that's unique may or may not be a primary key. In addition, multiple
UNIQUE constraints can be defined on a table.
CREATE TABLE Customer (SID integer CHECK (SID > 0), Last_Name varchar
(30), First_Name varchar(30));
Column "SID" has a constraint -- its value must only include integers greater than 0. So,
attempting to execute the following statement,
will result in an error because the values for SID must be greater than 0.
Please note that the CHECK constraint does not get enforced by MySQL at this time.
A primary key is used to uniquely identify each row in a table. It can either be part of the
actual record itself , or it can be an artificial field (one that has nothing to do with the
actual record). A primary key can consist of one or more fields on a table. When multiple
fields are used as a primary key, they are called a composite key.
5. PRIMARY KEY Constraint -Primary keys can be specified either when the table is
created (using CREATE TABLE) or by changing the existing table structure (using
ALTER TABLE).
Below are examples for specifying a primary key when creating a table:
MySQL:
CREATE TABLE Customer (SID integer, Last_Name varchar(30), First_Name
varchar(30),
PRIMARY KEY (SID));
SQL Server:
CREATE TABLE Customer (SID integer PRIMARY KEY, Last_Name
varchar(30), First_Name varchar(30));
MySQL:
ALTER TABLE Customer ADD PRIMARY KEY (SID);
Oracle:
ALTER TABLE Customer ADD PRIMARY KEY (SID);
SQL Server:
ALTER TABLE Customer ADD PRIMARY KEY (SID);
Note: Before using the ALTER TABLE command to add a primary key, you'll need to
make sure that the field is defined as 'NOT NULL' -- in other words, NULL cannot be an
accepted value for that field.
6. FOREIGN KEY Constraint :-A foreign key is a field (or fields) that points to the
primary key of another table. The purpose of the foreign key is to ensure referential
integrity of the data. In other words, only values that are supposed to appear in the
database are permitted.
For example, say we have two tables, a CUSTOMER table that includes all customer
data, and an ORDERS table that includes all customer orders. The constraint here is that
all orders must be associated with a customer that is already in the CUSTOMER table. In
this case, we will place a foreign key on the ORDERS table and have it relate to the
primary key of the CUSTOMER table. This way, we can ensure that all orders in the
ORDERS table are related to a customer in the CUSTOMER table. In other words, the
ORDERS table cannot contain information on a customer that is not in the CUSTOMER
table.
Table ORDERS
In the above example, the Customer_SID column in the ORDERS table is a foreign key
pointing to the SID column in the CUSTOMER table.
Below we show examples of how to specify the foreign key when creating the ORDERS
table:
MySQL:
CREATE TABLE ORDERS (Order_ID integer, Order_Date date, Customer_SID
integer, Amount double, Primary Key (Order_ID), Foreign Key (Customer_SID)
references CUSTOMER(SID));
Oracle:
CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date date,
Customer_SID integer references CUSTOMER(SID), Amount double);
SQL Server:
CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date
datetime, Customer_SID integer references CUSTOMER(SID), Amount double);
Below are examples for specifying a foreign key by altering a table. This assumes that the
ORDERS table has been created, and the foreign key has not yet been put in:
MySQL:
ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES
CUSTOMER(SID);
SQL Server:
ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES
CUSTOMER(SID);
7. OBSERVATIONS :- This command are used to limit any table so that the
wrong data canot be inserted.
2.TRUNCATE TABLE STATEMENT -Sometimes we wish to get rid of all the data in
a table. One way of doing this is with DROP TABLE, which we saw in the last section.
But what if we wish to simply get rid of the data but not the table itself? For this, we can
use the TRUNCATE TABLE command. The syntax for TRUNCATE TABLE is
So, if we wanted to drop the table called customer that we created in the CREATE
TABLE section, we simply type
So, if we wanted to truncate the table called customer that we created in SQL CREATE
TABLE, we simply type,
The most frequent uses are as follows (we will use SUBSTR() here):
SUBSTR(str,pos): Select all characters from <str> starting with position <pos>. Note
that this syntax is not supported in SQL Server.
SUBSTR(str,pos,len): Starting with the <pos>th character in string <str> and select the
next <len> characters.
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego