0% found this document useful (0 votes)
44 views

Class 10 DBMS Queries Notes

english hindi maths science

Uploaded by

drishtibadlia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Class 10 DBMS Queries Notes

english hindi maths science

Uploaded by

drishtibadlia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DBMS QUERIES

NOTE: Always end queries with a semicolon;

SHOW DATABASES; will give you a list of all the databases

---------------------------------------------------------------------------------------------------------------------------------------------------------

CREATE Command

CREATE TABLE <tablename> (


COLUMN_1 DATATYPE (SIZE),
COLUMN_2 DATATYPE (SIZE), …
);

CREATE is a DDL command this will create a table with the specified columns having specified datatypes

Query OK, 0 rows affected This shows that query was successfully executed followed by the time taken to execute it.

-------------------------------------------------------------------------------------------------------------------------------------------------------

DESC <tablename>; or DESCRIBE<tablename>; shows the structure of the table

--------------------------------------------------------------------------------------------------------------------------------------------------------
INSERTING RECORDS IN DATABASE (DML command)

3 ways of inserting records:

1. Insert values in the same order as columns created in table


INSERT INTO <tablename> VALUES (value1, value2, value3, …);

2. Mention all field names and all values to be inserted in the same order as mentioned fields
INSERT INTO <tablename> (field1, field2, fields3, …) VALUES (value1, value2, value3, …);

3. Mention only specified columns in which you must enter values in the same order
INSERT INTO <tablename> (field1, field3, …) VALUES (value1, value2);

----------------------------------------------------------------------------------------------------------------------------------------------

NOTE: If you explicitly want to insert a NULL value, then it can be done in 2 ways:

i) While specifying values, mention ‘NULL’ as one of the values.

ii) Do not mention any value for that field

----------------------------------------------------------------------------------------------------------------------------------------------

CONSTRAINTS

Used to specify rules for data in a table. There are 6 different constraints in SQL:
UNIQUE, DEFAULT, CHECK, NOT NULL, PRIMARY KEY, FOREIGN KEY
Consider the following example:

Creating a primary table, which is the Parent table and a secondary table which is the Student table with constraints.

NOTE: Parent table (Primary table) is created first and then the Student table (secondary table) to ensure Referential
Integrity, Secondary table references the Primary table.
---------------------------------------------------------------------------------------------------------------------------------------------------------

Activity: Let’s create a table emp with 3 fields: id, department and name

Inserting values:
SELECT keyword

1. Select all rows from emp table (* means all fields from table)

2. Select particular rows based on condition (WHERE clause)

3. AND clause

4. OR clause
5. Select particular columns (condition is optional) One example has been done with WHERE clause and
another has been done without it.

6. DISTINCT clause: gives unique distinct values found in the specified field, eliminates redundancy

7. ALL clause: fetches all values found in the field. Redundancy observed.

8. Performing simple calculations


9. Current system date curdate()

10. Column aliases (AS clause)

11. Conditions based on a Range (BETWEEN clause)

12. Conditions based on a List (IN clause, NOT IN clause)

IN Clause

NOT IN Clause
13. Searching for NULL (IS NULL, IS NOT NULL)

Searching for NULL value

Searching for a NOT NULL value

14. Pattern matching (LIKE clause): % for substring, _ (underscore) for single character

Where name ends with letter ‘a’

Where name has second letter ‘e’

15. PRIMARY KEY USING CREATE TABLE


Method-1: PRIMARY KEY (fieldname)

Method-2: Mention as a constraint


NOTE: For creating a Composite Primary Key,
PRIMARY KEY (fieldname1, fieldname2, …);

For Composite Key, mention names of


all fields here

----------------------------------------------------------------------------------------------------------------------------------------------------------

16. PRIMARY KEY USING ALTER TABLE


ALTER TABLE <tablename> ADD PRIMARY KEY (fieldname);

17. FOREIGN KEY USING CREATE TABLE


FOREIGN KEY <fieldname> REFERENCES <name_of_primary_table> (field of primary table)

18. FOREIGN KEY USING ALTER TABLE


ALTER TABLE <tablename> ADD FOREIGN KEY <fieldname> REFERENCES <name_of_primary_table> (field
of primary table)

19. ALTERING TABLE STRUCTURE DDL Command


i) Add new column: ALTER TABLE <tablename> ADD <new column name> <datatype> (size);

ii) Modify datatype: ALTER TABLE <tablename> MODIFY <fieldname> <new datatype> (new size);

iii) Rename column: ALTER TABLE <tablename>


CHANGE <old column name> <new column name> <datatype>(size);

iv) Drop column: ALTER TABLE <tablename> DROP <fieldname>;


UPDATE command

DML command
Specifies the rows to be changed using the WHERE clause
UPDATE <tablename>
SET <column1>=<new value1>, <column2>=<new value2>
WHERE <condition>

DELETE command

DML command
Removes rows from table. If WHERE clause is not specified, will clear all rows (empty table will be left) If
WHERE clause is specified, then specific rows will be created
DELETE FROM <tablename> WHERE <condition>

Let us understand these 2 commands using an example. Recall the ‘emp’ table.

Update name and dept of employee where name of employee is NULL

Update the department of employee with id 105

Here is the final table after updating:


Suppose the Sales department is no longer needed, then those employees who have ‘Sales’ as their department will
also be deleted from this table.

Delete employees having department as Sales:

WHERE clause mentioned

This is what the emp table looks like now:

NOTE: In the above query WHERE clause has been mentioned. If WHERE clause is not mentioned in DELETE, then all
rows from the table will be removed

WHERE clause NOT mentioned

-------------------------------------------------------------------------------------------------------------------------------------------------------

DROP Command

Tables can be dropped in 2 ways:

i) DROP TABLE <tablename>; will drop the specified table from the database
ii) DROP TABLE IF EXISTS <tablename>; will drop the specified table from the database only if it exists in
the database

DROP is a DDL command

IMPORTANT TERMS:

DEGREE: refers to the no. of attributes or columns in a table

CARDINALITY: refers to the no. of rows or tuples

You might also like