Database Developers' Quick-Reference to MySQL DDL

AID: 1522
  • Status: Published

1880 points

  • ByDanielWilson
  • TypeGeneral
  • Posted on2009-09-14 at 11:21:37
Awards
  • Community Pick
Some time back I wrote an article to help DB developers get started in MySQL.
Database Developers' Quick-Reference to MySQL

Now I'm adding some Data Definition Language (DDL) statements to that.

1

Describe


What does the silly table look like?  What are it's fields and what are their types?
MS SQL:

exec sp_help tablename
                                    
1:

Select allOpen in new window


MySQL:

Describe tablename;
                                    
1:

Select allOpen in new window




2

information_schema tables


information_schema is presented as its own DB.  Actually, I think it is its own Schema, but all the stuff a SQL Server developer would group as a database, MySQL groups as a Schema.

Most of your schema queries are going to one or more of the information_schema tables.
Consider information_schema.tables roughly equivalent to sys.tables and information_schema.columns roughly equivalent to syscolumns.

So ... the next several items will make use of that knowledge.



3

Does Table X Exist in my MySQL DB?


This is as easy as in SQL Server, but a bit different. In MySQL it's:

SELECT COUNT( * ) FROM information_schema.tables WHERE table_schema = 'MyDBName' AND table_name = 'MyTableName'; 
                                    
1:

Select allOpen in new window




4

Does Table X Have Column Y in my MySQL DB?


This one's actually a bit easier than in MS SQL Server as no join is required ...

SELECT count( * ) FROM information_schema.columns WHERE table_schema = 'MyDBName' AND Table_Name = 'MyTableName' AND column_Name = 'MyColumnName'; 
                                    
1:

Select allOpen in new window




5

Add table only if it does not exist


This one's better in MySQL than in MS SQL Server also:

CREATE TABLE IF NOT EXISTS tablename ( ... ); 
                                    
1:

Select allOpen in new window




6

Drop Table only if it Exists


This one's better in MySQL than in MS SQL Server also
Again, there's just an If Exists at the end of the DDL statement.

DROP TABLE IF EXISTS tablename; 
                                    
1:

Select allOpen in new window




7

Add a column only if it does not exist


This one's clunky! YUCK!!!
Because the IF statement is only available within procedures and functions, you have to have a procedure that does this. The solution is here:
http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm



8

Drop a column only if it exists


Clunky just like the above:

delimiter '//' ;
create procedure DropColumnIfExists(
	IN dbName tinytext,
	IN tableName tinytext,
	IN fieldName tinytext)
begin
	IF EXISTS (
		SELECT * FROM information_schema.COLUMNS
		WHERE column_name=fieldName
		and table_name=tableName
		and table_schema=dbName
		)
	THEN
		set @ddl=CONCAT('ALTER TABLE ',dbName,'.',tableName,
			' DROP ',fieldName);
		prepare stmt from @ddl;
		execute stmt;
	END IF;
end;
//
delimiter ';' ;
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window




I hope these help ... and if you'll send  me some more tips, I'll add them.  I'll probably add some more anyway as I keep working in MySQL.
Asked On
2009-09-14 at 11:21:37ID1522
Tags

MySQL

,

DDL

,

Create Table

Topic

MySQL Server

Views
869

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top MySQL Server Experts

  1. johanntagle

    286,814

    Guru

    6,000 points yesterday

    Profile
    Rank: Sage
  2. Ray_Paseur

    216,557

    Guru

    0 points yesterday

    Profile
    Rank: Savant
  3. DaveBaldwin

    119,595

    Master

    1,400 points yesterday

    Profile
    Rank: Genius
  4. angelIII

    61,340

    Master

    0 points yesterday

    Profile
    Rank: Elite
  5. mwvisa1

    57,185

    Master

    30 points yesterday

    Profile
    Rank: Genius
  6. HainKurt

    41,850

    0 points yesterday

    Profile
    Rank: Genius
  7. ralmada

    39,250

    0 points yesterday

    Profile
    Rank: Genius
  8. Roads_Roads

    33,080

    0 points yesterday

    Profile
    Rank: Genius
  9. arnold

    29,812

    0 points yesterday

    Profile
    Rank: Genius
  10. theGhost_k8

    29,785

    0 points yesterday

    Profile
    Rank: Sage
  11. Kdo

    29,682

    0 points yesterday

    Profile
    Rank: Genius
  12. bportlock

    26,604

    0 points yesterday

    Profile
    Rank: Genius
  13. jason1178

    23,574

    0 points yesterday

    Profile
    Rank: Genius
  14. maeltar

    23,236

    0 points yesterday

    Profile
    Rank: Guru
  15. StingRaY

    21,500

    0 points yesterday

    Profile
    Rank: Wizard
  16. smadeira

    19,968

    0 points yesterday

    Profile
    Rank: Wizard
  17. fundacionrts

    18,200

    0 points yesterday

    Profile
    Rank: Master
  18. gr8gonzo

    17,019

    0 points yesterday

    Profile
    Rank: Sage
  19. ChrisStanyon

    16,964

    0 points yesterday

    Profile
    Rank: Sage
  20. pratima_mcs

    16,614

    0 points yesterday

    Profile
    Rank: Genius
  21. TempDBA

    16,400

    0 points yesterday

    Profile
    Rank: Sage
  22. Sharath_123

    16,268

    0 points yesterday

    Profile
    Rank: Genius
  23. for_yan

    16,000

    0 points yesterday

    Profile
    Rank: Genius
  24. matthewspatrick

    15,800

    0 points yesterday

    Profile
    Rank: Savant
  25. AielloJ

    13,732

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame