3

Which SQL data types can I use to accommodate links like www.google.com?

Which SQL data type can I use to store html codes?

<a href="www.google.com">Google</a>

I would like to store my links in the database, to use a for loop to automatically populate he links.

Jervis Chionh
  • 127
  • 2
  • 2
  • 9
  • mostly `NVARCHAR(2083)` is enough, check [this](http://stackoverflow.com/questions/219569/best-database-field-type-for-a-url) – Shaiju T Nov 04 '15 at 13:41

1 Answers1

7

Any of the string data types (e.g. varchar, nvarchar, etc) can accomodate the href attributes from links. And another string column can accommodate the inner text. So something like:

CREATE TABLE T (
    --Various columns
    LinkHref nvarchar(max) not null,
    LinkBody nvarchar(max) not null
)

Then, construct the surrounding anchor tag when you're extracting them for display. This also makes the links more searchable in the database in the future.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 3
    Actually you might want to further normalize, by splitting up the URI into its components (protocol prefix [http, ftp,...], domain name, optional port, path). This would make queries like "give me all links to this domain" much easier. – sleske Jan 27 '12 at 08:24
  • but what if some of the hyperlinks contains more than one links? i cant seem to use the onclick function within a for loop – Jervis Chionh Jan 30 '12 at 12:08