Unique row question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laphan

    #1

    Unique row question

    Hi All

    I know you can set a row to be unique, but I want expand on this in that I
    want the DB schema to only disallow an entry if the row isn't unique across
    3 fields. Is it possible?

    My DDL for this table is as follows:

    CREATE TABLE `WEBSTRINGS` (
    `STRINGID` INT NOT NULL AUTO_INCREMENT,
    `TOKENID` INT DEFAULT 0,
    `LANGID` VARCHAR(30),
    `STRINGTEXT` VARCHAR(255),
    `PAGEID` SMALLINT DEFAULT 0,
    `GUI` TINYINT UNSIGNED DEFAULT 0,

    INDEX `indxLANGID` (`LANGID`),
    INDEX `indxGUI` (`GUI`),
    INDEX `indxTOKENID` (`TOKENID`),
    INDEX `indxSTRINGID` (`STRINGID`),
    PRIMARY KEY `PrimaryKey` (`STRINGID`)
    # -- can use? UNIQUE `unqSTRINGID` (`LANGID,STRING TEXT,GUI`)
    );

    The 'uniquness' I need is that if there is already an entry with the same
    LANGID, STRINGTEXT and GUI then don't allow the INSERT entry to go in.

    Please note that I already have this kind of checking in my front-end code,
    its just for when values are being poked in directly, eg via SQLyog.

    Thanks

    Laphan



  • steve

    #2
    Re: Unique row question

    "Laphan" wrote:[color=blue]
    > Hi All
    >
    > I know you can set a row to be unique, but I want expand on this in
    > that I
    > want the DB schema to only disallow an entry if the row isn’t
    > unique across
    > 3 fields. Is it possible?
    >
    > My DDL for this table is as follows:
    >
    > CREATE TABLE `WEBSTRINGS` (
    > `STRINGID` INT NOT NULL AUTO_INCREMENT,
    > `TOKENID` INT DEFAULT 0,
    > `LANGID` VARCHAR(30),
    > `STRINGTEXT` VARCHAR(255),
    > `PAGEID` SMALLINT DEFAULT 0,
    > `GUI` TINYINT UNSIGNED DEFAULT 0,
    >
    > INDEX `indxLANGID` (`LANGID`),
    > INDEX `indxGUI` (`GUI`),
    > INDEX `indxTOKENID` (`TOKENID`),
    > INDEX `indxSTRINGID` (`STRINGID`),
    > PRIMARY KEY `PrimaryKey` (`STRINGID`)
    > # -- can use? UNIQUE `unqSTRINGID` (`LANGID,STRING TEXT,GUI`)
    > );
    >
    > The ’uniquness’ I need is that if there is already an
    > entry with the same
    > LANGID, STRINGTEXT and GUI then don’t allow the INSERT entry to
    > go in.
    >
    > Please note that I already have this kind of checking in my[/color]
    front-end[color=blue]
    > code,
    > its just for when values are being poked in directly, eg via[/color]
    SQLyog.[color=blue]
    >
    > Thanks
    >
    > Laphan[/color]

    I think you have answered your own question. Create a unique index on
    three fields. See exact syntax here:
    http://dev.mysql.com/doc/mysql/en/CREATE_INDEX.html

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/mySQL-Unique...ict133020.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=444520

    Comment

    • Laphan

      #3
      Re: Unique row question

      Many thanks for coming back to me Steve.

      Having re-jigged the UNIQUE to the proper syntax it does do what I want -
      wahey!!

      Just to finish this off here is my final table DDL:

      CREATE TABLE `WEBSTRINGS` (
      `STRINGID` INT NOT NULL AUTO_INCREMENT,
      `TOKENID` INT DEFAULT 0,
      `LANGID` VARCHAR(30),
      `STRINGTEXT` VARCHAR(255),
      `PAGEID` TINYINT UNSIGNED DEFAULT 0,
      `GUI` TINYINT UNSIGNED DEFAULT 0,

      INDEX `indxLANGID` (`LANGID`),
      INDEX `indxGUI` (`GUI`),
      INDEX `indxTOKENID` (`TOKENID`),
      INDEX `indxSTRINGID` (`STRINGID`),
      UNIQUE `unqSTRINGID` (`LANGID`,`STRI NGTEXT`,`GUI`),
      PRIMARY KEY `PrimaryKey` (`STRINGID`)
      );

      Do I really need the other INDEXes if I have this UNIQUE one set?

      What would be the pluses and minuses of it?

      Thanks

      Laphan



      steve <UseLinkToEmail @dbForumz.com> wrote in message
      news:10g8vg72fn c2jee@news.supe rnews.com...
      "Laphan" wrote:[color=blue]
      > Hi All
      >
      > I know you can set a row to be unique, but I want expand on this in
      > that I
      > want the DB schema to only disallow an entry if the row isn't
      > unique across
      > 3 fields. Is it possible?
      >
      > My DDL for this table is as follows:
      >
      > CREATE TABLE `WEBSTRINGS` (
      > `STRINGID` INT NOT NULL AUTO_INCREMENT,
      > `TOKENID` INT DEFAULT 0,
      > `LANGID` VARCHAR(30),
      > `STRINGTEXT` VARCHAR(255),
      > `PAGEID` SMALLINT DEFAULT 0,
      > `GUI` TINYINT UNSIGNED DEFAULT 0,
      >
      > INDEX `indxLANGID` (`LANGID`),
      > INDEX `indxGUI` (`GUI`),
      > INDEX `indxTOKENID` (`TOKENID`),
      > INDEX `indxSTRINGID` (`STRINGID`),
      > PRIMARY KEY `PrimaryKey` (`STRINGID`)
      > # -- can use? UNIQUE `unqSTRINGID` (`LANGID,STRING TEXT,GUI`)
      > );
      >
      > The 'uniquness' I need is that if there is already an
      > entry with the same
      > LANGID, STRINGTEXT and GUI then don't allow the INSERT entry to
      > go in.
      >
      > Please note that I already have this kind of checking in my[/color]
      front-end[color=blue]
      > code,
      > its just for when values are being poked in directly, eg via[/color]
      SQLyog.[color=blue]
      >
      > Thanks
      >
      > Laphan[/color]

      I think you have answered your own question. Create a unique index on
      three fields. See exact syntax here:
      http://dev.mysql.com/doc/mysql/en/CREATE_INDEX.html

      --
      http://www.dbForumz.com/ This article was posted by author's request
      Articles individually checked for conformance to usenet standards
      Topic URL: http://www.dbForumz.com/mySQL-Unique...ict133020.html
      Visit Topic URL to contact author (reg. req'd). Report abuse:



      Comment

      • Bill Karwin

        #4
        Re: Unique row question

        Laphan wrote:[color=blue]
        > Do I really need the other INDEXes if I have this UNIQUE one set?
        > What would be the pluses and minuses of it?[/color]

        Read this excerpt from: http://dev.mysql.com/doc/mysql/en/MySQL_indexes.html

        "If the table has a multiple-column index, any leftmost prefix of the
        index can be used by the optimizer to find rows. For example, if you
        have a three-column index on (col1, col2, col3), you have indexed search
        capabilities on (col1), (col1, col2), and (col1, col2, col3).

        MySQL can't use a partial index if the columns don't form a leftmost
        prefix of the index. Suppose that you have the SELECT statements shown here:

        SELECT * FROM tbl_name WHERE col1=val1;
        SELECT * FROM tbl_name WHERE col2=val2;
        SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

        If an index exists on (col1, col2, col3), only the first of the
        preceding queries uses the index. The second and third queries do
        involve indexed columns, but (col2) and (col2, col3) are not leftmost
        prefixes of (col1, col2, col3)."

        So, if you need to do queries that perform sorting operations on col2
        and col3, or search based on conditions in col2 and col3 (in your case,
        `STRINGTEXT` and `GUI`), the performance of these queries will likely be
        improved by having individual indexes on these colums.

        I have an analogy that I use to help people understand compound indexes.
        Suppose I ask you to look up names in a telephone book. If I ask you
        to find all last names staring with a prefix such as "Stev", it's pretty
        easy to find all of them; they're sorted together. But if I ask you to
        find all _first_ names that begin with "Stev," then you are forced to
        scan through the whole phone book. The phone book is ordered by last
        name, then by first name; the equivalent of a compound index on those
        columns, and no other index.

        To improve the speed of the second search, you'd have to have an
        alternate list of names, ordered by first name. But that would double
        the thickness of the book, for a search that you probably don't need
        often enough to justify the extra paper and printing.

        Likewise, in a database, an additional index on the secondary columns of
        a compound index will benefit queries on those columns, but at a cost of
        additional storage space, and some computational expense to maintain the
        index as records are added and deleted.

        Whether this expense of storage and computation is justified depends on
        how frequently you need to do such lookups in your application, and how
        important is the speed of these lookups when you do them.

        Regards,
        Bill K.

        Comment

        • Laphan

          #5
          Re: Unique row question

          Many thanks for coming back to me Steve.

          Having re-jigged the UNIQUE to the proper syntax it does do what I want -
          wahey!!

          Just to finish this off here is my final table DDL:

          CREATE TABLE `WEBSTRINGS` (
          `STRINGID` INT NOT NULL AUTO_INCREMENT,
          `TOKENID` INT DEFAULT 0,
          `LANGID` VARCHAR(30),
          `STRINGTEXT` VARCHAR(255),
          `PAGEID` TINYINT UNSIGNED DEFAULT 0,
          `GUI` TINYINT UNSIGNED DEFAULT 0,

          INDEX `indxLANGID` (`LANGID`),
          INDEX `indxGUI` (`GUI`),
          INDEX `indxTOKENID` (`TOKENID`),
          INDEX `indxSTRINGID` (`STRINGID`),
          UNIQUE `unqSTRINGID` (`LANGID`,`STRI NGTEXT`,`GUI`),
          PRIMARY KEY `PrimaryKey` (`STRINGID`)
          );

          Do I really need the other INDEXes if I have this UNIQUE one set?

          What would be the pluses and minuses of it?

          Thanks

          Laphan



          steve <UseLinkToEmail @dbForumz.com> wrote in message
          news:10g8vg72fn c2jee@news.supe rnews.com...
          "Laphan" wrote:[color=blue]
          > Hi All
          >
          > I know you can set a row to be unique, but I want expand on this in
          > that I
          > want the DB schema to only disallow an entry if the row isn't
          > unique across
          > 3 fields. Is it possible?
          >
          > My DDL for this table is as follows:
          >
          > CREATE TABLE `WEBSTRINGS` (
          > `STRINGID` INT NOT NULL AUTO_INCREMENT,
          > `TOKENID` INT DEFAULT 0,
          > `LANGID` VARCHAR(30),
          > `STRINGTEXT` VARCHAR(255),
          > `PAGEID` SMALLINT DEFAULT 0,
          > `GUI` TINYINT UNSIGNED DEFAULT 0,
          >
          > INDEX `indxLANGID` (`LANGID`),
          > INDEX `indxGUI` (`GUI`),
          > INDEX `indxTOKENID` (`TOKENID`),
          > INDEX `indxSTRINGID` (`STRINGID`),
          > PRIMARY KEY `PrimaryKey` (`STRINGID`)
          > # -- can use? UNIQUE `unqSTRINGID` (`LANGID,STRING TEXT,GUI`)
          > );
          >
          > The 'uniquness' I need is that if there is already an
          > entry with the same
          > LANGID, STRINGTEXT and GUI then don't allow the INSERT entry to
          > go in.
          >
          > Please note that I already have this kind of checking in my[/color]
          front-end[color=blue]
          > code,
          > its just for when values are being poked in directly, eg via[/color]
          SQLyog.[color=blue]
          >
          > Thanks
          >
          > Laphan[/color]

          I think you have answered your own question. Create a unique index on
          three fields. See exact syntax here:
          http://dev.mysql.com/doc/mysql/en/CREATE_INDEX.html

          --
          http://www.dbForumz.com/ This article was posted by author's request
          Articles individually checked for conformance to usenet standards
          Topic URL: http://www.dbForumz.com/mySQL-Unique...ict133020.html
          Visit Topic URL to contact author (reg. req'd). Report abuse:



          Comment

          • Bill Karwin

            #6
            Re: Unique row question

            Laphan wrote:[color=blue]
            > Do I really need the other INDEXes if I have this UNIQUE one set?
            > What would be the pluses and minuses of it?[/color]

            Read this excerpt from: http://dev.mysql.com/doc/mysql/en/MySQL_indexes.html

            "If the table has a multiple-column index, any leftmost prefix of the
            index can be used by the optimizer to find rows. For example, if you
            have a three-column index on (col1, col2, col3), you have indexed search
            capabilities on (col1), (col1, col2), and (col1, col2, col3).

            MySQL can't use a partial index if the columns don't form a leftmost
            prefix of the index. Suppose that you have the SELECT statements shown here:

            SELECT * FROM tbl_name WHERE col1=val1;
            SELECT * FROM tbl_name WHERE col2=val2;
            SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

            If an index exists on (col1, col2, col3), only the first of the
            preceding queries uses the index. The second and third queries do
            involve indexed columns, but (col2) and (col2, col3) are not leftmost
            prefixes of (col1, col2, col3)."

            So, if you need to do queries that perform sorting operations on col2
            and col3, or search based on conditions in col2 and col3 (in your case,
            `STRINGTEXT` and `GUI`), the performance of these queries will likely be
            improved by having individual indexes on these colums.

            I have an analogy that I use to help people understand compound indexes.
            Suppose I ask you to look up names in a telephone book. If I ask you
            to find all last names staring with a prefix such as "Stev", it's pretty
            easy to find all of them; they're sorted together. But if I ask you to
            find all _first_ names that begin with "Stev," then you are forced to
            scan through the whole phone book. The phone book is ordered by last
            name, then by first name; the equivalent of a compound index on those
            columns, and no other index.

            To improve the speed of the second search, you'd have to have an
            alternate list of names, ordered by first name. But that would double
            the thickness of the book, for a search that you probably don't need
            often enough to justify the extra paper and printing.

            Likewise, in a database, an additional index on the secondary columns of
            a compound index will benefit queries on those columns, but at a cost of
            additional storage space, and some computational expense to maintain the
            index as records are added and deleted.

            Whether this expense of storage and computation is justified depends on
            how frequently you need to do such lookups in your application, and how
            important is the speed of these lookups when you do them.

            Regards,
            Bill K.

            Comment

            Working...