SQL_commands2
SQL_commands2
SQL_commands2
SQL Commands
Constraints
Truncate Delete
rename
• In order to make schema, Table, Mention attributes that’s we do with the help of DDL
• But in order to insert values in that table we use DML
• In DCL we discuss about the access to users of our database
• In TCL we use for transactions like we use online payment (ACID properties work here like if
transaction is complete or in between the process or got failed)
• Constraints: Like when we mention attributes there, we mention constraints (like Keys, Check
constraints, Default, Unique, Default, Null)
Data Types:
In SQL, data types define the type of data that can be stored in a column of a table. Each database
system might have its own set of data types, but here's a list of common data types along with
examples:
1. INT (Integer):
4. DATE:
5. TIME:
7. FLOAT or DOUBLE:
8. BOOLEAN or BIT:
Used for creating a set of predefined values that a column can take.
Connection :
show user;
conn c##user3/user3 ;
show user
DDL:
1. Create Command:
• In Table Name – we can’t use . or – or stu (space)table this should not be happening
• Table name should be single value
• Data types: Integer, varchar (4 byte size),
• After the last attribute don’t use comma(,) and after closing the bracket put ;(semi-column)
*In workbench its graphical user interface and CMD is character ser interface
Practice:
IN ORACLE:
desc student;
Alter Command:
• alter table studentdetails add FatherName varchar(20) ;
• desc student1;
•
Other ways to use ALTER command
SQL> ALTER TABLE studentdetails ADD date_of_birth DATE;
Rename Command:
• Rename studentdetails to Studentdata;
• alter table student2 rename column Name to StudentName; (to change the column name in
a table)
• show tables;
Drop Command:
• drop table student1;
• show tables;
Truncate command:
This command will remove all rows from the studentdetails table, but the table structure will
remain intact. It's important to note that TRUNCATE cannot be used on tables that are
referenced by foreign key constraints unless you disable or remove the constraints temporarily.
• Truncate table studentdata;
DML:
Now we already have tables let’s insert values into cells of this table;
1. Insert Command:
insert into studentdetails values (101, 'Sanjay', 'Rajender Singh', 2020, 'B.Tech', 500,
469);
Select * from studentdetails;
2. Now in case of RollNumber is the primary key so it will not accept duplicate entry.
4. Now if you don’t mention any value for registrationNumber again it will show the error.
insert into studentdata values(103, 'joy', , 'B.Tech', 100, 74, 'pradipta', 'vanshika') ;
5. But if we will mention NULL it will accept it.
insert into studentdata values(103, 'joy',null , 'B.Tech', 100, 74, 'pradipta', 'vanshika') ;
6. If we want to add values for two attributes instead of adding for all
Here TotalMarks are not null because we already set its default value as 100
7. We used check(MarksObtained<=TotalMarks)
insert into studentdata values(105, 'kriti' ,127, 'MBA' , 100, 101, 'jagnish prasad' , 'mamta
');
Lets use correct command-
insert into studentdata values(105, 'kriti' ,127, 'MBA' , 100, 98, 'jagnish prasad' , 'mamta '
);
Update Command:
Delete Command:
If want to delete any record from the table
delete from studentdata where rollnumber=104;
select * from studentdata;
JOINS:
SQL joins allow you to combine rows from two or more tables based on a related
column between them. Here are some common SQL join types in MySQL with
examples:
1. INNER JOIN:
The INNER JOIN retrieves records that have matching values in both tables.
• INNER JOIN:
To retrieve a list of employees along with their department names (only
employees who belong to a department):
LEFT JOIN:
In database management systems (DBMS), a LEFT JOIN, also known as a LEFT
OUTER JOIN, is a type of SQL join operation that combines rows from two or
more tables based on a related column, and it returns all rows from the left
table (table1) and matching rows from the right table (table2). If there is no
match found in the right table, NULL values are returned for columns from
the right table.
Or
Inner Join
select employees.Name, departments.Dept_Name from employees inner join
departments on employees.Dept_ID = departments.Dept_ID;
Left Join:
select employees.Name, departments.Dept_Name from employees left join
departments on employees.Dept_ID = departments.Dept_ID;
OR
Right Join:
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no
match.
Now the tables look like this:
OR
Full Join :
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or
right (table2) table records.
| Emp_ID | Name | Dept_ID |
+--------+---------+---------+
| 1 | Prakash | 101 |
| 2 | Pritosh | 102 |
| 3 | Rimple | 103 |
| 4 | Ridhima | 104 |
| 5 | Priya | 101 |
| 6 | Ridha | 101 |
| 7 | Seema | 102 |
| 8 | ritika | NULL |
| 9 | manya | NULL |
Table2:
select * from departments;
+---------+-----------------+
| Dept_ID | Dept_Name |
+---------+-----------------+
| 101 | HR |
| 102 | Sales |
| 103 | marketing |
| 104 | Production |
| 105 | Accounts |
| 106 | Design Thinking |
+---------+-----------------+
• Now let’s say this is new table and for Emp_ID there is a manager with ID = 7
• If we check whole table Seema is the manager of Prakash with Emp_ID= 1
• Now if for better clarity I want to see all details of Seema as a manager of
Prakash in a single tuple then we do that by self join
Here if we want to just see employee name and his/her manager name then we will use the
upper mentioned command.
UNION:
Now we have two tables employees and employees2 having an entry common in between
If we use union to join these two tables it will not include duplicate entries
• select employees.Emp_ID, employees.Name from employees union select
employees2.Emp_ID, employees2.Name from employees2;
• Here we can see earlier if we count the total entries in employees and employees2
table it was 11
• But when we use union to combine these tables it didn’t show the duplicate entry and
total count of entries is 10
UNION ALL
Cross Join:-
• A cross join, also known as a Cartesian join or a cross product, is a type of join operation
in relational database management systems (RDBMS). It combines every row from one
table with every row from another table, resulting in a Cartesian product of the two
tables. Unlike other join types like inner joins or outer joins, cross joins do not require
a specific condition or criteria for matching rows; they simply combine all possible
combinations.
• It simply multiplies each row of table 1 with each row of table 2
• Example :