Return to Snippet

Revision: 22079
at December 31, 2009 10:55 by CDIDevs


Initial Code
-- create/drop tables
CREATE TABLE t_test
 (a INTEGER PRIMARY KEY
 ,b VARCHAR(10)
 )

	-- ...with identity
	CREATE TABLE t_test(
	 id INTEGER IDENTITY
		  PRIMARY KEY,
	 name VARCHAR(10)
	 );
	INSERT INTO t_test(name) VALUES ('Andrew');
	INSERT INTO t_test(name) VALUES ('Gordon');
	SELECT * FROM t_test;

	
	-- ... with pkey
	CREATE TABLE track(
	  album CHAR(10) NOT NULL,
	  dsk INTEGER NOT NULL,
	  posn INTEGER NOT NULL,
	  song VARCHAR(255),
	  PRIMARY KEY (album, dsk, posn)
	)

	-- ... with fkey
	CREATE TABLE album(
	  id CHAR(10) NOT NULL PRIMARY KEY,
	  title VARCHAR(100),
	  artist VARCHAR(100)
	);

	CREATE TABLE track(
	  album CHAR(10),
	  dsk INTEGER,
	  posn INTEGER,
	  song VARCHAR(255),
	  FOREIGN KEY (album) REFERENCES album(id)
	);


DROP TABLE t_test

-- add/remove columns
ALTER TABLE a DROP COLUMN y 
ALTER TABLE a ADD z INTEGER

Initial URL
http://sqlzoo.net/howto/source/z.dir/tip550066/sqlserver

Initial Description

                                

Initial Title
Common SQL Commands (create table, etc)

Initial Tags
update

Initial Language
SQL