~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/doc/dev/kexi_alter_table.txt

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-----------------------
 
2
KEXI ALTER TABLE ISSUES
 
3
-----------------------
 
4
 
 
5
1. PROBLEMS
 
6
- different databases have different set of altering capabilities
 
7
  (SQLite has no table altering capabilities at all; re-creation is needed:
 
8
  this has high cost in time and disk space for large tables)
 
9
- we need to collect every detailed change made in Table Designer and apply
 
10
  these changes to existing table schema
 
11
- in the case when a table is already filled with data, we need to perform
 
12
  tasks to move that data to a new structure,
 
13
  especially if database engine can't do it for us or if the engine
 
14
  wants simply drop the data because convertion cannot be done automatically
 
15
- because of above issues, some work needs to be done at the client side
 
16
 
 
17
2. What can be altered using ALTER TABLE:
 
18
2.1 Inserting columns
 
19
                (into a specified position)
 
20
                ADD COLUMN <column_definition> [FIRST | AFTER <col_name> ]
 
21
 
 
22
                (adding many colums at the end)
 
23
                ADD COLUMN <column_definition>,...
 
24
 
 
25
2.2 Adding constraints, indices
 
26
                ADD INDEX [<index_name>] [<index_type>] (<index_col_name>,...)
 
27
 
 
28
                ADD CONSTRAINT [<symbol>] PRIMARY KEY [<index_type>] (<index_col_name>,...)
 
29
 
 
30
                ADD CONSTRAINT [<symbol>] UNIQUE [<index_name>] [<index_type>] (<index_col_name>,...)
 
31
 
 
32
                ADD CONSTRAINT [<symbol>] FOREIGN KEY [<index_name>] (<index_col_name>,...)
 
33
                        [<reference_definition>]
 
34
 
 
35
2.3 Altering column properties
 
36
                ALTER COLUMN <col_name> {SET DEFAULT <literal> | DROP DEFAULT}
 
37
 
 
38
                CHANGE COLUMN <old_col_name> column_definition [FIRST|AFTER <col_name>]
 
39
 
 
40
                (rename column)
 
41
                CHANGE <old_column> <new_column>
 
42
 
 
43
                MODIFY COLUMN <column_definition> [FIRST | AFTER <col_name>]
 
44
 
 
45
2.4 Dropping
 
46
                DROP COLUMN <col_name>
 
47
 
 
48
                DROP PRIMARY KEY
 
49
 
 
50
                DROP INDEX <index_name>
 
51
 
 
52
                DROP FOREIGN KEY <fk_symbol>
 
53
 
 
54
2.5 Renaming a table
 
55
                RENAME TO <new_tbl_name>
 
56
 
 
57
 
 
58
3. How to perform table altering be re-creation:
 
59
SQLite:  (taken from Ticket 236: Add RENAME TABLE
 
60
        - will be easier to work around missing ALTER TABLE
 
61
                http://www.sqlite.org/cvstrac/tktview?tn=236,8)
 
62
        Currently the recommended method to ALTER TABLE is:
 
63
 
 
64
        1. Create a temporary table.
 
65
        2. Copy all data from original table to temporary table.
 
66
        3. Drop the original table.
 
67
        4. Create a new table.
 
68
        5. Copy all data from the temporary table to the new table.
 
69
 
 
70
For example, suppose you have a table named "t1" with columns
 
71
names "a", "b", and "c" and that you want to delete column "c"
 
72
from this table. The following steps illustrate how this could be done:
 
73
 
 
74
BEGIN TRANSACTION;
 
75
CREATE TEMPORARY TABLE t1_backup(a,b);
 
76
INSERT INTO t1_backup SELECT a,b FROM t1;
 
77
DROP TABLE t1;
 
78
CREATE TABLE t1(a,b);
 
79
INSERT INTO t1 SELECT a,b FROM t1_backup;
 
80
DROP TABLE t1_backup;
 
81
COMMIT;
 
82
 
 
83
If the table name also changes on altering, it's easier to do the
 
84
altering (no temp. table needed):
 
85
 
 
86
BEGIN TRANSACTION;
 
87
CREATE TABLE t2(c,d);
 
88
INSERT INTO t2 (c,d) SELECT a,b FROM t1;
 
89
DROP TABLE t1;
 
90
COMMIT;
 
91
 
 
92
        If we had RENAME TABLE, we'd get easier alter table here:
 
93
        1. Rename original table to temp name.
 
94
        2. Create new table with ORIGINAL name.
 
95
        3. Move data from previous to new table.
 
96
        4. Drop previous table.
 
97
 
 
98
4. PROPOSED ALGORITHM
 
99
 
 
100
 
 
101
 
 
102
5. Useful documentation
 
103
        http://dev.mysql.com/doc/mysql/en/ALTER_TABLE.html
 
104
 
 
105
 
 
106
6. Document History
 
107
2004-07-20 js
 
108
        Started
 
109
        Asked Richard Hipp about RENAME TABLE
 
110