If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Enhancement SQL Help,

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-10, 08:44
cork24 cork24 is offline
Registered User
 
Join Date: Nov 2010
Posts: 3
Enhancement SQL Help,

I am trying to Create tables, with Enhancements

Code:
CREATE TABLE Employee
             (empNo  	VARCHAR(4)    NOT NULL,
              fName	VARCHAR(25),
              lName	VARCHAR(20),
		    address VARCHAR(45),
			DOB smallint,
               Sex char NOT NULL
                   CHECK(sex IN('M','F')),
               position VARCHAR NOT NULL
CHECK(position IN('MAanger', 'Team Leader', 'Analyst', 'Software Developer')),
               deptNo VARCHAR(10),
            
	     
PRIMARY KEY (EmpNo));

CREATE TABLE Department

             (deptNo   VARCHAR(10)	NOT NULL,
	      deptName	VARCHAR(50),
	      mgrEmpNo VARCHAR(35),
              
             
PRIMARY KEY (deptNo));


CREATE TABLE Project

             (projNo  VARCHAR(24)	NOT NULL,
	      projName	VARCHAR(40),
	      deptNo	VARCHAR(10),
                        
PRIMARY KEY (projNo));


CREATE TABLE WorksOn

             (empNo  		VARCHAR(4)	NOT NULL,
	      projNo	VARCHAR(10),
	      dateWorked	dateworked,
           hoursWorked VARCHAR(10),
                       
PRIMARY KEY (empNo, projNo, dateWorked));

Commit;
Is this Right to only have the user Enter either M or F for Sex and for Work position only to have the only following
Reply With Quote
  #2 (permalink)  
Old 11-27-10, 10:42
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
1. you ~must~ supply a length for CHAR and VARCHAR columns
2. mysql doesn't support CHECK constraints

fix those and try again, and please show your error messages
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-27-10, 11:22
cork24 cork24 is offline
Registered User
 
Join Date: Nov 2010
Posts: 3
ok, if MySql does not support check constraints is their any other way to only allow users to enter M or F for Sex and Manager Team Leader etc. in SQL looking over google and cant seem to find anything,
Reply With Quote
  #4 (permalink)  
Old 11-27-10, 13:20
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by cork24 View Post
...is their any other way to only allow ...
absolutely, yes -- use a foreign key
Code:
CREATE TABLE Sexes
( Sex CHAR(1) NOT NULL PRIMARY KEY
);
INSERT INTO Sexes VALUES ('M'),('F')
;

CREATE TABLE Positions
( Position VARCHAR(37) NOT NULL PRIMARY KEY
);
INSERT INTO Positions VALUES 
 ('Manager')
,('Team Leader')
,('Analyst')
,('Software Developer')
;

CREATE TABLE Departments
( deptNo  VARCHAR(10) NOT NULL PRIMARY KEY
, deptName VARCHAR(50)
, mgrEmpNo VARCHAR(35)
);

CREATE TABLE Employee 
( empNo VARCHAR(4) NOT NULL PRIMARY KEY
, fName VARCHAR(25)
, lName VARCHAR(20)
, address VARCHAR(45)
, DOB DATE
, Sex CHAR(1) NOT NULL 
      REFERENCES Sexes ( Sex )
, position VARCHAR(37) NOT NULL
      REFERENCES Positions ( position )
, deptNo VARCHAR(10)
      REFERENCES Departments ( deptNo )
);
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-27-10, 14:16
cork24 cork24 is offline
Registered User
 
Join Date: Nov 2010
Posts: 3
Thank you will set this up. and take out the CREATE IN class.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On