# Database diagram with dbdiagram.io

Creating database diagrams before an actual database implementation is quite important because it reduces the time spent during implementation, it provides a blueprint for the developer to follow during implementation.

 ## WHAT IS A DATABASE DIAGRAM?
A database diagram is a graphical representation of the structure of a database and the relationship between database objects.
Database diagrams help you visualize the relationship between  the tables on your database therefore giving you a clearer picture of what you want to implement, Most times developers spend time restructuring their database because they never actually went through the database design phase, take for example a UI/UX designer who has to keep iterating until he/she is able to perfect the features for the product he/she is working to excel, so also do developers have to prioritize database schema designs.

### GETTING STARTED  WITH DBDIAGRAM.IO
1. Visit [dbdiagram.io](https://dbdiagram.io/home)
2. Click on  the **CREATE YOUR DIAGRAM** button
3. Navigate to screen that has the appearance of a code editor(left-hand side).
4. You can go through the already written structure to get a better overview before clearing the editor's screen.

#### CREATING YOUR FIRST DATABASE TABLE
1. Go to the cleared editor and type in<br>
 table **table's name**{<br>
}<br>, This tells dbdiagram.io that you and creating a table with the specified name
2. Within the curly braces type in the columns required for your database alongside the column's data type and metadata.<br>
Table user{<br>
Id int [pk, not null, increments]<br>
Username string[unique]<br>
Email string[unique]<br>
Password string [not null]<br>
Address string[null]<br>
}  <br>,The data placed within the square bracket gives more information about the given field.
For example:<br>
**Pk** means that the ID field is a primary key, A primary key is unique means of identification when we talk about database it's the data under a specific column that is unique, doesn't change and is not null, this means that that column cannot be empty for any given entity on your database.
**Not null** signifies that this column cannot be empty.
**Null** signifies that that column can be left empty(null) for a given entity.
**Unique** signifies that the data under that column must be unique within a given database.<br>
#### RELATIONSHIPS WITHIN A DATABASE.
Relationships within the database world means connection between two or more tables.
The following are some of the relationship type you can find while talking about databases:
1. **<**  One-To-Many
2.  **>**  Many-To-One
3.  **- **  One-To-One<br>
**One-To-Many**
One to many simply means that an entity on one table is connected to different entities on a different table.
For example:
A student from a table named student can be associated with different courses from the courses table but a single course cannot be connected to one student.,Imagine having a course created specifically for you to study in school 😂?.<br>
**Many-To-One**
The only difference between the Many-To-One and the One-To-Many relationship is that for the Many-To-One relationship many entities from the parent table are connected to a single entity on the child table while for the One-To-Many an entity on the parent table is connected to two or more entities on the child table, **what do I mean by parent table?**.
A parent table can be seen as a universal table, the table that births the child table, basically without the parent table the child table wouldn't exist.
Without creating a student or a teachers table the creation of the subjects table wouldn't be important, so the teachers/students table births the subjects table.<br>
**One-To-One**
A single element on the parent table is connected to a single element on the child table.<br>
###### CREATING RELATIONSHIPS BETWEEN TABLES USING  DBDIAGRAM.IO
the following are the steps to follow in creating relationships between tables using dbdiagram.io:
1. **Long-syntax**
Ref name-optional { table1.field1 < table2.field2 }

2. **Short-syntax**
Ref name-optional: table1.field < table2.field2

**Example**
Table students{<br>
Id int [pk, increments]<br>
name string [not null]<br>
}<br>
Table deparment{<br>
Id int[pk, increments] <br>
studentId string[unique, not null]<br>
}<br>
Ref students.name -department.studentId
<br>
This reads one name on the students table to one studentId on the department table<br>
I hope this article was helpful to you?, Thanks for reading.



