Dup Ver Goto 📝

Sql Basics 1

PT2/lang/sql sql basics does not exist
To
58 lines, 222 words, 1583 chars Page 'Sql_Basics_01' does not exist.

Example 1: Clip

The idea here is to have a LAN wide clipboard using a Mariadb server to store and retrieve clips.

Creating

create database clip;
grant all on clip.* to me@'%' identified by 'boingboing'; 
create table clip (
    id integer auto_increment primary key, 
    name varchar(256) unique, 
    value text, 
    index clipindex (name), 
    time timestamp default current_timestamp on update current_timestamp,
    fulltext index clipcontent (value));
create table history (
    id integer auto_increment primary key, 
    name varchar(256), 
    value text, 
    time timestamp default current_timestamp on update current_timestamp,
    index historyindex (name));

Store

Here we insert or update (upsert)

insert into clip (name,value) 
    values ("boing","random fluffball") 
    on duplicate key update name=VALUES(name), value=VALUES(value);
insert into history (name,value) 
    values ("boing","random fluffball") 
    on duplicate key update name=VALUES(name), value=VALUES(value);

With WHERE:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Read

select name, value, time from clip where name = "boing";

This will return 1 or 0.

select name, value, time from history where name = "boing";

This will return all values stored under that name. We can have a background process to prune old values if desired.

Sorting and Pagination

SELECT id, name, value, time FROM clip WHERE name = "boing"
    ORDER BY id DESC

Examples In Python