-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmodifying_tables.sql
72 lines (43 loc) · 1.44 KB
/
modifying_tables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
USE Sales;
SHOW TABLES;
-- 1. renaming tables
RENAME TABLE customers TO clients;
ALTER TABLE clients RENAME customers;
-- 2. copying tables
CREATE TABLE leads LIKE customers;
describe leads;
describe customers;
CREATE TABLE clients AS SELECT * FROM customers;
SELECT * FROM clients;
-- 3. rename columns
DESCRIBE customers;
ALTER TABLE customers RENAME COLUMN firstname to F_NAME;
-- 4. Changing data types
ALTER TABLE customers MODIFY Address varchar(25);
ALTER TABLE customers CHANGE F_NAME FirstName varchar(15);
-- 5. Adding columns
ALTER TABLE customers ADD COLUMN phone varchar(13) AFTER email;
DESCRIBE customers;
-- 6. Data Validiation
ALTER TABLE customers ADD CONSTRAINT CHECK(phone regexp '^[0-9]*$');
SELECT * FROM customers;
UPDATE customers SET phone = "123" WHERE CID = "AB98";
CREATE TABLE test ( ID int not null auto_increment primary key,
price int CHECK(price >20),
note varchar(10) NOT NULL DEFAULT "eeny" CONSTRAINT CHECK(note in("eeny","meeny","miny","mo"))
);
DESCRIBE test;
INSERT INTO test (price) VALUES (21);
SELECT * FROM test;
ALTER TABLE test ADD column stooge ENUM("moe", "larry", "curly");
INSERT INTO test (stooge) VALUES ("Moe");
-- 7. Removing Constraints
ALTER TABLE customers DROP CHECK customers_chk_1;
SHOW CREATE TABLE customers;
-- 8. Removing columns
ALTER TABLE test DROP stooge;
DESCRIBE test;
ALTER TABLE test MODIFY ID int;
ALTER TABLE test DROP PRIMARY KEY;
-- 9. Delete tables
DROP TABLE test;