0% found this document useful (0 votes)
11 views14 pages

sql commands1

Uploaded by

sasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
11 views14 pages

sql commands1

Uploaded by

sasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

COMMANDS IN SQL

1. To create database
Syntax :
create database databasename;
Eg:
create database school;

2. To see all databases


Syntax:
show databases;
Eg:
show databases;

3. To select database
Syntax:
use databasename;
Eg:
use school;

4. To create table
Syntax:
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
:
attributenameN datatype constraint);
Eg:
create table student(RNO char(5) primary key,NAME varchar(25),MARK int(3) not null);
create table Admin(Admno char(7),class char(10),RNO char(5));

5. To see structure of table


Syntax:
Desc 0r Describe tablename;
Eg:
Desc student;
6. To insert values
Syntax:
INSERT INTO tablename VALUES(value 1, value 2,....);
Eg:
Insert into student values(1001,’raj’,90),(1002,’vino’,97),(1003,’Dev’,87),
(1004,’Tharun’,67),(1005,’Mano’,78);

7. display records from a table


Syntax:
Select * from tablename;
Eg:
Select * from student;

8. display particular records


Syntax:
Select * from tablename where condition;
Eg:
Select * from student where mark>90;

9. To display particular columns from a table


Syntax:
Select col1,col2 from tablename;
Eg;
Select RNO,mark from student;

10. To display particular columns from a table


Syntax:
Select col1,col2 from tablename where condition;
Eg;
Select RNO,mark from student where mark>90;

11. To see all tables


Syntax:
Show tables;
Eg:
Show tables;

12.To delete Table


Syntax:
Drop table tablename;
Eg:
Drop table student;

13. To insert values only for some of the attributes in a table


INSERT INTO tablename (column1, column2, ...)VALUES (value1, value2, ...);
Eg:
Insert into student(rno,name) values(1001,’raj’),(1002,’vino’);

14. Add an attribute to an existing table


Syntax:
ALTER TABLE table_name ADD attribute name DATATYPE;
Eg:
Alter table student add DOB date;
Alter table student add Aadhar int (12);
Alter table student add class char(7)
Alter table student add address varchar(15);

15. Add primary key to a table


Syntax:
ALTER TABLE tablename ADD PRIMARY KEY (column_name);
Eg:
Alter table student add primary key(Aadhar);

16. Add foreign key to a table


Syntax:
ALTER TABLE table_name ADD FOREIGN KEY(attribute name) REFERENCES
referenced_table_name (attribute name);
Eg:
Alter table student add foreign key(RNO) references Admin(RNO);

17. Modify datatype of an attribute


Syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE;
Eg:
Alter table student modify Aadhar int(12);
18. Add default value to an attribute
syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE DEFAULT default_value;
Eg:
ALTER Table student modify class varchar default ‘XIIA’;

19. Remove an attribute


Syntax:
ALTER TABLE table_name DROP attribute;
Eg:
ALTER TABLE student DROP place;

20. Remove primary key from the table


Syntax:
ALTER TABLE table_name DROP PRIMARY KEY;
Eg;
Alter table student drop primary key;

21. Distinct clause(avoid duplicate values in a column)


Syntax:
Select distinct column_name from tablename;
Eg:
Select distinct place from student;

22. Distinct clause with condition


Syntax:
Select distinct column_name from tablename where condition;
Eg:
Select distinct place from student where mark<90;

23. Satisfy 2 conditions


Syntax:
Select * from tablename where condition1 and condition2;
Eg:
Select * from student where mark>90 and Rno>’1005’;

24. Satisfy 1 condition among 2 conditions


Syntax:
Select * from tablename where condition1 or condition2;
Eg:
Select * from student where mark>90 or Rno>’1005’;

25. range operator


Syntax:
Select * from tablename where column between val1 and val2;
Eg:
Select * from student where mark between 80 and 90;

26. Membership operator IN


Syntax:
Select * from tablename where column in (value,value);
Eg:
Select * from student where place in(‘kpd’,’vlr’,;gym’);

27. Membership operator NOT IN


Syntax:
Select * from tablename where column not in (value,value);
Eg:
Select * from student where place not in(‘kpd’,’vlr’,;gym’);

28. ORDER BY Clause


Syntax:
Select * from student order by column;
Eg (Ascending)
29. ORDER BY CLAUSE FOR DESCENDING
Syntax:
Select * from student order by mark;
Eg(descending)
Select * from student order by mark desc;

30. Renaming columns


Syntax:
Select column name as newname from table name;
Eg:
Select Name as studentname from student;

31. Applying null values on column


Syntax:
Select * from tablename where column is null;
Eg:
Select * from student where name is null;
Select * from student where name is not null;

32. pattern matching(%,_)


The LIKE operator makes use of the following two wild card characters:
• % (per cent)- used to represent zero, one, or multiple characters
• _ (underscore)- used to represent exactly a single character
Eg:
Select * from student where name like ‘k%’;
Select * from student where name like ‘_b%’;
Select * from student where name like ‘%s%’ ;
Select * from student where name ‘%a’;

33. update values in record and using calculation


Syntax:
UPDATE table_name SET attribute1 = value1, attribute2 = value2, ... WHERE
condition;
Eg:
Update student set MARK=99 where RNO=1001;
INCREASE 20% MARK TO PARTICULAR RECORD
Update student set mark=mark+mark*.2 where RNO=1005

34. Data Deletion DELETE statement is used to delete/remove one or more records
from a table.
Syntax:
DELETE FROM table_name WHERE condition;
Eg:
Delete from student where mark is null;

35. Single Row Functions These are also known as Scalar functions. Single row
functions are applied on a single value and return a single value. Figure 9.3 lists
different single row functions under three categories — Numeric (Math), String,
Date and Time

Math functions:
36. Aggregate Functions Aggregate functions are also called Multiple Row
functions. These functions work on a set of records as a whole and return a single
value for each column of the records on which the function is applied
37. Group By cLauSe In SQL At times we need to fetch a group of rows on the basis
of common values in a column. This can be done using a group by clause. It groups
the rows together that contains the same values in a specified column. We can use
the aggregate functions (COUNT, MAX, MIN, AVG and SUM) to work on the
grouped values. HAVING Clause in SQL is used to specify conditions on the rows
with Group By clause
38.operations on relations We can perform certain operations on relations like
Union, Intersection and Set Difference to merge the tuples of two tables. These three
operations are binary operations as they work upon two tables. Note here that these
operations can only be applied if both the relations have the same number of
attributes and corresponding attributes in both tables have the same domain. 9.10.1
UNION (∪) This operation is used to combine the selected rows of two tables at a
time. If some rows are same in both the tables, then result of the Union operation
will
show those rows only once. Figure 9.4 shows union of two sets. Music Dance
Figure 9.4: Union of two sets Let us consider two relations DANCE and MUSIC
shown in Tables 9.18 and 9.19 respectively.

39. Cartesian Product (X)


Cartesian product operation combines tuples from two relations. It results in all
pairs of rows from the two input relations, regardless of whether or not they have
the same values on common attributes. It is denoted as ‘X’. The degree of the
resulting relation is calculated as the sum of the degrees of both the relations under
consideration. The cardinality of the resulting relation is calculated as the product
of the cardinality of relations on which cartesian product is applied
Syntax:
Select * from table1,table2;
mysql> SELECT * FROM DANCE, MUSIC;
40. JOIN on two tables JOIN operation combines tuples from two tables on
specified conditions. This is unlike cartesian product which make all possible
combinations of tuples
a) Using condition in where clause mysql> SELECT * FROM UNIFORM U, COST
C WHERE U.UCode = C.UCode;

b) Explicit use of JOIN clause mysql> SELECT * FROM UNIFORM U JOIN


COST C ON U.Ucode=C.Ucode;

c) Explicit use of NATURAL JOIN clause The output of queries (a) and (b) shown
in Table 9.26 has a repetitive column Ucode having exactly the same values. This
redundant column provides no additional information. There is an extension of
JOIN operation called NATURAL JOIN which works similar to JOIN clause in
SQL but removes the redundant attribute. This operator can be used to join the
contents of two tables if there is one common attribute in both the tables.

You might also like