You are on page 1of 4

MySQL functions (LIBRARY FUNCTIONS)

STRING FUNCTIONS: String functions are used to manipulate the text string in many ways.
Lower() / Lcase() This function returns the result in Select Lower(“KV1kpa”); /
lowercase (small letters) Select LCase(“KV1kpa”);
ANSWER:
kv1kpa
Upper() / Ucase() This function returns the result in UPPER Select Upper(“KV1kpa”); /
case (capital letters) Select UCase(“KV1kpa”);
ANSWER:
KV1KPA
Concat() This function returns the result after Select concat(“India”, “Bharat”);
concatenating two strings ANSWER:
IndiaBharat
Length() This function calculates the length of the Select Length(“Jai Hind!”);
string. ANSWER: 9
Char() This function returns the character for each Select Char(66,65,71);
integer passed.
NOTE: ASCII (American Standard Code for ANSWER:
Informaton Interchange) Eg. A=65,B=66 ..so BAG
on a= 97,b=98 ..so on
Rtrim() This function returns the result after Select Rtrim(“ India ”);
removing extra spaces from the trailing ANSWER:
end(right side). India
LTrim() This function returns the result after Select Ltrim(“ India ”);
removing extra spaces from the ANSWER:
beginning/start(left side). India
Trim() This function returns the result after Select Trim(“ India ”);
removing extra spaces from both ANSWER:
starting(left) and trailing end(right). India
Right() This function returns the extracted Select Right(“Bharat”, 2);
characters from right (trailing) side. ANSWER:
at
Left() This function returns the extracted Select Left(“Bharat”, 3);
characters from left (beginning) side. ANSWER:
Bha
Mid() / This function returns the substring (part) of Select mid(“Kendriya Vidyalaya,5,10);
a string Select Substr(“Kendriya Vidyalaya,5,10);
Substr() / REMEMBER: MID(“string”, starting Index Select Substring(“Kendriya
Substring() number, number of characters to be Vidyalaya,5,10);
extracted) ANSWER:
(All the above 3 NOTE: Index no. in MySQL for a string riya Vidya
starts from 1. Any thing written inside ‘ ’ or NOTE: Here, 5 is the starting index, 10 is
funstions are same) “ ” is the string. The output produced is the the no. of characters to be
substring. extracted(taken)
Instr() This function returns the index of the first Select instr(“Bharat”, “a”);
occurrence of substring ANSWER:
3
Replace () The REPLACE() function replaces all Select Replace(“India is great.”,
occurrences of a substring within a string, “India”,“Bharat”);
with a new substring. ANSWER:
Note: This function performs a case- Bharat is great.
sensitive replacement.
NUMERIC FUNCTIONS: The numeric functions are those functions that accept numeric values as input parameters
(arguments) and returns numeric values after execution of the query
Mod() Returns remainder of one expression by Select Mod(7,3);
dividing by another expression. Select 7%3;
Select 7 Mod 3 ;
% ANSWER: 1
Sqrt() Returns the non-negative square root of Select Sqrt(16);
numeric expression. ANSWER: 4
Pow() / Power() Returns the value of one expression raised Select Pow(3,2); /
to the power of another expression. Select Power(3,2);
ANSWER: 9
Sign() Returns the sign of a given number. Select Sign(15), Sign(-4), Sign(0);
(+ for positive number, - for negative ANSWER:
number, 0 for 0) +1 -1 0
Round() Returns numeric expression rounded to an
integer. It can be used to round an
QUERY ANSWER
expression to a number of decimal points.
Select round(54.279,1); 54.3
Select round(54.279,2); 54.28
Select round(54.279); 54
Select round(54.279,0); 54
Select round(54.679); 55
Select round(54.679,0); 55
Select round(54.279,-1); 50
Select round(324.279,-1); 320
Select round(354.279,-2); 300
Select round(754.279,-1); 750
Select round(754.279,-2); 800

Truncate() Returns numeric expression1 truncated to


expression2 decimal places. If expression2 is
QUERY ANSWER
0, then the result will have no decimal point.
Select truncate(54.279,1); 54.2
Select truncate(54.279,2); 54.27
Select truncate(54.279); Error (two
arguments
required)
Select truncate(54.279,0); 54
Select truncate(54.679,0); 54
Select truncate(54.279,-1); 50
Select truncate(324.279,-1); 320
Select truncate(354.279,-2); 300
Select truncate(754.279,-1); 750
Select truncate(754.279,-2); 700
DATE AND TIME FUNCTIONS: Date and Time functions operate on values of the DATE data type.

Curdate(); This function returns the current date. Select curdate();


Select current_date();
Current_Date(); Select current_date;
Current_Date; ANSWER:→ 2017-08-24
Sysdate() Returns the current date and time. Select sysdate();
ANSWER:→ 2017-08-24 13:56:20
Now() Returns the time at which the function Select now();
executes. ANSWER:→ 2017-08-24 13:57:45
Date() Returns the Date part of a date or date time Select Date(“2017-08-24 04:17:20”);
expression. (yyyy-mm-dd) ANSWER:→ 2017-08-24
Month() Returns the month from the date passed as Select Month(“2017-08-24”);
argument. (mm form) ANSWER:→ 08
Year() Returns the year from the Date argument Select Year(“2017-08-24”);
passed. ANSWER:→ 2017
MonthName() Returns the full name of the month in text. Select Monthname(“2017-08-24”);
Eg. January,February….December ANSWER:→ August
DayName() The dayname() function returns the name Select DayName(“2017-08-24”);
of the weekday in which it falls in text. ANSWER:→ Friday
Eg.Sunday,Monday…
DayofMonth() / The dayofmonth() function returns the Select DayofMonth(“2017-08-24”);
day(dd) for the specified date. (1-31) Select Day(“2017-08-24”);
Day() ANSWER:→ 24
DayofWeek() The dayofweek() function returns the day of Select DayofWeek(“2017-08-24”);
week in numeric(1-7)Eg.1-Sunday,2- ANSWER:→ 5
Monday…7-Saturday
DayofYear() The dayofyear() function returns the day of Select DayofYear(“2017-08-24”);
the year (1-366) ANSWER:→ 236
DatePart(interval,date) The DatePart() function returns the Select DatePart(m,“2017-08-24”);
specified part of a date. The function ANSWER:→ 8
returns the value in integer. Datepart can
take the first argument(interval) as
(m/mm/year/yy/yyyy/dd/d/weekday/w/dw)
PRIMARY KEY This refers to a set of one or more attributes that can uniquely identify
rows/records within the table.
• CanNOT hold NULL values
• CanNOT hold duplicate(redundant) values
CANDIDATE KEY All attribute/field combinations inside a relation that can serve as primary
key are candidate keys as they are candidate for primary key positions.
ALTERNATE KEY A candidate key that is not primary key, is called an alternate key.
FOREIGN KEY A non-key attribute, whose values are derived from the primary key of some
other table , is known as foreign key in its current table.
RELATION / TABLE A relation is a table storing logically related data;
• A data must be atomic in a cell;
• All rows of this table are distinct;
• Ordering of rows and columns is immaterial.
DOMAIN This is a pool of a values from which the actual values appearing in a given
column are drawn
TUPLE / RECORD/ ROW A row of a table is generally referred to a tuple or record.
ATTRIBUTE/ FIELD/ COLUMN A column of a table is generally referred to as an attribute or field.
DEGREE This refers to the total no. of attributes/fields/columns in a table.

CARDINALITY This refers to total no. of rows/records/tuples in a table.

TABLE: Dept
Primary Key of Dept Alternate Key
Candidate Key

Deptno DName Location


10 IT Mumbai
20 Sales Delhi
30 Marketing Mumbai

TABLE: Emp
Primary Key of Emp Foreign Key of Emp

Empno Ename Sal Deptno


1001 ABC 20000 20
1002 EFG 25000 10

Cardinality of Dept=3, Degree of Dept=3


Cardinality of Emp=2, Degree of Emp=4
Domain of Deptno= All the values in the column of Deptno
Domain of Dname= All the values given in the column of DName (IT,Sales,Marketing)

By Mrs. Payal Bhattacharjee,PGT(CS),K V No.1 Kanchrapara(Kolkata region)

You might also like