SQL Training
Ben Horne
What is SQL?
SQL Operations
SQL Create Database
CREATE DATABASE [database];
CREATE DATABASE retain DEFAULT CHARACTER SET ‘utf8’ DEFAULT COLLATE ‘utf8_bin’;
SQL Create Table
Each column’s properties needs to be defined, so the query is typically more complex than this example.
SQL Query
SHOW DATABASES;
SELECT * FROM [table];
SELECT [column1],[column2] FROM [table];
SQL Clause Conditions
SELECT * FROM [table1] WHERE [table1].[column1] = [table1].[column2];
SQL Insert
INSERT INTO [table] VALUES ([data1],[data2],[etc]);
INSERT INTO [table] ([column1],[column2],[etc]) VALUES ([data1],[data2],[etc]);
SQL Update
UPDATE [table] SET [column] = [new_data] WHERE [column with primary key] = [value];
For a valuable usage of this in Retain, see KB “How to Rebuild Indexes”
SQL Delete
DROP TABLE [table];
DELETE FROM [table] WHERE [column with primary key] = [value];
Never run without where clause or all data in table will be deleted.
Other SQL Operations
Retain and SQL
Retain Configuration Database
Retain Message Database
Retain Message Database
How to find all information about a message in Retain?
SELECT * FROM t_document WHERE document_id IN (SELECT document_id FROM t_message_attachments WHERE message_id = [parent node #]) \G;
Retain Message Database
To find all the recipients for a message:
SELECT * FROM t_recipient WHERE recipient_id IN (SELECT recipient_id FROM t_message_recipients WHERE message_id = [#]);
Retain Message Database
How to find a messages for a hash on disk?
omit the “.dat” - this extension is only used on disk and is not stored in the database
That last query may return more results than you want. It returns all messages that point to that attachment.
Retain Message Database
SELECT * FROM t_abook WHERE f_uid LIKE (SELECT f_uuid FROM t_uuid_mapping WHERE uid_mapping_id = [#]);
Conclusion