Monday, February 4, 2013

Creating and altering tables

Use CommunityAssist

Insert into Person(lastName, firstname)
Values ('Isabella', 'Queen')

insert into PersonContact(ContactInfo, PersonKey, ContactTypeKey)
Values('queen@gmail.com', Ident_Current('Person'), 6)

Select * from Person

Select * From PersonContact

Use Master;

Create Database Music

Use Music

Create table Album
(
 AlbumKey int identity(1,1) primary key,
 AlbumTitle nvarchar(255) not null,
 AlbumDate Date,
 AlbumPrice Decimal(5,2) not null default 1, 
 AlbumArt varbinary(max) 
)

Create Table Track
(
 TrackKey int identity not null,
 TrackTitle nvarchar(255) not null,
 TrackLength decimal(5,2),
 AlbumKey int not null,
 Constraint PK_Track Primary key(TrackKey),
 Constraint FK_Album Foreign Key (AlbumKey)
  references Album(AlbumKey)
)

Create table Artist
(
 Artistkey int identity(1,1) not null,
 Artistname nvarchar(255) not null,
 ArtistInfo xml 
)

Alter table Artist
Add constraint PK_Artist Primary key(ArtistKey)

Alter Table Album
Drop constraint [PK__Album__554E540739505B7C]

Drop Table Album

Create table TrackArtist
(
 TrackArtistKey int identity(1,1) primary key,
 ArtistKey int Foreign Key references Artist(ArtistKey),
 Trackkey int not null
)

Alter table TrackArtist
Add Constraint FK_Track Foreign key (TrackKey)
 References Track(TrackKey)

Alter table TrackArtist
Add TrackDescription xml

Alter Table TrackArtist
Drop Column TrackDesription

Alter Table Album
Add Constraint unique_AlbumTitle unique(AlbumTitle)

Alter Table Album
Add Constraint album_PriceRange Check (AlbumPrice between 1 and 500)

Alter Table Section
Add Constraint chk_Quarter 
Check (Qtr in ('Fall', 'Winter', 'Spring', 'Summer'))

No comments:

Post a Comment