1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Database Class Help

Discussion in 'BBS Hangout' started by Sajan, Mar 2, 2013.

  1. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    [​IMG]

    So I have to create a database with 5 entities and 5 attributes each with only 1:M relationships.

    My database is about managing a nutrition store inventory. What do yall think?
     
  2. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    sorry I missed a relation in the first ERD.

    [​IMG]
     
  3. DarkHorse

    DarkHorse Member

    Joined:
    Oct 9, 1999
    Messages:
    6,756
    Likes Received:
    1,303
    Seems good. Just looking for confirmation?


    I do have to admit I don't totally understand your inventory table. Is each meant to represent a single item? What if you have more than one of the same kind of item at a store?
     
    1 person likes this.
  4. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    so the idea is that one store can have many products, and multiple quantities of that product. also, product x can be sold at 0 stores, or many stores.

    we can't have M:N relationships, so i tried to break it down into two 1:M relations.

    i am not sure if i did that right though.
     
  5. B

    B Member

    Joined:
    Jul 19, 2001
    Messages:
    1,901
    Likes Received:
    24
    Second diagram looks good to me. If you are looking for a challenge, you could have a customer purchase history table, but that would end up being a M:N relationship.
     
    1 person likes this.
  6. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,511
    Likes Received:
    59,008
    That's solid. Don't ever consider m:m. You did it right

    A store does not have a product and a product does not have a store. By definition. So you correctly slid a mapping table in the middle called inventory where you match products to stores

    This will work
     
    1 person likes this.
  7. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    alright so i got another assignment due. it's my first time writing DDL thingies.

    what am I doing wrong with this? isqlplus won't create the tables.

    Code:
    create table school (
    school_code	numeric,
    school_name 	varchar (20),
    CONSTRAINT PK_SCHOOL PRIMARY KEY (school_code),
    CONSTRAINT FK_SCHOOL_PROFESSOR FOREIGN KEY (PROF_NUM) references PROFESSOR (PROF_NUM)
    );
    
    create table professor (
    prof_num 	numeric, 
    prof_specialty 	varchar (20),
    prof_rank 	varchar (20),
    prof_lname 	varchar (20),
    prof_fname 	varchar (20),
    prof_initial 	char (1),
    prof_email 	varchar (30),
    CONSTRAINT PK_PROFESSOR PRIMARY KEY (prof_num),
    CONSTRAINT FK_PROFESSOR_DEPARTMENT FOREIGN KEY (DEPT_CODE) references DEPARTMENT (DEPT_CODE)
    );
    
    create table department (
    dept_code 	numeric,
    dept_name	varchar(20),
    CONSTRAINT PK_DEPARTMENT PRIMARY KEY (DEPT_CODE),
    CONSTRAINT FK_DEPARTMENT_SCHOOL FOREIGN KEY (SCHOOL_CODE) references SCHOOL (SCHOOL_CODE),
    CONSTRAINT FK_DEPARTMENT_PROFESSOR FOREIGN KEY (PROF_NUM) references PROFESSOR (PROF_NUM)
    );
    
    CREATE TABLE STUDENT (
    stu_num		numeric,
    stu_lname 	varchar (20),
    stu_fname 	varchar (20),
    stu_initial 	char (1),
    stu_email 	varchar (30),
    constraint PK_student primary key(stu_num),
    constraint FK_student_department foreign key (dept_code) references department (dept_code),
    constraint FK_student_professor foreign key (prof_num) references professor (prof_num)
    );
    
    create table course (
    crs_code 	numeric,
    crs_title	varchar(20),
    crs_description varchar(20),
    crs_credit	varchar(20),
    constraint PK_course	primary key (crs_code),
    constraint fk_course_department foreign key (dept_code) references department (dept_code)
    );
    
    create table class (
    class_code 	numeric,
    class_section	varchar(20),
    class_time	varchar(20),
    constraint pk_class primary key (class_code),
    constraint fk_class_course foreign key (crs_code) references course (crs_code),
    constraint fk_class_professor foreign key (prof_num) references  professor (prof_num),
    constraint fk_class_room foreign key (room_code) references room(room_code)
    );
    
    create table enroll (
    enroll_date 	date,
    enroll grade	char(1),
    constraint pk_enroll primary key(class_code,stu_num),
    constraint fk_enroll_class foreign key(class_code) references class(class_code),
    constraint fk_enroll_student foreign key(stu_num) references student(stu_num)
    );
    
    create table room (
    room_code 	numeric,
    room_type	varchar(20),
    constraint PK_room Primary Key (room_code),
    constraint FK_room_building Foreign Key (bldg_code) references Building (bldg_code)
    );
    
    create table building (
    bldg_code	numeric,
    bldg_name	varchar(20),
    bldg_location	varchar(20),
    constraint PK_building primary key (bldg_code)
    );
    
    this is the error i get
    Code:
    ERROR at line 1: 
    ORA-00942: table or view does not exist 
    CONSTRAINT FK_SCHOOL_PROFESSOR FOREIGN KEY (PROF_NUM) references PROFESSOR (PROF_NUM)
                                                *
    ERROR at line 5: 
    ORA-00904: "PROF_NUM": invalid identifier 
    CONSTRAINT FK_PROFESSOR_DEPARTMENT FOREIGN KEY (DEPT_CODE) references DEPARTMENT (DEPT_CODE)
                                                    *
    ERROR at line 10: 
    ORA-00904: "DEPT_CODE": invalid identifier 
    CONSTRAINT FK_DEPARTMENT_SCHOOL FOREIGN KEY (SCHOOL_CODE) references SCHOOL (SCHOOL_CODE),
                                                 *
    ERROR at line 5: 
    ORA-00904: "SCHOOL_CODE": invalid identifier 
    constraint FK_student_department foreign key (dept_code) references department (dept_code),
                                                  *
    ERROR at line 8: 
    ORA-00904: "DEPT_CODE": invalid identifier 
    constraint fk_course_department foreign key (dept_code) references department (dept_code)
                                                 *
    ERROR at line 7: 
    ORA-00904: "DEPT_CODE": invalid identifier 
    constraint fk_class_course foreign key (crs_code) references course (crs_code),
                                            *
    ERROR at line 6: 
    ORA-00904: "CRS_CODE": invalid identifier 
    enroll grade	char(1),
                	*
    ERROR at line 3: 
    ORA-00907: missing right parenthesis 
    constraint FK_room_building Foreign Key (bldg_code) references Building (bldg_code)
    
    thanks/reps.
     
  8. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    :confused: "enroll grade"? Didn't you mean "enroll_grade"? Also, "Building" might have to be with a lowercase.

    Everything else will build out with minor errors. :eek:

    I think this could have gone in the "proofread my text" thread. ;)

    Use Notepad++ and it will know you're using .sql code.
     
    1 person likes this.
  9. arkoe

    arkoe (ง'̀-'́)ง

    Joined:
    Dec 13, 2001
    Messages:
    10,387
    Likes Received:
    1,598
    You're trying to point foreign keys to tables that don't exist yet, i.e. the SCHOOL table references PROFESSOR... but PROFESSOR gets created after SCHOOL.

    If it's possible, order your scripts in such a way that the referenced tables were created before the script runs. If that's not possible, add the foreign key constraints on in separate commands after all referenced objects are created.
     
  10. Air Langhi

    Air Langhi Contributing Member

    Joined:
    Aug 26, 2000
    Messages:
    21,943
    Likes Received:
    6,696
    Create the tables first and then then alter them to create the constraints.
     
    1 person likes this.
  11. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    thanks for the notepad++ suggestion. I am using it now.

    i figured this is my issue..i don't know how to though. haha. which table would come first? it seems like they are all referencing some other table.
     
  12. arkoe

    arkoe (ง'̀-'́)ง

    Joined:
    Dec 13, 2001
    Messages:
    10,387
    Likes Received:
    1,598
  13. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    Got it to work.

    Code:
    create table building (
    bldg_code	numeric,
    bldg_name	varchar(20),
    bldg_location	varchar(20),
    constraint PK_building primary key (bldg_code)
    );
    
    create table room (
    room_code 	numeric,
    bldg_code	numeric,
    room_type	varchar(20),
    constraint PK_room Primary Key (room_code),
    constraint FK_room_building Foreign Key (bldg_code) references building (bldg_code)
    );
    
    create table school (
    school_code 	numeric,
    school_name 	varchar(20),
    prof_num	numeric,
    CONSTRAINT PK_SCHOOL PRIMARY KEY (school_code)
    );
    
    create table professor (
    prof_num 	numeric, 
    dept_code	numeric,
    prof_specialty 	varchar(20),
    prof_rank 	varchar(20),
    prof_lname 	varchar(20),
    prof_fname 	varchar(20),
    prof_initial 	char(1),
    prof_email 	varchar(30),
    CONSTRAINT PK_PROFESSOR PRIMARY KEY (prof_num)
    );
    
    create table department (
    dept_code 	numeric,
    dept_name	varchar(20),
    school_code	numeric,
    prof_num	numeric,
    CONSTRAINT PK_DEPARTMENT PRIMARY KEY (dept_code),
    CONSTRAINT FK_DEPARTMENT_SCHOOL FOREIGN KEY (SCHOOL_CODE) references SCHOOL (SCHOOL_CODE),
    CONSTRAINT FK_DEPARTMENT_PROFESSOR FOREIGN KEY (PROF_NUM) references PROFESSOR (PROF_NUM)
    );
    
    ALTER table school
    	ADD CONSTRAINT FK_School_Professor
    	FOREIGN KEY (PROF_NUM)
    	REFERENCES PROFESSOR (PROF_NUM);
    
    ALTER table professor
    	ADD CONSTRAINT FK_Professor_Department
    	FOREIGN KEY (dept_code) 
    	REFERENCES DEPARTMENT (dept_code);
    
    create table course (
    crs_code 	numeric,
    dept_code	numeric,
    crs_title	varchar(20),
    crs_description varchar(20),
    crs_credit	varchar(20),
    constraint PK_course	primary key (crs_code),
    constraint FK_course_department foreign key (dept_code) references department (dept_code)
    );
    
    CREATE TABLE STUDENT (
    stu_num		numeric,
    stu_lname 	varchar(20),
    stu_fname 	varchar(20),
    stu_initial 	char(1),
    stu_email 	varchar(30),
    prof_num	numeric,
    dept_code	numeric,
    constraint PK_student primary key(stu_num),
    constraint FK_student_department foreign key (dept_code) references department (dept_code),
    constraint FK_student_professor foreign key (prof_num) references professor (prof_num)
    );
    
    create table class (
    class_code 	numeric,
    class_section	varchar(20),
    class_time	varchar(20),
    crs_code numeric,
    prof_num numeric,
    room_code numeric,
    constraint pk_class primary key (class_code),
    constraint fk_class_course foreign key (crs_code) references course (crs_code),
    constraint fk_class_professor foreign key (prof_num) references  professor (prof_num),
    constraint fk_class_room foreign key (room_code) references room (room_code)
    );
    
    create table enroll (
    class_code	numeric,
    stu_num	numeric,
    enroll_date 	date,
    enroll_grade	char(1),
    constraint pk_enroll primary key(class_code,stu_num),
    constraint fk_enroll_class foreign key(class_code) references class (class_code),
    constraint fk_enroll_student foreign key(stu_num) references student(stu_num)
    );

    what's the best way to drop all those tables? I tried this but of course the bolded tables won't drop because of the foreign keys. Is there a shortcut? ;)
    drop table enroll;
    drop table class;
    drop table student;
    drop table course;
    drop table department;
    drop table professor;
    drop table school;

    drop table room;
    drop table building;
     
  14. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    That's what SHE was happy about. :eek:
     
  15. arkoe

    arkoe (ง'̀-'́)ง

    Joined:
    Dec 13, 2001
    Messages:
    10,387
    Likes Received:
    1,598
    You can individually drop the constraints before dropping the tables. The syntax for dropping them is at the bottom of that W3 Schools link.
     
    1 person likes this.
  16. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    ok i dont know what i am doing anymore. lol.

    here's a case study i have to do and what i mocked up so far. any help would be appreciated.

    [​IMG]

    [​IMG]
     
  17. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    LOL! ^ I love these kinds of problems. That's what we solve in IT. :)

    Students A
    Teachers B
    Teachers that are students C
    I'm just kidding, I don't know. :p
    :cool: Something like that. Good luck, man.
     
  18. Supermac34

    Supermac34 President, Von Wafer Fan Club

    Joined:
    Mar 31, 2000
    Messages:
    7,110
    Likes Received:
    2,457
    Get good at this stuff. I never have enough good data warehousing people to hire.
     
  19. Sajan

    Sajan Member

    Joined:
    Apr 18, 2009
    Messages:
    9,320
    Likes Received:
    7,112
    does this ERD have any obvious mistakes?
    [​IMG]
     

Share This Page