• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

MySQL Substring Default

Is there any way to create a column that defaults to a substring of another column?

create table names (
name varchar(256) not null,
shortname varchar(7) not null default substring(name,1,7)
)

Thanks

Mike
0
hungoveragain
Asked:
hungoveragain
  • 2
  • 2
1 Solution
 
mayankagarwalCommented:
you can create view on the main table to achieve the same
0
 
mayankagarwalCommented:
create table names (
name varchar(256) not null)

create view v_names as select name , shortname substring(name,1,7) from names
)




0
 
hungoveragainAuthor Commented:
That's a nice idea, however I need to be able to update the column based on possible user input

insert into names (name,shortname) values ('Robert Louis Stevenson','Bob')
resolves "Robert Louis Stevenson" - "Bob"

insert into names (name) values ('Robert Louis Stevenson')
resolves "Robert Louis Stevenson" - "Robert "

Any other ideas?

Thanks

Mike
0
 
hungoveragainAuthor Commented:
Solution did not fully solve the problem but no other solutions were given.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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