0% found this document useful (0 votes)
1 views5 pages

String Data Types

The document outlines data types in SQL, including string, numeric, and date/time types, along with their descriptions and differences. It also provides commands for database management such as CREATE, CONNECT, SHOW, ALTER, DROP, SELECT, INSERT, DELETE, UPDATE, and TRUNCATE. Each command is accompanied by syntax examples to illustrate its usage.

Uploaded by

bkthetryhard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

String Data Types

The document outlines data types in SQL, including string, numeric, and date/time types, along with their descriptions and differences. It also provides commands for database management such as CREATE, CONNECT, SHOW, ALTER, DROP, SELECT, INSERT, DELETE, UPDATE, and TRUNCATE. Each command is accompanied by syntax examples to illustrate its usage.

Uploaded by

bkthetryhard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATA TYPE

The data type of a column defines what value the column can hold: integer, character,
money, date and time, binary, and so on.

Each column in a database table is required to have a name and a data type and its size.

Ex. St_name char(25)

The data type is a guideline for SQL to understand what type of data is expected inside of
each column, and it also identifies how SQL will interact with the stored data.

In MySQL there are three main data types: string, numeric, and date and time.

String Data Types

Data type Description

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 to 255. Default is 1

VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and


special characters). The size parameter specifies the maximum
column length in characters - can be from 0 to 65535

ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list
of possible values. You can list up to 65535 values in an ENUM
list. If a value is inserted that is not in the list, a blank value will
be inserted.

Difference between CHAR & VARCHAR:

1. CHAR Datatype:

1. It provide fixed length of string.

2. Char occupies all the spaces and if space is remaining, then it fill all the blank spaces with
“space” or “white space”.

3. It store 255 characters.


2. VARCHAR Datatype:
1. It provide variable length of string.
2. Varchar occupies only required length and release remaining space.
3. It store 65535 characters.

Numeric Data Types

Data type Description

BOOL or BOOLEAN Zero is considered as false, nonzero values are considered as true.

INT or INTEGER(size) A medium integer. 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)

FLOAT(size, d) 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 d parameter.

Date and Time Data Types

Data type Description

DATE A date. Format: YYYY-MM-DD. The supported range is from


'1000-01-01' to '9999-12-31'

DATETIME A date and time combination. Format: YYYY-MM-DD


hh:mm:ss. The supported range is from '1000-01-01
00:00:00' to '9999-12-31 23:59:59'.
TIME A time. Format: hh:mm:ss. The supported range is from '-
838:59:59' to '838:59:59'

COMMANDS
1. CREATE – To create database/table
name can contain only alphabets, digits, underscore and
dollor.
Syntax: Create database/table database name/table name
Syntax: Create database if not exists database name;

E.G. CREATE DATABASE cbse;


CREATE DATABASE school12;
CREATE DATABASE school_12;
CREATE DATABASE school$12;
CREATE DATABASE _12;
CREATE DATABASE $123;
CREATE DATABASE ARUN_SHARMA;
CREATE TABLE STUDENT2;
CREATE DATABASE 147; NOT ALLOWED
CREATE DATABASE IF NOT EXISTS STUDENT1;

2. CONNECT/USE – To enter in a database


Syntax: Connect database name;
Ex. Use school1;

3. SHOW – To view existing databases and tables in current


database
Syntax: Show databases; Show tables;

4. DESC/DESCRIBE/EXPLAIN – To show structure of table


Syntax: Describe table name;
Ex. Desc student2;
5. ALTER –
To add new column in a table
Syntax: Alter table tablename add new columnname
datatype(size)
Ex. Alter table student add mob_no int
To rename table name
Syntax: ALTER TABLE table_name RENAME TO new_table_name;

Ex. Alter table student rename to student1;


To rename column name
Syntax: ALTER TABLE table_name CHANGE COLUMN old_name
new_name datatype(size)
Ex. alter table student1 change column dob date_of_birth
date;
To delete column
Syntax: ALTER TABLE table_name DROP COLUMN column_name;
Ex. Alter table student drop column gender;
To change datatype of column
Syntax: ALTER TABLE table_name MODIFY column_name
datatype(size)
Ex. Alter table student modify gender varchar(5);

6. DROP – To delete database or table permanently


Syntax: Drop database databasename;
Ex. Drop database school1; Drop table student2;

7. SELECT – To retrieve data from database


Ex. For specific column – select name,class, section from
student;
For all column or rows – select * from student;
SELECT name,marks FROM Student WHERE marks>=80;
8. INSERT INTO – To enter data or values in table
Syntax: Insert into tablename values(…);

9. Delete – To delete records/rows from table without delete


table
To delete all records – Delete from table name;
To delete particular records – Delete from table name where
id = 2;

10. UPDATE – To modify in existing records


Syntax: UPDATE table_name SET column1 = value1, column2 =
value2, ... WHERE condition;
Ex. Update student set marks = 55, section = “g” where roll_no = 6;

11. TRUNCATE – To delete records from table without delete


table
Same as delete but it do not work with where clause.
Syntax: Truncate table tablename;
Ex. Truncate table student2;

You might also like