Вы находитесь на странице: 1из 13

IP PROJECT

1. Explain following My-SQL commands with


Explanation, Syntax and Example:
(a) Create (with all constraints)- Constraints are used to limit the type of data that can go
into a table. They can be specified when a table is created (with the CREATE TABLE
statement). The various types of constraints are:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT

Syntax- Create Database<database name>;


Example-mysql>

create database vasu;

Query OK, 1 row affected (0.00 sec)

(b) Select - Distinct - The Select-Distinct statement will return rows that have
unique data in the column specified.

Syntax-Select Distinct <column name> from <table name>


Example- mysql> select*from stu;
+-------+-----------+-------+
| admno | name

| class |

+-------+-----------+-------+
| 16579 | Vasundhra | XI

| 14579 | Roop

| XI

| 15678 | Varun

| VI

+-------+-----------+-------+
mysql> Select Distinct class from stu;
+-------+

| class |
+-------+
| XI

| VI

+-------+

(c) Insert with Date- This helps us to insert date in a MySQL table.
Syntax(d) Between- The BETWEEN operator selects a range of data between two values. The
values can be numbers, text, or dates.

Syntax-Select*from <table name>where <column name>BETWEEN value1 AND value2;


Examplemysql> select*from stu where admno Between 15753 AND 18490;
+-------+---------+-------+
| admno | name

| class |

+-------+---------+-------+
| 15753 | Roop

| XI

| 16578 | Varun

| VI

| 18490 | Malvika | VIII

+-------+---------+-------+

(e) IN- The IN operator helps us to search the records within the given searching
criteria.

Syntax- Select*from <table name>where <column name>IN (value1, value2, value3,


value4.......)

Examplemysql> Select*from stu where admno IN(15753,18636,13485,14696);


+-------+------+-------+
| admno | name | class |
+-------+------+-------+

| 15753 | Roop | XI

| 13485 | Ekam | IX

+-------+------+-------+

(f) Searching (%,_) The LIKE condition allows us to use wildcards in the WHERE clause of

an SQL statement. This allows us to perform pattern matching. The LIKE condition can be
used in any valid SQL statement - select, insert, update, or delete.
The patterns that we can choose from are:
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character

Syntax- SELECT <column name>FROM <table name> WHERE <column name> LIKE pattern
Example- mysql> select*from stu WHERE name like'r%';
+-------+---------+-------+
| admno | name

| class |

+-------+---------+-------+
| 15753 | Roop

| XI

| 13678 | Radhika | XI

+-------+---------+-------+
mysql> select*from stu WHERE name like'E__m';
+-------+------+-------+
| admno | name | class |
+-------+------+-------+
| 13485 | Ekam | IX

+-------+------+-------+

(g) Update- The UPDATE statement is used to update existing records in a table.
Syntax- UPDATE <table name>SET column1=value, column2=value2,...
WHERE some_column=some_value

Example- mysql>

update stu set name='Amu' where admno=16579;

Query OK, 1 row affected (0.06 sec)


Rows matched: 1

Changed: 1

mysql> select*from stu;

Warnings: 0

+-------+-------+-------+
| admno | name

| class |

+-------+-------+-------+
| 16579 | Amu

NULL |

| 14569 | Roop

NULL |

| 16578 | Varun |

NULL |

+-------+-------+-------+

(h) Delete-The DELETE statement is used to delete rows in a table.


Syntax- DELETE from<table name>WHERE <column name>=some_value
Example- mysql>

select*from stu;

+-------+---------+-------+
| admno | name

| class |

+-------+---------+-------+
| 16579 | Amu

| XI

| 15753 | Roop

| XI

| 16578 | Varun

| VI

| 13678 | Radhika | XI

| 13485 | Ekam

| IX

| 18490 | Malvika | VIII

+-------+---------+-------+
mysql> delete from stu where admno=16579;
Query OK, 1 row affected (0.05 sec)

mysql> select*from stu;

+-------+---------+-------+
| admno | name

| class |

+-------+---------+-------+
| 15753 | Roop

| XI

| 16578 | Varun

| VI

| 13678 | Radhika | XI

| 13485 | Ekam

| IX

| 18490 | Malvika | VIII

+-------+---------+-------+

(e) Drop- The DROP TABLE statement is used to delete a table.


Syntax- DROP DATABASE <database name>
Example- mysql>

drop table stu;

Query OK, 0 rows affected (0.08 sec)

(f) Alter- The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.

Syntax- ALTER TABLE <table name>


Examplemysql> alter table stu drop column class;
Query OK, 3 rows affected (0.17 sec)Records: 3
mysql> select*from stu;
+-------+-----------+
| admno | name

+-------+-----------+
| 16579 | Vasundhra |
| 14569 | Roop

| 16578 | Varun

+-------+-----------+

Duplicates: 0

Warnings: 0

(g)IS NULL-

ISNULL helps us to search a null value in a table by using in the WHERE

clause(Relational operators like=,<> etc. cant be used with NULL).

Syntax- Select*from <table name>where i=NULL

Example- mysql>

select NAME from info where class IS NULL;

Empty set (0.01 sec)

2. Explain any 5 My-SQL Functions with Explanation,


Syntax and Examples.
(a) CHAR()- This function returns a character for each integer passed.
Syntax-

CHAR(value 1 [, value2])

Example- mysql>

Select CHAR(70,65,67,69);

+-------------------+
| CHAR(70,65,67,69) |
+-------------------+
| FACE

+-------------------+

(b) UPPER/UCASE

This function returns a given string into upper case.

Syntax- UPPER(str)
Examplemysql>

Select UPPER('vasundhra sharda');

+---------------------------+
| UPPER('vasundhra sharda') |
+---------------------------+
| VASUNDHRA SHARDA

+---------------------------+

(c) LENGTH-

This function returns the length of a given string in bytes.

Syntax- LENGTH(str)
Examplemysql> Select LENGTH('Vasundhra');
+---------------------+
| LENGTH('Vasundhra') |
+---------------------+
|

9 |

+---------------------+

(d) CURDATE()-

This function returns the current date.

Syntax-CURDATE()
or CURRENT_DATE()

Example- mysql>

Select CURRENT_DATE();

+----------------+
| CURRENT_DATE() |
+----------------+
| 2011-09-01

+----------------+

(e)YEAR()Syntax-

This function returns the year part of a date.

YEAR(date)

Examplemysql> select YEAR('2009-02-03');

+--------------------+
| YEAR('2009-02-03') |
+--------------------+
|

2009 |

+--------------------+

3. Solve following tables with results.


(a) mysql> select*from stu1;
+-----------+-------+---------+------------+--------+-------------+--------+
| StudentNo | Class | Name

| Game

| Grade1 | SUPW

| Grade2 |

+-----------+-------+---------+------------+--------+-------------+--------+
|

10 |

7 | Sameer

| Cricket

| B

| Photography | A

11 |

8 | Sujit

| Tennis

| A

| Gardening

| C

12 |

7 | Kamal

| Swimming

| B

| Photography | B

13 |

7 | Veena

| Tennis

| C

| Cooking

| A

14 |

9 | Archana | BasketBall | A

| Literature

| A

15 |

| Literature

| A

10 | Arpit

| Cricket

| A

+-----------+-------+---------+------------+--------+-------------+--------+

(1) mysql> select Name from stu1 where Grade1='C' OR Grade2='C';


+-------+
| Name

+-------+
| Sujit |
| Veena |
+-------+

(2) mysql> select Game from stu1;

+------------+
| Game

+------------+
| Cricket

| Tennis

| Swimming

| Tennis

| BasketBall |
| Cricket

+------------+

(3) mysql> select SUPW from stu1 where Name Like'A%';


+------------+
| SUPW

+------------+
| Literature |
| Literature |
+------------+

(b)

mysql> select*from SPORTS;

+-----------+-------+---------+------------+--------+-----------+--------+
| StudentNo | Class | Name

| Game1

| Grade1 | Game2

| Grade2 |

+-----------+-------+---------+------------+--------+-----------+--------+
|

10 |

7 | Sammer

| Cricket

| B

| Swimming

| A

11 |

8 | Sujit

| Tennis

| A

| Skating

| C

12 |

7 | Kamal

| Swimming

| B

| Football

| B

13 |

7 | Venna

| Tennis

| C

| Tennis

| A

14 |

9 | Archana | Basketball | A

| Cricket

| A

15 |

| Athletics | C

10 | Arpit

| Cricket

| A

+-----------+-------+---------+------------+--------+-----------+--------+

(1) mysql> Select Name from SPORTS where Grade1='C' or Grade2='C' OR Grade1='C'
and Grade2='C';
+-------+
| Name

+-------+
| Sujit |
| Venna |
| Arpit |
+-------+

(2) mysql> select Name from SPORTS where Game1=Game2;


+-------+
| Name

+-------+
| Venna |
+-------+

(3) mysql> select Game1,Game2 from SPORTS where Name Like'A%';


+------------+-----------+
| Game1

| Game2

+------------+-----------+
| Basketball | Cricket
| Cricket

| Athletics |

+------------+-----------+

(c) mysql>

select*from CLUB;

+----------+-----------+------+------------+------------+------+------+
| COACH_ID | COACHNAME | AGE

| SPORTS

| DATEOFAPP

| PAY

| SEX

+----------+-----------+------+------------+------------+------+------+
|

1 | KUKREJA

35 | KARATE

| 1996-03-27 | 1000 | M

1 | KUKREJA

35 | KARATE

| 1998-01-20 | 1000 | M

3 | KARAN

34 | SQUASH

| 1998-02-19 | 2000 | M

4 | TARUN

33 | BASKETBALL | 1998-01-01 | 1500 | M

5 | ZUBIN

36 | SWIMMING

| 1998-01-12 |

750 | M

6 | KETAKI

36 | SWIMMING

| 1998-02-24 |

800 | F

7 | ANKITA

39 | SQUASH

| 1998-02-20 | 2200 | F

8 | ZAREEN

37 | KARATE

| 1998-02-22 | 1100 | F

9 | KUSH

41 | SWIMMING

| 1998-01-13 |

900 | M

37 | BASKETBALL | 1998-02-19 | 1700 | M

10 | SHAILYA

+----------+-----------+------+------------+------------+------+------+

(1)

mysql> select*from CLUB where SPORTS='SWIMMING';

+----------+-----------+------+----------+------------+------+------+
| COACH_ID | COACHNAME | AGE

| SPORTS

| DATEOFAPP

| PAY

| SEX

+----------+-----------+------+----------+------------+------+------+
|

5 | ZUBIN

36 | SWIMMING | 1998-01-12 |

750 | M

6 | KETAKI

36 | SWIMMING | 1998-02-24 |

800 | F

9 | KUSH

41 | SWIMMING | 1998-01-13 |

900 | M

+----------+-----------+------+----------+------------+------+------+

(2)

mysql> select COACHNAME from CLUB ORDER BY DATEOFAPP DESC;

+-----------+------------+

| COACHNAME | DATEOFAPP

+-----------+------------+
| KETAKI

| 1998-02-24 |

| ZAREEN

| 1998-02-22 |

| ANKITA

| 1998-02-20 |

| SHAILYA

| 1998-02-19 |

| KARAN

| 1998-02-19 |

| KUKREJA

| 1998-01-20 |

| KUSH

| 1998-01-13 |

| ZUBIN

| 1998-01-12 |

| TARUN

| 1998-01-01 |

| KUKREJA

| 1996-03-27 |

+-----------+------------+

(3)

(d)

mysql> select*from student1;

+------+---------+----------+------------+---------+-------+-------+

| No

| Name

| Stripend | Stream

| AvgMark | Grade | Class |

+------+---------+----------+------------+---------+-------+-------+
|

1 | Karan

400 | Medical

78.5 | B

| 12B

2 | Divakar |

450 | Commerce

89.2 | A

| 11C

3 | Divya

300 | Commerce

68.6 | C

| 12C

4 | Arun

350 | Humanities |

73.1 | B

| 12C

5 | Sabina

500 | Nonmedical |

90.6 | A

| 11A

6 | John

400 | Medical

75.4 | B

| 12B

7 | Robert

250 | Humanities |

64.4 | C

| 11A

8 | Rubina

450 | Nonmedical |

88.5 | A

| 12A

9 | Vikas

500 | Nonmedical |

88.5 | A

| 12A

10 | Mohan

300 | Commerce

67.5 | C

| 12C

+------+---------+----------+------------+---------+-------+-------+
10 ows in set (0.01 sec)

(1)mysql>

select*from student1 where Stream='Nonmedical';

+------+--------+----------+------------+---------+-------+-------+
| No

| Name

| Stripend | Stream

| AvgMark | Grade | Class |

+------+--------+----------+------------+---------+-------+-------+
|

5 | Sabina |

500 | Nonmedical |

90.6 | A

| 11A

8 | Rubina |

450 | Nonmedical |

88.5 | A

| 12A

9 | Vikas

500 | Nonmedical |

88.5 | A

| 12A

+------+--------+----------+------------+---------+-------+-------+

(2)
(3)
(4)

Вам также может понравиться