[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 25
  • Last Modified:

automatically generated text fields in MySQL

How do I create a test  auto-generated field in a MySQL table? Can this field be used as a primary key? If so, what is the syntax for the create statement?

For example

Create table test (
text_id varchar(50) not null generated,
primary key (text_id));
0
chonabraham
Asked:
chonabraham
  • 2
1 Solution
 
Julian HansenCommented:
What do you mean by auto generated?
For a primary key the field must be unique - you can use an autonumber but this is an int field?

Not sure what you are asking?
0
 
chonabrahamAuthor Commented:
can you have a Pk that is varchar and have MySQL generate the unique characters? How do you get MySQL to generate a unique varchar combo to use as the value for any field?
0
 
Julian HansenCommented:
You can use a UUID() default value
create table `ee`.`TableName1`( 
   `id` char(40) DEFAULT 'UUID()' , 
   `Name` varchar(100) 
 )

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now