My SQL - Basic Concepts - 71766
My SQL - Basic Concepts - 71766
In our day to day life we use some kind of data. Banks, Universities , Libraries are
some of the organisations which require to deal with huge data. The voluminous data
when systematically arranged , is known as database.
A table consists of rows and columns. Columns are also known as fields.
Each column represents a particular type of information eg. Roll Number , Name ,
Address etc.
Columns / Fields
Rows / Records
• May contain blank space in between but not at the beginning or at end.
• It should be unique
Data Types
• Data that can not be used for mathematical calculations are called character
type of data.
• eg. Name CHAR(10) will reserve 10 bytes to store Name. Even if Name is Raju, it
will reserve 10 bytes.
• Numeric data is the data which can be used for mathematical calculations. Eg
Marks , Salary , Hours , Rate etc.
• They can be
TINYINT 1 byte
SMALLINT 2 bytes
MEDIUMINT 3 bytes
INT 4 bytes
BIGINT 8 bytes
FLOAT(w,d) 4 bytes
DOUBLE(w,d) 8 bytes
• Unsigned – This argument can be used only with numeric data columns. It
means no sign. In other words value can not be negative
• Zerofill - This argument can be used only with numeric data columns. It
attaches zeros on the left hand side of the number upto specified length.
Column Specifications
• PRIMARY KEY- Every table has one column whose value uniquely identifies
each row in the table.
• AUTO INCREMENT – This can be used with numeric column and primary key
column only.
• This will add 1 to the maximum positive value stored. By default it starts with 1 .
• UNIQUE – It makes sure that value in the column are unique. This means no
value can be repeated.
• ENUM – It is used to set a value from specified list. A list can contain maximum
65,535 values
• GRADE ENUM( 1 , 2 ,3 , 4 )
Starting My SQL
• Each command should be typed at mysql > prompt and end with ;
• A command need not be typed in a single line. Several lines can be used to
enter a command by pressing enter key. It shows prompt -- > on the next
line, where we can continue typing
• All keywords will be UPPERCASE and all user defined words will be in
lowercase.
CREATE
CREATING TABLES
Table employee
Do not give , or ; after Create table employee just strike the enter key
It gives you -> Start with ( give the list of columns after description of every column
give , after description of last column end with );
Table department
Table student
SHOW
DESCRIBE
INSERT
My Sql > INSERT INTO department VALUES (225, ‘SALES’ , ‘CLERK’ , 5);
It means emp_no = 1500 , f_name = Nita , l_name = Thakur , j-code=5 , h_date = 2011-
03-15 & Sal = 20000
SELECT
UPDATE
This will change contents of dept_name in lower case for all records
WHERE Clause
• Change the address as ‘DADAR’ & date of birth as 5th March 1990 for a student
with name ‘Karan’ in table student
• My Sql > UPDATE student
DELETE
• Write SQL command to delete records of employee hired after 1st January 2010
in the table employee.
DROP
RENAME
ALTER
• Keywords such as CHANGE , MODIFY , ADD , DROP , RENAME etc are used
in ALTER command to get the required results.
CHANGE
->VARCHAR(30);
This will rename column s_adrs as s_address as well as change the width to variable
length 30. Here we don’t have to specify the old width.
MODIFY
• If data type or other details of column name are to be changed retaining the
column name, MODIFY is used.
ADD
DROP
@@@@@@@@@@@@