简体   繁体   中英

How do I create this database?

I am trying to develop an English app, but I am struggling with the database, which obviously I am not good at it.

If someone could give me some light I would be grateful.

There it is:

The first app idea is easy, the application consists in the student register, puting a code which automatically move him to certain room occurring to his english level with everyone else, the room has a professor of course and the level uses a specific book where it has many chapter and exercises according to the chapters.

My question is how do I build a data base where the room has a book which has many chapter that have many exercises....

Don't know if that is clear or not... sorry I getting crazy!

Thanks for your time!!!

The first step is to design the database , which would start with identifying the sets of data which would translate into tables a table containg a list of grouped values.

So you have identified Rooms, Students?, Books, Chapters and Excercises and Professors. These would likely be candidates for tables.

A table consists of rows each having the same number of columns. A column will hold the same sort of data eg The room name.

The Room table, sounds like it probably has little information a number or name for the room. To make things easy/effecicient as for all tables so far mentioned, it may be best to use an id entifier to easily and efficiently identify the room when relating it to other tables.

So your Room table could be created with SQL along the lines of :-

CREATE TABLE IF NOT EXISTS room (_id INTEGER PRIMARY KEY, roomName TEXT UNIQUE);

Students and Professors as these are both people and will probably share much of the same information then both could both be in the same table, perhaps Poeple with amongst other data, such sa name email etc, a column to indicate wether the person is a Student or a Professor or perhaps another sort of person. You mentioned level (this could be used to differentitate a professor eg if levels were 1-10 for Enlgish then if the level were greater than 10 then it could indicate a professor).

So your Person table could be something like :-

CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY, personName TEXT, personEmail TEXT, personTypeIndicator INTEGER);
  • Note that the assumpion is that personTypeIndicator is 1-10 for students and say 100 for a Professor.

Books,Chapters and Excercises

CREATE TABLE IF NOT EXISTS book (_id INTEGER PRIMARY KEY, bookName TEXT, room_reference INTEGER NOT NULL REFERENCES room(_id) ON DELETE CASCADE ON UPDATE CASCADE);
CREATE TABLE IF NOT EXISTS chapter (_id INTEGER PRIMARY KEY, chapterName TEXT, bookReference INTEGER NOT NULL REFERENCES book(_id) ON DELETE CASCADE ON UPDATE CASCADE);
CREATE TABLE IF NOT EXISTS excercise (_id INTEGER PRIMARY KEY, excerciseName TEXT, chapter_reference INTEGER NOT NULL REFERENCES chapter(_id) ON DELETE CASCADE ON UPDATE CASCADE);

As you can see there are some commonalities throughout the tables.

  • _id this name has been used as it can be useful for Android.
  • INTEGER PRIMARY KEY this not only makes the column the primary key (has to be unique and is automatically known as an index) but in SQLite it makes the column (only one such column per table) an alias of the rowid column (the most efficient index) but also allows SQLite to assign a unique value (per table) to each row in the table.
  • REFERENCES table(column) this creates a FOREIGN KEY saying that a value in the column must match a value in the parent (referenced) table(column)
    • Due to a bug in SQLite NULL (nothing) is considered different to another NULL so the NOT NULL is used to ensure that you cannot reference NULL.
    • ON DELETE CASCADE and ON UPDATE CASCADE , if a parent is UPDATE or DELETED then all the child rows are UPDATE or DELETED respectively. This can simply maintenance (aka not orphan children).

Before writing any code it would be wise to test the above structure with some data. You might want to download an SQlite Management Tool such as DB Browser for SQLite, Navicat, DB Beaver and more. With such tools you can create tables, load them with data extract data and so on.

So with your favourite SQLite Management tool you could create the tables using SQL above and then load some data using :-

INSERT INTO room (roomName) 
    VALUES ('Room1') /* _id = 1 */,('Room2') /* _id = 2 */,
        ('Room3') /* _id = 3 and so on */,
        ('Room4'),('Room5'),('Room6'),('Room7'),
        ('Room8'),('Room9'),('Room10'),('Roomxx')
;
INSERT INTO person (personName, personEmail, personTypeIndicator)
    VALUES ('Fred','f@mail.x',0) /* _id = 1 */,
        ('Mary','m@mail.x',0) /* _id = 2 */,
        ('Jane','j@mail.x',0) /* _id = 3 and so on */,
        ('Bert','b@mail.x',0),('Prof Plum','pp@profmail.x',100)
;
INSERT INTO book (bookname,room_reference)
    VALUES ('Book1',1),('Book2',2),('Book3',3),('Book4',4),('Book5',5),
    ('Book6',6),('Some other Book',7),('Book8',8),('Book9',9),('Book10',10);
;
  • 11 rooms rows are inserted, 4 people are addded and 10 books are added each referencing (relating to) a room.

You can then use :-

SELECT * FROM room;
SELECT * FROM person;
SELECT * FROM book;

To view the data (not shown for brevity).

Now there is some data we could do try to find out the students in a room based on the personTypeIndicator (0 (no room) and 1-10 (the respective room)) using for brevity/simplicity the _id of the room as the indicator of the room # (you would normally do this, as it's wrong to make assumptions about the actual value of an _id ).

The following SQL will do this :-

SELECT * FROM book JOIN room ON room._id = book.room_reference LEFT JOIN person ON personTypeIndicator = book._id;

The result would be :-

在此处输入图片说明

  • as excpected, as all students were added with the personTypeIndicator as 0 and there are no rooms with an _id of 0.

To test the above query and also show an example of updating data the following could be used :-

UPDATE person SET personTypeIndicator = _id WHERE personTypeIndicator <= 10;
  • Note that this mimics the Students being upgraded by using their _id (1-4) to update the personTypeIndicator but only if the personTypeIndicator is 10 or less (so Prof Plum is not changed).

If the query is now run then :-

在此处输入图片说明

If the above suits then you should compete desiging the database, along with queries. You could then progress to implementing the design in an App.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM