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

Session 2 - SQL Commands

The document discusses various SQL commands for constraints, including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints. It also covers adding, modifying, and dropping constraints. Additional topics covered include aliases for tables and columns, testing for NULL values, and using GROUP BY.

Uploaded by

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

Session 2 - SQL Commands

The document discusses various SQL commands for constraints, including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints. It also covers adding, modifying, and dropping constraints. Additional topics covered include aliases for tables and columns, testing for NULL values, and using GROUP BY.

Uploaded by

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

Session 2 - SQL Commands

Constraints:
 NOT NULL Constraint (for existing ‘employee’ table)
ALTER TABLE employee
MODIFY SALARY DECIMAL (18, 2) NOT NULL;
INSERT INTO employee (ID, NAME, `Location`) VALUES (019, 'Eddie','Brooklyn')
(salary = 0)

 Default Constraint (for existing ‘employee’ table)


ALTER TABLE employee
MODIFY SALARY DECIMAL (18, 2) DEFAULT 5000.00;

DELETE FROM employee WHERE ID = 019


INSERT INTO employee (ID, NAME, `Location`) VALUES (019, 'Eddie','Brooklyn')
(#SALARY IS SET TO 5000)

 Drop Default Constraint


ALTER TABLE employee
ALTER COLUMN SALARY , DROP DEFAULT;

INSERT INTO employee (ID, NAME, `Location`) VALUES (020, 'Adam','UK') (#SALARY IS SET TO
NULL)

To update the empty cells:

UPDATE employee
SET
AGE = 37
WHERE ID = 019

UPDATE employee
SET AGE = 37 , SALARY = 7250
WHERE ID = 20

 Unique Constraint
ALTER TABLE employee
MODIFY AGE INT NOT NULL UNIQUE;

INSERT INTO employee (ID, NAME, `Location`, AGE) VALUES (021, 'Eve','USA', 35) (error
message Duplicate entry '35' for key 'AGE')
 Name a Unique constraint in multiple columns
ALTER TABLE employee
ADD CONSTRAINT myUniqueConstraint UNIQUE(AGE, SALARY);

INSERT INTO employee (ID, NAME, CITY, SALARY) VALUES (140, 'Mina','Alex', 2000)
(#error)

 Drop Unique Constraint


ALTER TABLE employee DROP INDEX myUniqueConstraint;

 Primary Key Constraint


ALTER TABLE employee
ADD PRIMARY KEY (ID);

 MULTIBLE COLUMNS:
ALTER TABLE employee
ADD PK_EMPID PRIMARY KEY (ID, NAME);

 Delete Primary Key Constraint


ALTER TABLE employee DROP PRIMARY KEY;
 Foreign Key Constraint
When create new table
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references employee (ID),
AMOUNT double,
PRIMARY KEY (ID));

When table was already been created:


ALTER TABLE ORDERS
ADD FOREIGN KEY (CUSTOMER_ID) REFERENCES employee (ID);

 Drop Foreign Key Constraint


ALTER TABLE ORDERS
DROP FOREIGN KEY;

 Check Constraint
ALTER TABLE employee
MODIFY AGE INT NOT NULL,
CHECK AGE >= 20;
INSERT INTO employee (ID, NAME, AGE, CITY, SALARY) VALUES (150, 'Mena', 17, 'Alex', 3850)
(#error)

 Drop Check Constraint


ALTER TABLE employee
DROP (CHECK AGE >= 20);

Alias:

To add a column into an existing table


ALTER TABLE employee
ADD COLUMN Dep_ID INT(3);

UPDATE employee
SET Dep_ID = 1
WHERE ID = 1;

UPDATE employee
SET Dep_ID = 3
WHERE ID = 2;

UPDATE employee
SET Dep_ID = 5
WHERE ID = 3;

UPDATE employee
SET Dep_ID = 7
WHERE ID = 4;
UPDATE employee
SET Dep_ID = 9
WHERE ID = 5;

UPDATE employee
SET Dep_ID = 11
WHERE ID = 6;

UPDATE employee
SET Dep_ID = 13
WHERE ID = 7;

UPDATE employee
SET Dep_ID = 15
WHERE ID = 8;

UPDATE employee
SET Dep_ID = 17
WHERE ID = 9;

UPDATE employee
SET Dep_ID = 19
WHERE ID = 10;

Create the department table:


CREATE TABLE department(
D_ID int(3) NOT NULL,
Name varchar(30) NOT NULL,
PRIMARY KEY(D_ID));
Insert data into table:

INSERT INTO department(D_ID, Name) VALUES ( 1, "Head Quarters");


INSERT INTO department(D_ID, Name) VALUES ( 3, "Human Resources");
INSERT INTO department(D_ID, Name) VALUES ( 5, "Public Relations");
INSERT INTO department(D_ID, Name) VALUES ( 7, "Fund Raising");
INSERT INTO department(D_ID, Name) VALUES ( 9, "IT");
INSERT INTO department(D_ID, Name) VALUES ( 11, "Sales");
INSERT INTO department(D_ID, Name) VALUES ( 13, "Complains & Suggestions");
INSERT INTO department(D_ID, Name) VALUES ( 15, "Customer Support");
INSERT INTO department(D_ID, Name) VALUES ( 17, "Natural Resources");
INSERT INTO department(D_ID, Name) VALUES ( 19, "Abroad Relations");

Alias for table:

SELECT emp.ID, emp.NAME, emp.SALARY, emp.Dep_ID, dep.Name


FROM employee AS emp, department as dep
WHERE emp.Dep_ID = dep.D_ID
Alias for Columns:
SELECT ID AS employee_ID, Name as emp_name FROM employee
WHERE SALARY >= 8000;

NULL Value Test:

SELECT * FROM employee WHERE dep_ID IS NULL;

SELECT * FROM employee WHERE dep_ID IS NOT NULL;

Group by:

SELECT Name, ID, AGE, Location, SALARY FROM employee


WHERE dep_ID IS NOT null
GROUP BY Name;

You might also like