Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 9:02 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Step-5- in SQl-server2000
PostPosted: Sat Apr 07, 2007 7:21 pm 
Offline
Newbie
User avatar

Joined: Thu Apr 05, 2007 9:57 pm
Posts: 17
Has thanked: 0 time
Have thanks: 0 time

DDL Statements(Advanced)


1. Adding Constraints

Example 1: ( Adding Constraint(s) in Table Creation Time )

Create a table called DEPT with DEPT_ID as primary key and DEPT_NAME must be a value from IS , CS , IT or DS

Code:
CREATE TABLE DEPT
(
  DEPT_ID int Primary Key,
  DEPT_NAME varchar(2) Unique,
  DESCRIPTION varchar(100) Not Null,
  Constraint DEPT_NAME_CK Check (DEPT_NAME in ('CS','IT','IS','DS'))
);


BETTER STATEMENT THAT DOES THE SAME JOB OF THE PREVIOUS ONE :

Code:
CREATE TABLE DEPT
(
  DEPT_ID int,
  DEPT_NAME varchar(2),
  DESCRIPTION varchar(100) Constraint DESCRIPTION_NN Not Null,   
  Constraint DEPT_ID_PK Primary Key (DEPT_ID),                  
  Constraint DEPT_NAME_UK Unique (DEPT_NAME),                 
  Constraint DEPT_NAME_CK Check (DEPT_NAME in ('CS','IT','IS','DS'))       
);


Example 2: ( Adding Foreign Key Constraint(s) in Table Creation Time )

Create a table called STUDENT with STUD_ID as primary key and STUD_NAME that canط·آ£ط¢آ¢ط£آ¢أ¢â‚¬ع‘ط¢آ¬ط£آ¢أ¢â‚¬â€چط¢آ¢t accept null values and a DEPT_ID column as a forign key to DEPT_ID in DEPT table

Code:
CREATE TABLE STUDENT
(
  STUD_ID int Primary Key,
  STUD_NAME varchar(30) Not Null,
  DEPT_ID INT,
  Constraint STUDENT_DEPT_ID_FK Foreign Key (DEPT_ID) References
  DEPT (DEPT_ID)              
);



Example 3: ( Adding A Constraint After Table Creation Time )

Add a unique constraint on STUD_NAME column after creating table STUDENT as in the previous example


Code:
ALTER TABLE STUDENT
ADD CONSTRAINT STUD_NAME_UK UNIQUE ( STUD_NAME);



2.Dropping Constraints

Example : ( Removing a constraint by its name )

Remove the constraint called STUD_NAME_UK from STUDENT table

Code:
ALTER TABLE STUDENT
DROP CONSTRAINT STUD_NAME_UK;



3.Adding Indexes

Example :

Add an index on column STUD_NAME IN STUDENT table

Code:
CREATE INDEX STUD_NAME_INDEX
ON STUDENT(STUD_NAME);



4.Dropping Indexes

Example : ( Removing an index by its name )

Remove the index called STUD_NAME_INDEX from STUDENT table
Code:
DROP INDEX STUDENT.STUD_NAME_INDEX;



5.Creating Views

Example 1:

Create a view called SALES_RER that conatins all information of employees working as sales representatives

Code:
CREATE VIEW SALES_REP AS
SELECT *
FROM employees
WHERE Title  = 'Sales Representative'



Example 2:

Create a view called SUPP_FOR_PROD that contains the names, prices and supplier names for each product


Code:
CREATE VIEW SUPP_FOR_PROD AS
SELECT  Products.ProductName,Suppliers.ContactName,Products.UnitPrice
FROM Products, Suppliers
WHERE Products.SupplierID = Suppliers.SupplierID



6.Dropping Views

Example : ( Removing a view by its name )

Remove the view called SUPP_FOR_PROD

Code:
DROP VIEW SUPP_FOR_PROD;


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 unit step and impulse function L.T  General Discussion  loomy  0
 (MS Press) Microsoft Visual CSharp 2008 - Step By Step  offers  Casper  0
 Cisco - Press - Configuring CallManager and Unity A Step-by-  offers  Casper  0
 TCP IP First Step  offers  Casper  0
 Cisco Press - Routing First-Step 2004  offers  Casper  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team