parsed.org

Tips by tag: queries

Building SQL in Query Tools by xinu on Jan 03, 2008 01:47 PM

You've got to build a query, but you're going to need bits of data from previous queries to do it. As you run the smaller queries, note the interesting value returned as a comment on the same line for use later:

select id_job from jobs where name like = '%foo%' -- 41319
select id_jobentry from jobentries where id_job = 41319

Thanks to McG for the tip.

aquacommentsqueriesquerysqltools
Eliminating Aggregates by cygnus on Mar 31, 2005 08:49 AM

If you have a query like this:

SELECT max(some_column) FROM mytable WHERE ...;

and you want to eliminate the sequence scan that kills the query's performance, an alternative is:

SELECT some_column FROM mytable WHERE ... ORDER BY some_column DESC LIMIT 1;

MIN and MAX aggregates can be eliminated this way.

aggregatesmaxminperformancepostgresqlqueriesscansql
Loading Data From File by xinu on Dec 31, 2005 04:06 PM

Consider the following schema:

CREATE TABLE loadtest (
    pkey int(11) NOT NULL auto_increment,
    name varchar(20),
    exam int,
    score int,
    time_enter timestamp(14),
    PRIMARY KEY (pkey),
);

And the data you need to load:

'name22999990',2,94
'name22999991',3,93
'name22999992',0,91

Running this query would load the data into the columns name, exam, score:

mysql> LOAD DATA INFILE '/tmp/out.txt' INTO TABLE loadtest
    -> FIELDS TERMINATED BY ',' (name,exam,score);

** Note: This tip borrowed from the Linux Gazette. You can read the complete article at http://www.linuxgazette.com/node/9059.

dataloadmysqlqueriesschemasql
PostgreSQL Tables by xinu on Aug 16, 2005 11:46 AM

You can use this query to find the table information:

SELECT
  a.attnum,
  a.attname AS field,
  t.typname AS type,
  a.attlen AS length,
  a.atttypmod AS lengthvar,
  a.attnotnull AS notnull
FROM
  pg_class c,
  pg_attribute a,
  pg_type t
WHERE
  c.relname = 'your_table_name'
  AND a.attnum > 0
  AND a.attrelid = c.oid
  AND a.atttypid = t.oid
  ORDER BY a.attnum
debugginginternalsmetadatapostgresqlqueriessql
Update a Domain Constraint by xinu on Jan 12, 2005 10:23 AM

Use these queries to change the structure of a DOMAIN constraint:

ALTER DOMAIN foo DROP CONSTRAINT bar;
ALTER DOMAIN foo ADD CONSTRAINT bar CHECK (VALUE = 'baz'::text);
constraintsdomainpostgresqlqueriesschemasql
RSS