Computer >> Computer tutorials >  >> Programming >> MySQL

What type of datatype should I use (MySQL) with a mix of string and number?


To mix string and number, you need to use VARCHAR() data type. Following is the syntax −

CREATE TABLE yourTableName
(
yourColumnName1 VARCHAR(40)
);

Let us create a table −

mysql> create table demo28
−> (
−> id int not null auto_increment primary key,
−> user_id varchar(40)
−> );
Query OK, 0 rows affected (0.64 sec)

Insert some records into the table with the help of insert command −

mysql> insert into demo28(user_id) values('John_123');
Query OK, 1 row affected (0.13 sec)

mysql> insert into demo28(user_id) values('David_44');
Query OK, 1 row affected (0.15 sec)

mysql> insert into demo28(user_id) values('Mike99');
Query OK, 1 row affected (0.16 sec)

mysql> insert into demo28(user_id) values('Sam101');
Query OK, 1 row affected (0.17 sec)

Display records from the table using select statement −

mysql> select *from demo28;

This will produce the following output −

+----+----------+
| id | user_id  |
+----+----------+
|  1 | John_123 |
|  2 | David_44 |
|  3 | Mike99   |
|  4 | Sam101   |
+----+----------+
4 rows in set (0.00 sec)