When it is allowed to write the tablename before a columnname to make a query more accurate.
For example. I have a table "customer" with a column "name". It is ok to write in each sql statement "customer.name" instead of "name"?
Can i use the long version also in create statements?
I know that the long version prevent errors in join statements so why not using it all the time.
INSERT/UDPATE/DELETE statements can operate only on a single table, so you must specify plain column names, and cannot rename the table with an alias. (Well, DELETE does not specify columns.)
However, this is different when using subqueries; with SELECT, all features are available:
UPDATE MyTable -- no alias allowed
SET ParentName = -- no table name allowed
(SELECT T2.Name
FROM MyTable AS T2
WHERE T2.ID = MyTable.ParentID);
Say that I want to run the following SQLite command to delete all rows in a particular table where the Name column is NULL:
DELETE FROM myTable WHERE Name IS NULL;
Now let's say that I've got 20 tables, and they aren't named in any pattern. I want to delete all rows where the Name column is NULL from every table in the database. Is there a statement I can use to do this in one go? Or will I have to run one statement to get the table names, then take the output and run 20 or so commands using each table name?
Here's my first crack at it from the sqlite3 console :
.output tmp
SELECT "DELETE FROM " || sqlite_master.name ||
" WHERE name is NULL;" FROM sqlite_master
WHERE type = "table" AND sqlite_master.name NOT LIKE 'sqlite_%';
.read tmp
This generates the delete statements for each table, outputs it to a file and then reads and runs that file.
(My comment was about the syntax in another answer, you can only delete from one table per delete statement.)
If you have mandatory column 'Name' in every table, then
DELETE FROM myTable1, myTable2, myTable3 WHERE Name IS NULL;
I have problems with sqlite3 and "num_rows". I cannot ask for num_rows of a result like mysql.Now i wan't to automate my SELECT query to ask for the rowcount.
Is there a way to replace all between two patterns?
Like
SELECT date,name,etc FROM myTable WHERE gender=1
// to
SELECT count(*) FROM myTable WHERE gender=1
I must replace all between SELECT and FROM with count(*).
Can i do this this preg_replace?
For your example provided it's simple, but I guess it won't work for all query syntaxes. But have it a try:
PHP code (don't know language you do use):
$count_sql = preg_replace('/^(SELECT)\\s+.*?\\s+(FROM.*)$/s', '$1 count(*) $2', $select_sql);
But I guess, there is a more elegant way to find the num_rows thing. You could use a subquery, like:
SELECT COUNT(*) FROM ( *** original query here *** )
It's not very efficient, but you don't have to deal with the query syntax.
Maybe i should do this in C# but i have more then one row with linkId X. I would like to remove it but i am unsure how. In code i could just use a foreach from 0 to n and remove any found rows with a greater (or !=) id but thats in code. Is there a less difficult way of doing it using sqlite?
Assuming the table's name is tableName and there is a primary key field named id, the following sql would do it. I think the following SQL query is general enough and should be able to be executed under any database engine.
delete from tableName
where id not in (
select min(id) from tableName
group by linkId
)
I want to find out, with an SQL query, whether an index is UNIQUE or not. I'm using SQLite 3.
I have tried two approaches:
SELECT * FROM sqlite_master WHERE name = 'sqlite_autoindex_user_1'
This returns information about the index ("type", "name", "tbl_name", "rootpage" and "sql"). Note that the sql column is empty when the index is automatically created by SQLite.
PRAGMA index_info(sqlite_autoindex_user_1);
This returns the columns in the index ("seqno", "cid" and "name").
Any other suggestions?
Edit: The above example is for an auto-generated index, but my question is about indexes in general. For example, I can create an index with "CREATE UNIQUE INDEX index1 ON visit (user, date)". It seems no SQL command will show if my new index is UNIQUE or not.
PRAGMA INDEX_LIST('table_name');
Returns a table with 3 columns:
seq Unique numeric ID of index
name Name of the index
unique Uniqueness flag (nonzero if UNIQUE index.)
Edit
Since SQLite 3.16.0 you can also use table-valued pragma functions which have the advantage that you can JOIN them to search for a specific table and column. See #mike-scotty's answer.
Since noone's come up with a good answer, I think the best solution is this:
If the index starts with "sqlite_autoindex", it is an auto-generated index for a single UNIQUE column
Otherwise, look for the UNIQUE keyword in the sql column in the table sqlite_master, with something like this:
SELECT * FROM sqlite_master WHERE type = 'index' AND sql LIKE '%UNIQUE%'
you can programmatically build a select statement to see if any tuples point to more than one row. If you get back three columns, foo, bar and baz, create the following query
select count(*) from t
group by foo, bar, baz
having count(*) > 1
If that returns any rows, your index is not unique, since more than one row maps to the given tuple. If sqlite3 supports derived tables (I've yet to have the need, so I don't know off-hand), you can make this even more succinct:
select count(*) from (
select count(*) from t
group by foo, bar, baz
having count(*) > 1
)
This will return a single row result set, denoting the number of duplicate tuple sets. If positive, your index is not unique.
You are close:
1) If the index starts with "sqlite_autoindex", it is an auto-generated index for the primary key . However, this will be in the sqlite_master or sqlite_temp_master tables depending depending on whether the table being indexed is temporary.
2) You need to watch out for table names and columns that contain the substring unique, so you want to use:
SELECT * FROM sqlite_master WHERE type = 'index' AND sql LIKE 'CREATE UNIQUE INDEX%'
See the sqlite website documentation on Create Index
As of sqlite 3.16.0 you could also use pragma functions:
SELECT distinct il.name
FROM sqlite_master AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE m.type='table' AND il.[unique] = 1;
The above statement will list all names of unique indexes.
SELECT DISTINCT m.name as table_name, ii.name as column_name
FROM sqlite_master AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE m.type='table' AND il.[unique] = 1;
The above statement will return all tables and their columns if the column is part of a unique index.
From the docs:
The table-valued functions for PRAGMA feature was added in SQLite version 3.16.0 (2017-01-02). Prior versions of SQLite cannot use this feature.