Friday, 22 April 2016

How to create, display, delete and change table and columns ?


Create table
CREATE TABLE post
( id INTEGER PRIMARY KEY,
  subject VARCHAR(80) NOT NULL,
  description VARCHAR(256) NOT NULL,
  create_date DATE
);

Create new table by selecting rows from another table
Create a backup copy of this table
CREATE TABLE postBackup
  SELECT * FROM post;


Display table
Display list of all tables in a database
SHOW TABLES;

Display schema of a tables
DESCRIBE TABLES;
DESC TABLES;

Display the "CREATE TABLE" statement of a table
SHOW CREATE TABLE post;

Display list of columns of a table
SHOW COLUMNS FROM post;


Change table
Rename a table
ALTER TABLE post
  RENAME TO posts;

Add a new column to a table
ALTER TABLE post
  ADD COLUMN author VARCHAR(40);

Delete an existing column from a table
ALTER TABLE post
  DROP COLUMN create_date;

Rename column from a table
ALTER TABLE post
  CHANGE COLUMN subject title VARCHAR(60);


Delete table 
Drop a table
DROP TABLE post;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.