Unity and database

Keywords: Database Unity

Install database - connect your computer host - create database
1, Database design
1. How to represent the relationship between databases in database design?
relational model
2. What are the relationships between entity sets X and Y?
One to many, one to one, many to one, many to many
3. What are the functions of the three paradigms of database?
The goal of the first paradigm is to ensure the atomicity of each column
The second formula requires each table to describe one thing
If the first relationship satisfies 2NF, and columns other than primary keys do not pass dependent primary keys
4. Database design in the project development cycle
Demand analysis stage: analyze customers' business and data processing requirements
Outline design stage: design the E-R model diagram of the database to confirm the correctness and completeness of the demand information
Detailed design stage: apply three paradigms to audit the database structure
Code writing stage: physically implement the database and write the application
Software testing phase:
Installation deployment:
5 draw E-R diagram
The symbol rectangle is a solid
The symbol ellipse is an attribute
Square is a relationship
2, Addition, deletion, modification and query of database
1. Login database command: mysql -h (server host address (this machine can be omitted)) - u (user name) - p (password); Note that the content in brackets is the content required in the following brackets
2. Create database: create database name;
3. View all databases: show databases;
4. Access database: use database name// The entered database can only exist
5. Delete database: drop database database name;
Note: you can only modify and add the data in the table after entering the data
6. Create table: create table name (field 1 data type [field attribute] [constraint] [index],...);
Field constraints
Non NULL constraint not null field cannot be null
Default constraint default assigns a default value to a field
The unique constraint unique key setting field has a unique value. It is allowed to be empty, but only one can be empty
Primary key constraint primary key sets this field as the primary key of the table, which can uniquely identify the table record
The foreign key constraint is used to establish a relationship between two tables. You need to specify which field of the main table to reference
Auto grow auto_increment sets this column as a self incrementing field. By default, each column is self incrementing by 1. It is usually used to set the primary key
7. Check whether the table exists: use table name
8. View all tables: show tables;
9. View the parameters in the table: desc table name;
10 delete table: drop table name;
11. Insert data: insert into table name (field 1, field 2, field 3) value (data 1, data 2, data 3)
12. Modify data: update table name set field 1 = modified data where field = Data
Note: data matching after where is met will be modified
13. Delete data: delete from table name where field = Data
Note: only when the conditions after where are met can they be deleted
3. Use database under unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
public class Test2 : MonoBehaviour {

void Start () {
    Startdata();
}
void Startdata()
{
    
    string a = "server=localhost;database=student;userid=root;password=root";
    //Server = host name database = database name userid = username password = password
    //Create an object that connects to the database
    MySqlConnection con = new MySqlConnection(a);//Connection data
    //open a connection
    con.Open();
    //Specific statements for table operations
    string sql = "insert into xinxi(name,sex,age) value('drug','male',18)";//"insert into" table name (field 1, field 2, field 3) value (data 1, data 2, data 3);
    //Create operation object
    MySqlCommand com = new MySqlCommand(sql, con);//Execute the operation statement on the table
    if (com.ExecuteNonQuery() > 0)
    {
        print("success");
    }
    //Contents of query table
    string selec = "select *from xinxi";//Query statement
    MySqlCommand cad = new MySqlCommand(selec, con);
    MySqlDataReader reader = cad.ExecuteReader();
    while (reader.Read())
    {
        int id = reader.GetInt32("id");
        string name = reader.GetString("name");
        string sex = reader.GetString("sex");
        print(id + "\t" + name + "\t" + sex);
    }
    reader.Close();
    con.Close();
}

}

Posted by kobayashi_one on Tue, 09 Nov 2021 14:03:07 -0800