开发者

How to store url in database and generate unque id

I want to create an application using php and mysql.

Assume i am making api call and getting a list of urls. I want to store those urls and each url should have one uniqe id( ie. number) so that i can pass that id to some page and I can get url from my db.

Assume i made a api call got five urls for keyword "xyz" and i got following urls and respective titles

google.com/abc1.html title1
google.com/abc2.html title2
google.com/abc3.html title3
google.com/abc4.html title4

so i want to generate a unique id of each url like

id1 google.com/abc1.html title1
id2 google.co开发者_JAVA百科m/abc2.html title2
id3 google.com/abc3.html title3
id4 google.com/abc4.html title4

constraints is url should be unique

database can hold around 12Lacs urls.

can you please guide me how to implement that? also give some optimization guide lines

Thanks


Use an auto_increment column for id and a unique constraint on the url column.

create table urls (
  id bigint not null auto_increment
, url varchar(250) unique
, title varchar(250)
);


12Lacs = 1.2 million, right?

For that you can use a regular unsigned integer with auto increment;

create table urls (
    id int unsigned not null auto_increment,
    url varchar(255) not null unique,
    title varchar(255) not null,
    primary key(id)
);

An unsigned integer can hold a value up to 4 294 967 295.


In addition to Martin's answer, you could make the id column unsigned and double it's capacity to 18446744073709551615 rows. If you need more storage than that, you can look at using UUIDs.

http://php.net/manual/en/function.uniqid.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜