Jun 242011
 

EXEC sp_MSforeachtable @command1 = “DROP TABLE ?”

This is a hidden SP in sql server, this will be executed for each table in the database you connected (you cant rollback this)
If u want to delete it from the command prompt try this

EXEC xp_cmdshell ‘SQLCMD -U <user> -P <password> -Q ‘EXEC sp_MSforeachtable @command1 = “DROP TABLE ?” ‘ ,no_output

Delete/truncate all the Data from each table for in the database you connected

EXEC sp_MSforeachtable @command1 = “DELETE FROM ?”
EXEC sp_MSforeachtable @command1 = “TRUNCATE TABLE ?”

I too explain it now, as sp_MSforeachtable is Stored Procedure, that will execute for all the tables for database & @command1  is variable which will run against each table for connected database, now whatever you will write in the double quotes, that will be act as a command for each table, where ‘?’ is the name of the table.
try this, it will clear your comcepts

EXEC sp_MSforeachtable @command1 = “SELECT * FROM ?”  — Selects all the rows form all the table
EXEC sp_MSforeachtable @command1 = “PRINT ‘?'” –Just print the tables names with owner(dbo)

For more understanding, go for the MSDN or google, this is the right way.

Link to source

Nov 102010
 

CREATE TABLE examples

Basic CREATE TABLE statement

A very basic CREATE TABLE statement which should work in any SQL database:

 mysql> CREATE TABLE example ( id INT, data VARCHAR(100) ); Query OK, 0 rows affected (0.03 sec)

Creating a table with a particular storage engine

MySQL provides a variety of different table types with differing levels of functionality. The usual default, and most widely used, is MyISAM. Other storage types must be explicitly defined:

 mysql> CREATE TABLE example_innodb ( id INT, data VARCHAR(100) ) TYPE=innodb; Query OK, 0 rows affected (0.03 sec)

Note that beginning with MySQL 4.1 ENGINE=innodb is the preferred method of defining the storage type.

Use SHOW CREATE TABLE (see below) to check that MySQL has created the table as you defined it.

Creating a table with auto_increment

Often you’ll want to be able to automatically assign a sequential value to a column:

 mysql> CREATE TABLE example_autoincrement ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100) ); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO example_autoincrement (data) -> VALUES ('Hello world'); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM example_autoincrement; +----+-------------+ | id | data | +----+-------------+ | 1 | Hello world | +----+-------------+ 1 row in set (0.01 sec)

Creating a table with the current timestamp

Often it’s useful to have an automatic timestamp on each record. The MySQL special datatype TIMESTAMP enables you to keep track of changes to a record:

 mysql> CREATE TABLE example_timestamp ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100), cur_timestamp TIMESTAMP(8) ); Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO example_timestamp (data) VALUES ('The time of creation is:'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM example_timestamp; +----+--------------------------+---------------------+ | id | data | cur_timestamp | +----+--------------------------+---------------------+ | 1 | The time of creation is: | 2004-12-01 20:37:22 | +----+--------------------------+---------------------+ 1 row in set (0.00 sec) mysql> UPDATE example_timestamp SET data='The current timestamp is: ' WHERE id=1; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM example_timestamp; +----+---------------------------+---------------------+ | id | data | cur_timestamp | +----+---------------------------+---------------------+ | 1 | The current timestamp is: | 2004-12-01 20:38:55 | +----+---------------------------+---------------------+ 1 row in set (0.01 sec)

The column cur_timestamp is automagically updated every time the record is changed.

Creating a table with TIMESTAMP DEFAULT NOW()

MySQL supports the construct TIMESTAMP DEFAULT NOW() only from verson 4.1:

 CREATE TABLE example_default_now ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, data VARCHAR(100), created TIMESTAMP DEFAULT NOW() );

In this case the column created retains its initial value and is not changed during subsequent updates.

For versions prior to 4.1, the only workaround is to create two timestamp columns in a table, and explicitly set the second one when inserting the record. Remember: the first TIMESTAMP will be automagically updated on each record update.

Viewing a table definition

For basic information on table columns, use DESC tablename:

 mysql> DESC example; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | data | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)

Exact definition of the table:

 mysql> SHOW CREATE TABLE example; +---------+------------------------------------------------+ | Table | Create Table | +---------+------------------------------------------------+ | example | CREATE TABLE `example` ( `id` int(11) default NULL, `data` varchar(100) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +---------+------------------------------------------------+ 1 row in set (0.00 sec)

Copy a table example:
You can “show create table tablename” to get the exact commands to create a duplicate table. Change the duplicate tablename to something different. The example below creates a copy of a table named tasktracker.


 CREATE  TABLE  `robdba3`.`tasktracker_copy` (  
 `id` int( 11  )  NOT  NULL  auto_increment ,
 `tasklist` varchar( 1000  )  NOT  NULL ,
 `cur_dttm` timestamp NOT  NULL  default CURRENT_TIMESTAMP  
on  update  CURRENT_TIMESTAMP ,
 `utc_dttm` datetime NOT  NULL ,
 PRIMARY  KEY (  `id`  )  ) ENGINE  =  MyISAM  DEFAULT CHARSET  = utf8;

INSERT INTO `robdba3`.`tasktracker_copy` SELECT * 
FROM `robdba3`.`tasktracker`;