IT Project RYTHAM
IT Project RYTHAM
Session: 2020-21
Class-10
Subject: Information Technology (402)
ANSWERS
We use Insert Command to insert records into a table. The SQL INSERT INTO
declaration is used to include new database rows in the database table.
Syntax:
If you add value for all columns of the table you may not have to specify the
column(s) name in the SQL query. Nonetheless, notice that the order of the
values is identical to that of the table columns. It will be the following SQL
INSERT INTO syntax:
Syntax:
Example:
As per the syntax of INSERT INTO syntax, we specify the name of the table
"INSERT INTO" keyword, followed the order of columns, in which you want the
values that need to be inserted. Post the "VALUES' keyword enter the column
values in order of the columns specified earlier.
1. INSERT INTO student (id, name, age) VALUES (‘1’, ‘Nitin’, ‘Noi
da’, 28);
After firing this query, our table will look like:
1 Nitin Noida 26
1 Nitin Noida 26
2 Amit New Delhi 23
3 Rohit Bareily 27
2. Select Query
Viewing all records from a table. These results tables are called result-sets.
Here, column1, column2...are the fields of a table whose values you want to
fetch. If you want to fetch all the fields available in the field, then you can use the
following
Syntax:
Example:
1. SELECT * FROM student;
The result of this query will be a display of all rows present in the table.
1 Nitin Noida 26
3 Rohit Bareily 27
2 Nitin Noida 26
3 Rohit Bareily 27
Example:
1. SELECT COUNT(1) FROM student;
The output of this query will be:
1 Nitin Noida 26
If we fire:
The number of rows our table has shall be returned. In our query, we may even
use MAX & MIN. For eg, whether a student with a maximum age needs to be
provided with information, we can fire:
For example:
1. SELECT sum(age)FROM student;
It will give 76 as output.
Remember, we can only use numerical columns for MAX, MIN, and SUM
functions. The text column utilizes certain features to trigger an error.
Example:
In our case, the result of this query will look like the following table:
1 Nitin Noida 26
Assume that we want to alter a student's age in our table called 'Rohit.' The
following query will be used:
Example:
Now if we fire:
1. SELECT * FROM student;
We will get the following table as output:
1 Nitin Noida 26
3 Rohit Bareily 27
Please pay attention to UPDATE or DELETE queries using the WHERE clause.
Suppose there is more than one student called 'Rohit' in our 'class' list. The age
of all students called 'Rohit' is modified to 28 in this case. Therefore, the
PRIMARY KEY should always be included in the WHERE clause during the
modification or deletion.
We must always take note of the column data types when we modify the details.
A numeric column can only contain numbers while a text column can contain
text. This implies that if we use an UPDATE clause to place age = 'Rohit' in the
age list, SQL would make an exception. You can find out more about SQL's error
types and exceptions.
6. Viewing records from a table without knowing exact
details
In the real world, there is a strong risk of us not understanding precisely what
column values as we communicate with the database. For instance, because I
am a data operator in a firm, I might know that in our organization there is an
employee called Nitin as I might have heard other managers talking about
him. Now I want to see whole Nitin records but I don't know how he's spelling
his name. Is 'Nitin' OR 'Netin' If it is. In that case, we could use the SQL-supplied
'LIKE' operator.
1 Nitin Noida 26
1 Nitin Noida 26
3 Rohit Bareily 27
4 Shuchi Livknow 24
5 Shuchi Patna 24
4 Shuchi Lucknow 22
5 Shuchi Patna 24
We have therefore noticed that we could not obtain a unique record simply by
using the name-value in the WHERE clause. Here, more than one condition must
be combined in WHERE that can simply be done with conditions such as AND or
OR. For example, if we fire:
1. SELECT * FROM student WHERE name = ‘shuchi’ AND age = 24;
We get the following output:
5 Shuchi Patna 24
To order to further enhance the quest, you may even merge AND & OR
requirements to the WHERE section. For example, if we fire
1. SELECT * FROM student WHERE name = ‘shuchi’ OR age > 23
Output will be:
1 Nitin Noida 26
3 Rohit Bareily 27
4 Shuchi Lucknow 22
5 Shuchi Patna 24
Name
Nitin
Rohit
We can notice that only student names are printed. Here we just have names of
those students whose age is over 25 because of the WHERE clause.
More than one column name may even be included with a comma in the SELECT
statement.
For example:
Name Address
Nitin Noida
Rohit Bareily
Shuchi Lucknow
Shuchi Patna
You can also change the sequence of columns to be displayed on your screen.
For example:
Age Name
26 Nitin
23 Amit
27 Rohit
22 Shuchi
24 Shuchi
.schema student;
EXPLAIN can be used to break the times of the different parts of your query
before a SQL statement. The explanation behind a sluggish query is good for
cataloging.