Database Commands - Class 12
Database Commands - Class 12
CREATE DATABASE [IF NOT EXIST] <database name>; CREATE DATABASE IF NOT EXIST mydb;
( [<column name><datatype><size>], ( ecode integer, ename char(20), sex char(1), grade char(2),
6 Unique Constraint Ensures that no two rows have the same value in the CREATE TABLE employee
specified columns
( ecode integer NOT NULL UNIQUE, ename (20), sex char(1),
grade char(2), gross decimal);
7 Primary Key constraint Is similar to unique constraint except that only one column CREATE TABLE employee
(or group of columns) can be applied in this constraint
( ecode integer NOT NULL PRIMARY KEY, ename char(20), sex
char(1), grade char(2), gross decimal);
8 Default constraint A default value can be specified and when the user does not CREATE TABLE employee
enter a value for the column automatically the defined
default value is inserted in the field. ( ecode integer, ename char(20), sex char(1), grade char(2)
DEFAULT ‘E1’, gross decimal);
9 Check constraint limits values that can be inserted into a column of a table CREATE TABLE employee
11 To applying constraints to table – CREATE TABLE items (icodechar(5) NOT NULL, descp char(20)
[When a constraint is applied to NOT NULL, ROL integer, QQH integer, CHECK(ROL < QQL),
the group of columns of a table it UNIQUE (cicode, descp)
is called as table constraint]
DESCRIBE items;
14 To insert values into a table INSERT INTO <table-name>[<column list>] INSERT INTO items VALUES(1,”RICE”);
VALUES(<value>,<value>...);
(OR)
16 To insert date into table For any field with DATE datatype must be entered in INSERT INTO DateholderVALUES(“2000-02-11”)
YYYY-MM-DD format
17 To insert data from another table Inserts values to a table from select values from another table INSERT INTO branch1 SELECT*FROM branch2 WHERE gross
for with or without a condition. > 7000.00;
18 To view particular columns from SELECT <column name [, <column name>, …] SELECT name, age, dob FROM student;
a table FROM <table-name>;
19 To view non redundant datas in a SELECT DISTINCT <column name [,<column name>,...] SELECT DISTINCT city FROM suppliers;
column FROM <table name>;
21 To view a table for a condition SELECT<column name>[,<column name>,...] SELECT name,aggregate FROM student WHERE aggregate >
FROM<table-name> 350;
WHERE<condition>;
22 To perform simple calculations in SELECT 4*3;
mysql
23 To give column alias name for a SELECT <column name> AS [columnalias][,<columnname> SELECT date,type AS “Event type” FROM event
particular column name AS [column alias].....]
FROM <table name>
24 To display a query for a null IFNULL(<column name>, value-to-be-substituted) SELECT name,birth,IFNULL(death,”Alive”) FROM Pet;
value
26 Condition based on pattern LIKE keyword is used to compare the string in table with
matches condition and display them according to it
(i)To check if a particular data The position of % in character indicates whether the first or SELECT firstname,lastname,city FROM members WHERE pin
starts with a certain character(s) terminal characters. “one%” matches with strings beginning LIKE “13%”;
across a column and display row with “one” and “%one” matches with strings ending with
datas for a particular field “one”,
name(s)
(ii) NOT keyword is used along SELECT firstname,lastname,city FROM members WHERE pin
with LIKE to display the given NOT LIKE “13%”;
field elements for non similar
starting/ending character(s)
27 To display a table by sorting it in SELECT <column name>[<column name>...] SELECT*FROM employee ORDER BY ename;
any particular order FROM <table name>
[WHERE <predicate>]
[ORDER BY <column name>[[ASC]/DESC]];
(i) To arrange the table in Either ASC or nothing is mentioned to sort the table into SELECT*FROM employee ORDER BY enameASC;
ascending order ascending order
(ii) To arrange the table in DESC keyword is mentioned to sort the table in descending SELECT*FROM employee ORDER BY ename DESC;
descending order order
28 To update datas present in the UPDATE <table name> UPDATE items
relation. SET <column name> = <new value> SET ROL = 400
WHERE <predicate> WHERE ROL = 300
(OR)
UPDATE employee
SET gross = gross*2
WHERE(grade= “E3” OR grade= “E4”);
29 Deleting or removing all values, DELETE FROM <table name> DELETE FROM items;
or some values from a table using [WHERE <predicate>];
WHERE clause DELETE FROM employee
WHERE gross < 2200.00;
30 To add columns to a database ALTER TABLE <table name> ADD <column ALTER TABLE EmplADD(tel_num integer);
name><datatype > [size] [<constraint name>];
31 To modify column definitions ALTER TABLE <table name> ALTER TABLE emplMODIFY(thriftplan number(9,2));
MODIFY(<column name><newdatatype>[(new
size)][FIRST|AFTER column];
32 To change a column name ALTER TABLE <table name> ALTER TABLE customers CHANGE
CHANGE[COLUMN] <old column name><new column first_namefirstnameVARCHAR(20);
name><column definition>;
(i) To drop a primary key ALTER TABLE <table name> ALTER TABLE Empl DROP PRIMARY KEY;
constraint from a table DROP PRIMARY KEY;
(ii) To drop a column from a table ALTER TABLE <table name> ALTER TABLE Empl DROP COLUMN deptno;
DROP COLUMN <column name>;
(iii) To drop a Foreign key from a ALTER TABLE <table name> ALTER TABLE Empl DROP FOREIGN KEY fk_1;
table DROP FOREIGN KEY <column name>;
They can be used together to ALTER TABLE EMPL DROP PRIMARY KEY, DROP
remove a wide range of FOREIGN KEY fk_1, DROP COLUMN deptno;
components
34 To drop a table from a database DROP TABLE[IF EXISTS] <table name> DROP TABLE items;