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

Database Commands - Class 12

This document provides a summary of SQL commands and functions for working with databases and tables. It includes commands for: 1. Creating, opening, and dropping databases. 2. Creating tables with various constraints like unique, primary key, default, check, and foreign key constraints. 3. Inserting, updating, and selecting data from tables, including inserting null values, dates, and data from other tables. 4. Viewing table structure and performing queries with conditions, sorting, aggregation, and aliases.

Uploaded by

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

Database Commands - Class 12

This document provides a summary of SQL commands and functions for working with databases and tables. It includes commands for: 1. Creating, opening, and dropping databases. 2. Creating tables with various constraints like unique, primary key, default, check, and foreign key constraints. 3. Inserting, updating, and selecting data from tables, including inserting null values, dates, and data from other tables. 4. Viewing table structure and performing queries with conditions, sorting, aggregation, and aliases.

Uploaded by

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

S.NO.

FUNCTION SYNTAX EXAMPLE

1 To create a database CREATE DATABASE <database name>; CREATE DATABASE mydb;

CREATE DATABASE [IF NOT EXIST] <database name>; CREATE DATABASE IF NOT EXIST mydb;

2 To open the database USE <database name>; USE mydb;

3 To check the existing databases SHOW DATABASES; SHOW DATABASES;

4 To remove databases DROP DATABASE <database name>; DROP DATABASE mydb;

5 To create table CREATE TABLE <table name> CREATE TABLE employee

( [<column name><datatype><size>], ( ecode integer, ename char(20), sex char(1), grade char(2),

[( <column name><datatype><size>] …. ); gross decimal);


APPLYING CONSTRAINTS TO TABLE COLUMNS

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

( ecode integer, ename char(20), sex char(1), grade char(2),

gross decimal CHECK(gross > 20000) );


10 Foreign key constraint Referential integrity is ensured through foreign key CREATE TABLE items(itemno integer NOT NULL PRIMARY
constraint. Whenever two tables are related by a common KEY, itemname VARCHAR(6));
column, then the related columns in the parent table should
be either declared a PRIMARY KEY or UNIQUE KEY and CREATE TABLE orders(orderno integer NOT NULL PRIMARY
the related column in the child table should have FOREIGN KEY, itemno integer REFERENCES items(itemno));
KEY constraint.

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]

12 To display the list of tables SHOW TABLES; SHOW TABLES;


created in the database

13 To view the table structure of a DESC[RIBE] <table-name> DESC items ;


particular table
(OR)

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)

INSERT INTO items(itemno,itemname) VALUES(1,”wheat”);


15 To insert Null values into table Field names not mentioned in column list or typing NULL INSERT INTO empl
for a particular field name inserts null value into the table. (Empo,Ename,Job,Mgr,Heridate,Sal,Comm,Deptno)
VALUES(8100,”YASH”,”ANALYST”,NULL,”10-MAY-
03”,6000,NULL,20);

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>;

20 To view all datas in a table SELECT * FROM<table-name>; SELECT * FROM student;

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

25 To display a text on query output SELECT salesman_name,com*100,”%” FROM salesman;

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) To check if a particular data


ends with a certain character(s) SELECT name FROM emp WHERE name LIKE “y%”;
across a column and display row
datas for a particular field
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

(i)To update datas in multiple UPDATE items


columns SET ROL = 400, QOH = 700
WHERE icode< “I040”;

(ii) To update by using UPDATE employee


expressions as predicates SET gross = gross+900;

(OR)

UPDATE employee
SET gross = gross*2
WHERE(grade= “E3” OR grade= “E4”);

(iii)To update datas to null values UPDATE employee


SET grade = NULL
WHERE 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>;

33 To remove table components

(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;

IF EXISTS clause checks whether


the particular database is present
in the database or not DROP TABLE IF EXISTS players;

You might also like