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

dbms_practical_adivya_08 (1)

Uploaded by

karya787384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

dbms_practical_adivya_08 (1)

Uploaded by

karya787384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DBMS

Submitted in partial fulfilment of the requirements for the degree of BCA

SUBMITTED BY

ADIVYA RAJ SHARMA

08

Mrs. DEEPIKA
Department of BCA
SQL (STRUCTURED QUERY LANGUAGE)

DDL (Data Definition Language): DDL is a subset of SQL used to define


and modify the structure of a database.

• Create
• Renane
• Alter
• Drop
• Truncate

Create Command: We use this command to create Database and table.

Create Database

Syntax: Create database <database name>


Example: Create database college;

NOTE : We have to use college database before creating table in it.

Use Database

Syntax: Use <database name>


Example: Use college;

Create Table

Syntax: Create table <table name> ( Col1 datatype constraint, Col2


datatype constraint, Col3 datatype constraint);
Example: Create table students (
Roll int(20) ,
Name varchar(20),
Courses varchar(20),
);

Rename Command: The RENAME command in SQL is used to change the


name of a database object, such as a table.

Renaming a Table
Syntax: RENAME TABLE <old_table_name> TO <new_table_name>
Example: RENAME TABLE students TO scholar;

Alter Command: The ALTER command in SQL is used to modify the


structure of an existing table. It can be used to add, delete, or modify
columns or even to change the table’s structure in other ways.

Adding a New Column


Syntax: ALTER TABLE <table_name> ADD <column_name> datatype;
Example: ALTER TABLE scholar ADD gender varchar(10);
Modifying an Existing Column

Syntax: ALTER TABLE <table_name> MODIFY <column_name>


new_datatype;
Example: ALTER TABLE scholar MODIFY gender varchar(5);

Dropping a Column

Syntax: ALTER TABLE <table_name> DROP COLUMN


<column_name>;
Example: ALTER TABLE scholar DROP COLUMN gender;

Renaming a Column

Syntax: ALTER TABLE <table_name> RENAME COLUMN


<old_column_name> TO <new_column_name>;
Example: ALTER TABLE scholar RENAME COLUMN rollno TO stu_id;

Renaming a Table

Syntax: ALTER TABLE <old_table_name> RENAME TO


<new_table_name>;
Example: ALTER TABLE scholar RENAME TO student;
Add Keys Constraints

Syntax: ALTER TABLE <table_name> ADD PRIMARY KEY


(column_name);
Example: ALTER TABLE student ADD PRIMARY KEY (stu_id);

Drop Command: The DROP command in SQL is used to delete object


like database, tables, columns or key constraints.

Dropping a column from a Table

Syntax: ALTER TABLE <table_name> DROP COLUMN


<column_name>;
Example: ALTER TABLE scholar DROP COLUMN gender;

Dropping a Table

Syntax: DROP TABLE <table_name>;


Example: DROP TABLE student;

Dropping a key Constraints

Syntax: ALTER TABLE <table_name> DROP PRIMARY KEY;


Example: ALTER TABLE student DROP PRIMARY KEY;
Dropping a Database

Syntax: DROP DATABASE <database_name>;


Example: DROP DATABASE college;

Truncate Command: This is used to delete table’s data.

Syntax: TRUNCATE TABLE <table_name>;


Example: TRUNCATE TABLE student;

DML (DATA MANIPULATION LANGUAGE)

• Insert
• Update
• Delete

1. Insert Command: The INSERT command in SQL is used to insert


rows into a table. You can insert data into all columns of the table or
just specific column.

Inserting data into all columns

Syntax: INSERT INTO <table_name> VALUES (val1, val2, …);


Example: INSERT INTO student VALUES (1, “Aditya”, “BCA”);

Inserting data into specific columns

Syntax: INSERT INTO student (col1, col2) VALUES (val1, val2);


Example: INSERT INTO student (rollno, name) VALUES (4, “Aditya”);

2. Update Command: The UPDATE command in SQL is used to


modify existing data in a table. It allows you to change the values of
one or more rows based on a condition.

Updating a single column


Syntax: UPDATE <table_name> SET col. = Val. WHERE col. = Val.;
Example: UPDATE student SET course = “Dbms” WHERE rollno = 1;

Updating Multiple Column

Syntax: UPDATE <table_name> SET col1 = val1, col2 = val2 WHERE


col = Val;
Example: UPDATE student SET course = “IT”, Name = “Mayank”
WHERE rollno = 1;

3. Delete Command: The DELETE command in SQL is used to remove


rows from a table. You can delete specific rows based on a condition
or all rows from a table.

Deleting Specific Rows


Syntax: DELETE FROM <table_name> WHERE col = Val;
Example: DELETE FROM student WHERE rollno = 5;
Delete All Rows

Syntax: DELETE FROM <table_name>;


Example: DELETE FROM student;

DQL (DATA QUERY LANGUAGE)

1. Select Command: The SELECT command in SQL is used to retrieve


data from a database. It allows you to query specific columns, filter
data, join tables, aggregate results and much more.

Selecting All Column

Syntax: SELECT * FROM <table_name>;


Example: SELECT * FROM student;

Selecting specific Column

Syntax: SELECT col1, col2 FROM <table_name>;


Example: SELECT rollno, name FROM student;

Using WHERE to filter results

Syntax: SELECT col1, col2, FROM <table_name> WHERE condition;


Example: SELECT name, course FROM student WHERE rollno = 1;
Using DISTINCT to avoid duplicates

Syntax: SELECT DISTINCT <col_name> FROM <table_name>;


Example: SELECT DISTINCT course FROM student;

Using ORDER BY to short results

Syntax: SELECT col1, col2 FROM <table_name> ORDER BY


<col_name> [ASC | DESC];
Example: SELECT name FROM student ORDER BY rollno DESC;

Using LIMIT to restrict the number of rows

Syntax: SELECT col1, col2 FROM <table_name> LIMIT number;


Example: SELECT rollno, name FROM student LIMIT 2;

Using GROUP BY for aggregation

Syntax: SELECT col1, COUNT(*) FROM <table_name> GROUP BY


col1;
Example: SELECT course, COUNT(*) FROM student GROUP BY
course;

Using HAVING to filter grouped results

Syntax: SELECT col1, COUNT(*) FROM <table_name> GROUP BY


col1 HAVING COUNT(*) > number;
Example: SELECT course, COUNT(*) FROM student GROUP BY
course HAVING COUNT(*) < 1;
Using BETWEEN for range filtering

Syntax: SELECT col1, col2 FROM <table_name> WHERE column


BETWEEN val1 AND val2;
Example: SELECT rollno, name FROM student WHERE rollno
BETWEEN 2 AND 3;

Using IS NULL to check for null values

Syntax: SELECT col1, col2 FROM <table_name> WHERE col IS


NULL;
Example: SELECT rollno, name FROM student WHERE name IS
NULL;

AGGREGATE FUNCTION
• Count
• Sum
• Avg
• Min
• Max

Count: This function returns the number of rows.

Syntax: SELECT COUNT(*) FROM col;


Example: SELECT COUNT(*) FROM student;
Sum: This function returns the sum of a numeric column.

Syntax: SELECT SUM(col) FROM <table_name>;


Example: SELECT SUM(salary) FROM emp;

Avg: This function returns the average value of a numeric column.

Syntax: SELECT AVG(col) FROM <table_name>;


Example: SELECT AVG(salary) FROM emp;

Min: This function returns the smallest value in column.

Syntax: SELECT MIN(col) FROM <table_name>;


Example: SELECT MIN(salary) FROM emp;
Max: This function returns the largest value in column.

Syntax: SELECT MAX(col) FROM <table_name>;


Example: SELECT MAX(salary) FROM emp;

KEYS CONSTRAINTS
• PRIMARY KEY
• FOREIGN KEY

Primary key: A Primary Key in SQL is a field (or a combination of


fields) that uniquely identifies each record in a table. It ensures that
no duplicate or NULL values are allowed for the column(s) defined
as the primary key.

Key Characteristics of a Primary Key:

Uniqueness: Every value in the primary key column must be


unique.

Non-NULL: A primary key cannot contain NULL values.


One Per Table: Each table can have only one primary key, though it
can consist of one or more columns (composite key).

Syntax: CREATE TABLE employees


(Col_1 Datatype PRIMARY KEY,
Col_2 Datatype,
Col_3 Datatype);

Example: CREATE TABLE employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
department VARCHAR(50));

Foreign key: A Foreign Key in SQL is a field (or collection of fields)


in one table that refers to the Primary Key in another table. It is
used to establish a relationship between two tables, ensuring
referential integrity by ensuring that the value of the foreign key
matches a valid value in the referenced table.

Key Characteristics of a Foreign Key:

A foreign key can accept NULL values unless the column is


explicitly defined as NOT NULL.
It enforces referential integrity by linking two tables.
The foreign key ensures that you cannot insert a value into the
foreign key column that does not exist in the primary key column of
the referenced table.

Syntax: Step 1: Create The Referenced Table


CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR (100));

Step 2: Create The Referencing Table


CREATE TABLE employees
(emp_id INT PRIMARY KEY,
emp_name VARCHAR (100),
department_id INT,

FOREIGN KEY (department_id) REFERENCES


departments(department_id));

SOME OTHER OPERATORS Like In As

Like Operator: In SQL, the LIKE operator is used to filter data based
on a pattern. It's commonly used with the WHERE clause to find
values in a column that match a specific pattern.

There are two wildcard characters you can use:

% : Represents any sequence of characters (zero or more).


_ : Represents a single character.

Syntax: Using %: Select * from student where name like %V;


Using _: Select * from student where name like _r%;
Example:
Example:

In Operator: In SQL, the IN operator is used to filter data by checking


if a column’s value matches any value in a specified list. This is
especially useful for filtering based on multiple specific values
without writing multiple OR conditions.

Syntax: Select * from <table name> where <col name> In (value1,


value2….);
Example: Select * from student where city in (“Delhi”,” up”);

As Operator: In SQL, the AS operator is used to rename a column or


table with an alias temporarily, making it easier to work with complex
queries and results. Aliases improve readability and simplify the
output, especially when dealing with calculated fields or joined
tables.

Syntax: Select <col. Name> As <alias name> from <table name>


Example: Select Name as student_name from student;

STRING FUNCTIONS
• Length
• Upper
• Lower
• Ltrim
• Rtrim
• Lpad
• Rpad
• Instr
• Reverse

Length: It displays the length of the string.


Syntax: Select length(col_name) from <table name>;
Example: Select Length(name) from Student;

Upper: It will convert the string into the capital letter.

Syntax: Select upper(col_name) from <table name>;


Example: Select Upper(name) from student;

Lower: It will convert the string into lower case letter.

Syntax: Select lower(col_name) from <table name>;


Example: Select lower(name) from student;

Ltrim: Used to trim the white space in string from left side.

Syntax: Select Ltrim(col_name) from <table name>;


Example: Select ltrim(name) from student;

Rtrim: Used to trim the white space in string from right side.

Syntax: Select Rtrim(col_name) from <table name>;


Example: Select Rtrim(name) from student;
Lpad: Used to make given string of the given size by adding the given
character from left.

Syntax: Lpad (string, length, pad_string);


Example: Select Lpad ('123', 5, '0') AS PaddedNumber;

Rpad: Used to make given string of the given size by adding the given
character from right.

Syntax: Rpad (string, length, pad_string)


Example: Select Rpad ('123', 5, '0') AS PaddedNumber;

Instr: In SQL, the INSTR function is used to find the position of a


substring within a string. It returns the position of the first
occurrence of the specified substring. If the substring isn’t found, it
returns 0.

Syntax: INSTR (string, substring);


Example: Select INSTR ('Hello World', 'World') AS Position;
Reverse: It will reverse the order of the string.

Syntax: Select reverse(col_name) from <table name>;


Example: Select reverse(name) from student;
20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy