MySQL based learning 1

MySQL common DOS commands

How to enter MySQL

mysql -u root -p
Enter receive Enter password when prompted, enter your password, enter the MySQL command-line window command mode.
How to enter MySQL

Show all databases

After successfully entering the MySQL command mode, the command line window, type:
show databases;
This command displays all MySQL database has been created.
Show all databases

New Database

In the command window, enter:
create database <创建的数据库的名称>;
After you've created, you can use show databases;to check whether successfully created.
New Database

Delete Database

In the command line window, type:
drop database <删除的数据库名称>;
After deleting finished, can be used show databases;to check whether deleted successfully.
Delete Database

Access to the database

In the command window input:
use <数据库名称>;
When the command window output: Database changed , described open succeeded.
Access to the database

Display database contains current data table

In use use <数据库名称>;after entering the database.
In the command window input:
show tables;
When the command window is displayed Tables_in_ <database name> , a display is completed.
Display database contains current data table

Further database contains data tables

In the command window input:
show tables from <数据库名称>;
When the command window is displayed Tables_in_ <database name> , a display is completed.
Further database contains data tables

Displays the current database name

In the command line window, type:
select database();
When the command line window displays Database () , then he is the name of the database currently in use on the next line.
Displays the current database name

Check the status of MySQL

In the command line window, type:
status;
Check the status of MySQL
Note: MySQL default port number: 3306.


MySQL directory structure

table of Contents Features
bin Store executable file
docs Storage of documents and logs
include 存放头文件
lib 存放一系列库文件
share 存放字符集,语言信息
COPYING 版本许可协议
mydefault.ini 数据库使用的配置文件
Data 存放数据库里存的信息,数据库迁移时,拷贝这里面的文件夹

数据表结构

数据表

数据表是数据库最重要的组成部分之一,包括行和列。一个关系型数据库包括多个数据表。
如下图:

学号 姓名 性别 班级
196******1 王明 2
196******7 李红 2
196******6 李明 1
196******4 王红 1

一列(数据元素)包含了相同意义和相同类型的一组数据。又称为属性或者字段。字段包括字段名和字段值两部分,字段名是在创建表格结构时定义的,字段值是在表格定义完成之后添加的。如上图,学号,姓名,性别,班级都是字段名,而下面对应的数据则是字段值。

冗余字段

某一字段属于一个数据表,但是同时出现在别的数据表。
坏处:影响数值一致性,会加大内存影响空间。
好处:使用空间换取时间,提高响应速度。

主键

主键是一个字段或者多个字段且不能为空,其值能唯一地标识表中的每一行,通过它可强制表的实体完整性。 RDBMS还规定一个数据表中只能包含一个主键。

外键

外键用于关联两个数据表,在同一数据库当中,有的表是有关联的,所以为了降低数据冗余,经常用一种数据单独存在一个表中,查询的时候向多个表获得数据。


表关系外键

1对1

比如员工的工资,一个雇员只有一个工资,而一个工资只能属于一个雇员。则雇员和工资之间具有一对一关系。

1对n

学生与班级,一个班级有多名学生,但是一名学生只有一个班级,则学生与班级是1对n关系。

学号 姓名 性别 班级
196******1 王明 2
196******7 李红 2
196******6 李明 1
196******4 王红 1
班级 班主任
1 王老师
2 李老师

n对m

学生与课程,一名学生可以选择多门课程,一门课程也有多名学生,而遇到这种情况需要把n对m关系转化成连个1对n关系

学生表

学号 姓名 性别
196******1 王明
196******7 李红
196******6 李明
196******4 王红

学生课程成绩表

学号 课程编号 成绩
196******1 101 80
196******7 102 87
196******6 101 83
196******4 102 85

课程表

课程编号 课程名称
101 计算机基础
102 程序设计

如增加学生课程成绩表,则学生表和学生课程成绩表之间是一对多关系,课程表和学生课程成绩表之间也是一对多的关系。


数据库表的设计 E-R图

什么是E-R图?

E-R图也称实体-联系图,提供了表示实体类型、属性和联系的方法,用来描述现实世界的概念模型。

为什么要使用E-R图?

利用E-R模型,更好的模拟现实。

实体

矩形表示,矩形框内写明实体名。
实体是现实世界中各种事物的抽象,是数据的载体。关键在于一个实体能与另一个实体相区别,具有相同属性的实体具有相同的特征和性质。

属性

椭圆形表示,并用无向边将其与相应的实体连接起来。
实体所具有的某一特性,一个实体可由若干个属性来刻画。属性不能脱离实体,属性是相对实体而言的。

关系

菱形表示,菱形框内写明联系名,并用无向边分别与有关实体连接起来,同时在无向边旁标上关系的类型(1:1,1 : n 或 m : n)。
比如老师给学生授课存在授课关系,学生选课存在选课关系。

设计原则

  1. 一张表描述一个实体或实体之间的关系。
  2. 避免表之间存在重复字段。
  3. 表中的字段是最原始的基本数据元素。

实例

实体包括:

  1. 货仓表:货仓号,面积,电话号码。
  2. 职工表:职工号、姓名、年龄、职称。
  3. 学生表:学号,姓名,年龄。

三范式

1.原子性

Each column of a database table are not subdivided data units, the first normal form (1NF).

2. The non-primary property dependent

If a relationship 1NF satisfied, and in addition to other than the primary key column is fully dependent on the primary key, he meets a second paradigm (2NF) (paradigm requires that each second table describes only one thing)

3. transitive dependencies

2NF must be met, and requires that each row of the table is directly related only indirectly related to the primary key instead, (each column in the table is only dependent on the primary key), it satisfies the third paradigm (3NF).

Published an original article · won praise 2 · views 55

Guess you like

Origin blog.csdn.net/lolly1023/article/details/104555359