Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:02:37+00:00 2026-06-04T06:02:37+00:00

I am writing a Perl based application that converts an SQLite database into an

  • 0

I am writing a Perl based application that converts an SQLite database into an Excel workbook and back again. It works as is, but is missing one small piece. I do not know how to determine if the UNIQUE constraint has been set for a specific column.

To get a list of tables in a database, I use:

select name from sqlite_master 
  where type = 'table' 
  and name <> 'sqlite_sequence' order by name;

Then to get column and constraint information for each table, I do:

PRAGMA table_info($TableName);

This tells me everything I need to know except if a column has the UNIQUE constraint enabled.

In case this is not clear, here is a trivial example. Suppose I create a database table by doing this:

CREATE TABLE DATA 
(
    ID integer primary key, 
    Invoice integer unique, 
    Product varchar, 
    Comment varchar
);

I want to know how I can subsequently interrogate the SQLite database table created in this manner to determine which columns have the UNIQUE constraint set. In this case, it would be the Invoice column.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:02:38+00:00Added an answer on June 4, 2026 at 6:02 am

    You need to use a the PRAGMA index_list statement.

    This subroutine determines the value of the unique attribute, given a database handle and a table and column name.

    sub column_unique {
      my ($dbh, $table, $column) = @_;
      my $sth = $dbh->prepare("PRAGMA index_list($table)");
      $sth->execute;
      my $rec = $sth->fetchall_hashref('name');
      return $rec->{$column} && $rec->{$column}{unique};
    }
    

    If the column appears in the index_list pragma for the table, the value of the unique attribute (either '0' or '1') is returned. If not then undef is returned. This allows calls like

    if (column_unique($dbh, 'DATA', 'Invoice')) { ... }
    

    Edit

    My apologies, my first answer was wrong. SQLite allows only indexes to be declared unique, and those indexes can be multi-column combinations. This revised subroutine looks at the composition of each unique index for the table and checks whether the given column is all or part of it.

    This isn’t very satisfactory, as being a part of a unique index doesn’t say much about the column itself, but any improvement would depend on what you want to use the information for.

    sub column_unique {
    
      my ($dbh, $table, $column) = @_;
    
      my $sth = $dbh->prepare("PRAGMA index_list($table)");
      $sth->execute;
      my $list = $sth->fetchall_hashref('name');
    
      foreach my $index (keys %$list) {
    
        next unless $list->{$index}{unique};
    
        my $sth = $dbh->prepare("PRAGMA index_info($index)");
        $sth->execute;
        my $info = $sth->fetchall_hashref('name');
    
        return 1 if $info->{$column};
      }
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to be writing to a MySQLite database file, using Perl's DBD:SQLite
I have application that works using Perl's CGI::Fast . Basically mainloop of the code
I am writing a Perl script (in Windows) that is using File::Find to index
I'm writing a Perl script that reads data from the infamous /dev/input/event* and I
Perl is really good for writing the kind of string/file parsing programs that I
I'm writing a tool in Perl that needs to scan for certain binary patterns
I am writing a comparefiles subroutine in Perl that reads a line of text
So i'm writing a quick perl script that cleans up some HTML code and
I'm writing a perl script that takes a duration option, and I'd like to
I am writing a perl routine that mounts specific drives at startup. However, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.