Mysql Class 12 SN
Mysql Class 12 SN
Datatypes: Each column in a database table is required to have a name and a data type.
• String
• Numeric
• Date & Time
String:
• Char(size): A fixed length string (can contain letters ,numbers and special characters). The size parameter
specifies the column length in characters. Can be from 0-255. Default is 1.
• Varchar(size): A variable length string(can contain letters ,numbers and special characters). The size
parameter specifies the column length in characters. Can be from 0-255/65535. Default is 1.
• Binary(size): Equal to char(), but stores binary byte strings. The size parameter specifies the column length
in bytes. Default is 1.
• Blob(size): For BLOBs (Binary Large Objects). It holds up to 65535 bytes of data.
Numeric:
• INT(size): Signed range is from 2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The
size parameter specifies the maximum display width(which is 255).
• Integer(size): Same as INT(size)
• Float(size,decimal): A floating point number. The total number of digits is specified in size, the number of
digits after the decimal point is specified in the decimal parameter.
• Double(size,decimal): A floating point number. The total number of digits is specified in size, the number of
digits after the decimal point is specified in the decimal parameter.
Constraints:
• Constrains are the certain type of restrictions on data value that an attribute can have.
• They are used to ensure correctness of data.
• However it is not mandatory to define a constraint for each attribute of a table.
MySQL Page 1
• However it is not mandatory to define a constraint for each attribute of a table.
Some constraints are below -
• NOT NULL: To ensure the column does not have any null values.
• Unique: To ensure the values are not repeated in a column.
• Default: A default value will be set when no entries are made.
• PRIMARY KEY
• FOREIGN KEY
Drop Statements:
• Drop table if exists <table_name>;
The above command deletes the table from the database.
Delete:
• Delete from <table_name> where <condition>;
Deletes the entry from the table for the specified condition.
• Delete from <table_name>;
All values can be removed using the above command.
Update:
• Update <table_name> set column1 = value1, column2 =value2 where <condition>;
The above commands updates the values of column where the condition is specified.
Order By Clause:
The names can be arranged in descending alphabetical order using the select statement.
If ORDER BY is given without a value then it is arranged in ascending value as default. Descending keyword is Desc
and for ascending is Asc.
• Select * from <table_name> Order by <column_name> desc;
This is display everything from the table given with the names in ascending order.
Operators:
• Arithmetic
• Relational
• Logical
• Uncategorized (Between,And,In)
Between:
• Select <column_name> from <table_name> where <column_name> between <value1> and <value2>;
The above commands displays the value from the column between the specified values.
Not Between:
• Select <column_name> from <table_name> where <column_name> not between <value1> and <value2>;
This command displays values which DO NOT lie between the specified values.
MySQL Page 2
In:
• Select * from <table_name> where <column_name> in (value1,value2…);
This shows the table with the only values specified.
Not In:
• Select * from <table_name> where <column_name> not in (value1,value2…);
• This shows all the values from the table except the values that are specified.
And:
• Select * from <table_name> where <condition1> and <condition2>;
This command shows the table for which both the conditions are met.
Not Operator:
• Select <column1>,<column2> from <table_name> where not <condition>;
This shows all the values except where the not condition is met.
Like Operator:
The two wild card operator : % and __ which will be used along with like.
% sign is used to search for 0 or more characters that look like the given pattern.
__ sign is used to search specifically for one number or character.
Example: Select * from teachers where name like '%a';
This will show all teachers name ending with a.
Column Alias:
It is a temporary name/label given to the column that will appear in the output.
• <column_name > as <column_alias>;
Ex: select adno, name, p+c+m as total from students;
This will change p+c+m as total.
Distinct:
• Select Distinct <column_name> from <table_name>;
Displays uniquely entered values in a database.
MySQL Page 3