VARCHAR
Example
Section titled “Example”CREATE TABLE Customers ( CustomerName VARCHAR(255), ContactName VARCHAR(255));
INSERT INTO Customers (CustomerName, ContactName)VALUES ('Cardinal', 'Tom B. Erichsen');Output
Section titled “Output”Query OK, 0 rows affected (0.03 sec)
Query OK, 1 row affected (0.00 sec)Explanation
Section titled “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
Section titled “Example”CREATE TABLE Employees ( ID INT PRIMARY KEY, Name VARCHAR(20), Age INT);
INSERT INTO Employees (ID, Name, Age)VALUES (1, 'John Doe', 30);Output
Section titled “Output”Output from the operation is not visible directly. To see the inserted data, use a SELECT statement.
SELECT * FROM Employees;Output
Section titled “Output” ID | Name | Age----+---------+----- 1 | John Doe| 30Explanation
Section titled “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
Section titled “Example”CREATE TABLE Customers ( CustomerID int, CustomerName VARCHAR(40));
INSERT INTO Customers (CustomerID, CustomerName)VALUES (1, 'John Doe');Output
Section titled “Output”Command(s) completed successfully.Explanation
Section titled “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
Section titled “Example”CREATE TABLE Employees ( ID INT PRIMARY KEY, Name VARCHAR2(30), Address VARCHAR2(50), Email VARCHAR2(30));
INSERT INTO Employees (ID, Name, Address, Email)VALUES (1, 'John Doe', '123 Main St', 'jdoe@example.com');Output
Section titled “Output”Query OK, 0 rows affected (0.03 sec)
Explanation
Section titled “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
Section titled “Example”CREATE TABLE Employee ( ID INT PRIMARY KEY NOT NULL, NAME VARCHAR(100), AGE INT NOT NULL, ADDRESS VARCHAR(50));
INSERT INTO Employee (ID,NAME,AGE,ADDRESS) VALUES (1, 'John', 25, 'New York');
SELECT * FROM Employee;Output
Section titled “Output”ID NAME AGE ADDRESS------ --------- ------- -----------1 John 25 New YorkExplanation
Section titled “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.