DB Queries
DB Queries
Sure, here are some example queries using various database constraints:
6. **Dropping a Constraint**:
```sql
ALTER TABLE Employees
DROP CONSTRAINT FK_DepartmentID;
```
7. **Enabling/Disabling Constraints**:
```sql
ALTER TABLE Orders
DISABLE CONSTRAINT FK_ProductID;
ALTER TABLE Orders
ENABLE CONSTRAINT FK_ProductID;
```
Joins in DB:
Joins are used to combine rows from two or more tables based on a related column between them.
Common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Here's an
example of an INNER JOIN:
ADD CONSTRAINT -- Creates a new constraint on an existing table, which is used to specify rules
for any data in the table.
ALTER TABLE -- Adds, deletes or edits columns in a table. It can also be used to add and delete
constraints in a table, as per the above.
ALL -- Returns true if all of the subquery values meet the passed condition.
ANY -- Returns true if any of the subquery values meet the given condition.
AS -- Renames a table or column with an alias value which only exists for the duration of the query.
CHECK -- Adds a constraint that limits the value which can be added to a column.
EXISTS -- Checks for the existence of any record within the subquery, returning true if one or more
records are returned.
IS NOT NULL -- The reverse of NULL. Tests for values that aren’t empty / NULL.
ROWNUM -- Returns results where the row number meets the passed condition.
SELECT -- Used to select data from a database, which is then returned in a results set.
SELECT INTO -- Copies data from one table and inserts it into another.
SELECT TOP -- Allows you to return a set number of records to return from a table.
TOP -- Used alongside SELECT to return a set number of records from a table.
TRUNCATE TABLE -- Similar to DROP, but instead of deleting the table and its data, this deletes
only the data.
UNION -- Combines the results from 2 or more SELECT statements and returns only distinct values.
VALUES -- Used alongside the INSERT INTO keyword to add new values to a table.
WHERE -- Filters results to only include data which meets the given condition.
Table :- EmpProject
Schema:-
create table empproject(empid int foreign key references employee(empid),
projectid varchar(50) foreign key references project(projectid), clientid
varchar(50) foreign key references clienttable(clientid),startyear int, endyear
int)
Table :- EmpDept
Schema:-
create table empdept(deptid varchar(50) primary key,deptname varchar(100),
dept_off varchar(100), depthead int foreign key references employee(empid))
Simple Queries
1. Select the detail of the employee whose name
start with P.
or
select * from employee where department in(select deptid from empdept where
deptname='development')