DB Programs
DB Programs
UPDATE employees
SET salary = 70000
WHERE id = 1;
UPDATE employees
SET position = 'Senior Developer'
WHERE position = 'Developer';
UPDATE employees
SET salary = salary * 1.10
WHERE position = 'Manager';
Primary key:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
position VARCHAR(50),
salary DECIMAL(10, 2),
PRIMARY KEY (id)
);
Foreign key:
CREATE TABLE departments (
dept_id INT NOT NULL AUTO_INCREMENT,
dept_name VARCHAR(100) NOT NULL,
PRIMARY KEY (dept_id)
);
Unique constraint:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL,
position VARCHAR(50),
salary DECIMAL(10, 2),
PRIMARY KEY (id),
UNIQUE (email)
);
Not NULL:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
position VARCHAR(50),
salary DECIMAL(10, 2),
PRIMARY KEY (id)
);
Default:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
position VARCHAR(50) DEFAULT 'Employee',
salary DECIMAL(10, 2),
PRIMARY KEY (id)
);
Check Constraint:
CREATE TABLE employees (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
position VARCHAR(50),
salary DECIMAL(10, 2) CHECK (salary > 0),
PRIMARY KEY (id)
);
Alter command:
Add a column
ALTER TABLE table_name
ADD column_name column_type;
ALTER TABLE table_name
ADD column_name column_type after Counm_name
;
Drop colunm:
ALTER TABLE employees
DROP COLUMN birthdate;
Renaming a clounm:
ALTER TABLE table_name
CHANGE old_column_name new_column_name column_type;
Foreign key:
alter table weapon add primary key(wp_id);
Unique:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name);
Check constraint:
ALTER TABLE employees
ADD CONSTRAINT check_salary CHECK (salary >= 0);
Logical operator:
And:
OR:
Not:
Between:
SELECT * FROM table_name
WHERE column_name BETWEEN value1 AND value2;
%:
SELECT * FROM table_name
WHERE column_name LIKE 'abc%';
_
SELECT * FROM table_name
WHERE column_name LIKE '_bc';
Character set [];
SELECT * FROM table_name
WHERE column_name LIKE '[a-c]%';
Range of characters:
SELECT * FROM table_name
WHERE column_name LIKE 'a[c-f]d';
Order by:
Select * from employees order by amount asc;
Select * from employees order by amount desc;
Select * from employees order by amount,first_name;
Limit clause:
For Join:
Inner join:
select heros.first_name,heros.salary,weapon.wep from heros inner
join weapon on heros.wp_id = weapon.wp_id;
Table short form:
select h.first_name,h.salary,w.wep from heros as h join weapon as w
on h.wp_id = w.wp_id;
Alias name:
select h.first_name as hero,h.salary,w.wep as power from heros as h
join weapon as w on h.wp_id = w.wp_id order by h.first_name;
Unions:
Need same no of colunms and and should be of same data type
Union
Then
Union all
DML:
Insert
Update
Delete
DQL:
Select
TCL:
Autocommit
Commit
rollback
DCL :
Grant and revoke:
Grant all on Table_name to User_name@ Host_name;
Revoke all on Table_name from User_name@Host_name;
Previleges:
SELECT
INSERT
DELETE
UPDATE
ALL