VARCHAR
VARCHAR in SQL is a data type that is used to store non-Unicode strings. The size of a VARCHAR column can vary, and its maximum size is determined at the time of creation of the database table. It stands for variable character string. Unlike CHAR data type, VARCHAR does not pad spaces when the entered value in a VARCHAR column is smaller than its declared size. Moreover, the storage size of VARCHAR data type is the actual length of the data entered, not the maximum size declared at table creation.
Example
Output
Explanation
In this example, a new table named “Customers” is created with two columns: CustomerName and ContactName. Both columns are defined by the VARCHAR data type with a maximum character length of 255. A new row is then inserted into the Customers table, with ‘Cardinal’ and ‘Tom B. Erichsen’ as the values for the CustomerName and ContactName columns, respectively.
Example
Output
Output from the operation is not visible directly. To see the inserted data, use a SELECT statement.
Output
Explanation
The VARCHAR
keyword in SQL is used to define columns of variable-length character strings. The VARCHAR(20)
in the creation of the Employees
table indicates that the Name
field can store up to 20 characters. The INSERT INTO
statement is used to add a new row of data, inserting ‘John Doe’ into the Name
field of the table.
Example
Output
Explanation
In the example provided, VARCHAR(40)
is used to create a Customers table with a CustomerName column that can store up to 40 characters. The INSERT INTO
statement is then used to insert a record into the table.
Example
Output
Query OK, 0 rows affected (0.03 sec)
Explanation
The VARCHAR2
data type is used to store variable length string values. In the above example, three columns: Name
, Address
, and Email
of the Employees
table are created with the VARCHAR2
data type, with a limit of 30 and 50 characters respectively. In the INSERT
statement, a new employee record is inserted with an ID of 1, Name
of ‘John Doe’, an Address
of ‘123 Main St’, and an Email
of ‘jdoe@example.com’.
Example
Output
Explanation
The VARCHAR data type is used to store character values of varying length, but up to a specified length. In the example, the NAME column can store up to 100 characters and the ADDRESS column can store up to 50 characters. The Employee table contains information about an employee- ID, NAME, AGE and ADDRESS. An entry for an employee named ‘John’, aged 25, living in ‘New York’ is inserted into the table and displayed using the SELECT statement.