The basic concept of a database table and simple single operation

A: MySQL basic introduction:
MySQL is a relational database
mysql is a part of Oracle's database products.
Divided into Business and Community Edition
Technical point of view:
the MySQL database is a C / S (Client / Server) Model service B / S (Browser / Server)
the MySQL network communication model: NIO + scenario to achieve connection pool, support the high concurrency
 
MySQL jargon explained :
  Entity : the real world exists objectively and can be the difference between things. For example, "a student", "book", "a lesson" and so on. It is worth emphasizing here the "thing" is not just tangible "thing", it can also be virtual, but rather that "the relationship between the teacher and the school."
 Properties : textbook explanation: "The entity has certain characteristics," shows that property began as a logical concept, for example, "gender" is a "person" as an attribute. In a relational database, the concept of physical property is, property can be seen as "a table."
 Tuple: table row is a tuple.
 Component: a property value tuple. In a relational database, it is an atomic operation, i.e. in a relational database doing anything when the attribute is "inseparable." Otherwise it is not a relational database.
 Code: the table can uniquely identify a tuple of a property (or property group), if such a code has more than one, then we call the candidate code, we pick out a code from the candidate do the boss, it is called primary code .
 Full code: if one yard contains all the attributes, this code is the full code.
 Main properties (primary key) : this property is a property as long as the primary attribute appeared in any of the candidate codes.
  Non-primary property (non-primary key attribute) : on the contrary, does not appear in any of the above candidate code before, this property is the non-primary property.
  Outer code (foreign key) : an attribute (or attribute group), it is not a code, but other codes table, it is the outer code.

The benefits of database paradigm:
1, reduce data redundancy
2, eliminate abnormal
2, so that a more rational organization of data
Table students (student ID, student name, student ages)
transcript (student ID, program ID, student name, student age, achievement)
1, a first paradigm (1NF): maintaining the atomic nature of each column
column is a basic data row can not be resolved
if not 1NF, is split into many relationship entity
Note: do not meet the first paradigm can not be called Relational Database
:( student table Student ID, name, age and home address)
EG: 18 1 Zhang Xi'an, Shaanxi Province, Xi'an University of 7 apartments
Xi'an, Shaanxi Province Weiyang District, Xi'an Technological University, Xi'an, Shaanxi Weiyang District 7 apartments marsh street Xi'an University
after split:
student table :( student ID, name, age, address, ID)
address table :( address ID, province, city, district, street)
2, a second paradigm (2NF): 1NF, entirely dependent on the primary key attribute (dependent relief portion)
non-primary key attribute is fully dependent on the primary, if not dependent on the primary key, it should be split into a new body,
designed in many relationship
Student achievement table :( student ID, program ID, student name, student age, course name, course grade)
primary key (student ID, program ID)
student name -> Student ID partly dependent
students of all ages -> Student ID depends in part on
the course name -> course ID depends in part on
the course grade -> student ID, program ID completely dependent
After the split:
Table students (student ID, student name, age of the students) -> Primary key: Student ID
curriculum (course ID, course name) -> Primary Key: Course ID
student achievement table (student ID, course ID, course grades) -> primary key: student ID, program ID

3, a third paradigm (3NF): in 2NF basis, not dependent on other properties of non-primary property (transfer eliminate dependency)
Student Information Table (student ID, student name, college name, phone college, college address)
primary key: Student ID
Student Name -> Student ID
School Name -> Student ID
School Phone -> College -> Student ID
After the split:
Table students (student ID, student name, college ID) primary key: Student ID
School table (college ID, name of college, college phone School Address) Primary key: College ID
General relational database in third normal form on it
Analysis shows that: the higher the paradigm, the more the table, the more problems brings to the table:
1, the query operation will connect multiple tables, queries increased the blessing of reading
2, query need to connect multiple tables, reducing the performance of the query
Two: Library simple operation (running on cmd)
Command connection to the database: mysql -uroot -p (enter a password to enter)
Exit Database: quit; (semicolon)

 

See all libraries: show databases;

Create a database of SQL: create database database_name;

Delete data SQL: drop database database_name;

Select the database of SQL: use database_name;

Under the current library display all the tables SQL: show tables;

 

 Three: table-related operations

1, create a table

create table table_name

Attribute Name Data Type [integrity constraint],

Attribute Name Data Type [integrity constraint],

...
属性名 数据类型[完整性约束]
)
注意:在数据库创建的时候要选取合适的数据类型,而且还要添加完整性约束
完整性约束条件有:
-------------------------------------
     约束条件       | 说明
-------------------------------------
   primary key     | 修饰的属性是该表的主键
-------------------------------------
   foreign key     | 修饰的数据是该表的外键
-------------------------------------
   not null        |表示该字段不能为null
-------------------------------------
   unique          |修饰的属性值是唯一的
-------------------------------------
   auto_increment  |mysql的特色,表示该属性是自增的
-------------------------------------
   default         | 设置属性默认值
-------------------------------------
eg:create table t2(id int primary key, name varchar(5));
2.1、查看表
2.1.1、 desc table_name;
用desc可以查看表的字段名称(Fileld)、类型(Type)、是否为空(Null)、约束条件(Key)、默认值(Default)、备注信息(Extra)
2.1.2  show create table table_name;
show create table可以打印创建的表的SQL,
并且显示该表的 存储引擎和字符集编码(截图不方便,这样显示)
mysql> show create table t2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                               |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `id` int(11) NOT NULL,
  `name` varchar(6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3.1 修改表
在使用的过程中不满足使用情况时,进行修改
 3.1.1 修改表名SQL
 alter table old_table_name rename [to] new_table_name;
 
 3.1.2、修改表中字段类型SQL:
 alter table table_name modify 属性名 数据类型;
 eg:alter table t1 id bigint;
 
 3.1.3、修改表中属性名SQL:
 alter table table_name change 旧属性名 新属性名 新数据类型;
 eg:alter table t1 change id idd int;
 
 3.1.4、增加新的字段SQL:
 alter table table_name add 属性名 数据类型 [完整性约束] [first|after 属性名2]
 eg:alter table t1 add age int;

显示该表的存储引擎和字符集编码

 3.1.5、删除属性SQL:
 alter table table_name drop 属性名;
 eg:alter table t1 drop age2;
 
 3.1.6、修改属性的排列顺序SQL:
 alter table table_name modify 属性名1 数据类型 first | after 属性名2;
 
4. 查询表SQL
SQL基本的结构如下:
select 属性列表 from table_name [where 条件表达式1]
[group by 属性1 [having 条件表达式2]]
[order by 属性2 [asc | desc]]
 
4.1 带in的子查询
[not] in(元素1,元素2...元素n)
eg:select * from Student where SID in (1,3,5);
4.2 带between and 的范围查询
[not] between 取值1 and 取值2
eg:select * from Student where SID between 3 and 7;
4.3 带like的模糊查询字符串
[not] like 'abc';
注意:like可以结合通配符使用
%:表示0个或者是任意长度的字符串
_:只能表示单个字符
eg:select * from Student where Sname like 'CJ%';
   +-----+--------+------+------+
   | SID | Sname  | Ssex | Sage |
   +-----+--------+------+------+
   |   5 | CJ1210 | 1    |   11 |
   |   7 | CJ1210 | 1    | NULL |
   +-----+--------+------+------+
4.4 空值查询
is [not] null;  
eg:select * from Student where Sage is not null;
4.5 带and 的多条件查询
条件表达式1 and 条件表达式2 [and ...条件表达式n]
eg:select * from Student where Ssex ='nan' and Sage=20;
4.6 带or的多条件查询
条件表达式1 or 条件表达式2 [or ...条件表达式n]
4.7 去重复查询
select distinct 属性名
eg:select distinct Sage from Student;
4.8 对结果排序
order by 属性名 [asc| desc];
asc:升序(默认是胜序)  desc:降序
eg:select * from Student order by SID asc;
4.9 分组查询
group by 属性名[having 条件表达式]
eg:select * from Student group by Ssex having Sage >= 18;
4.10 limit分页查询
不指定起始位置 limit是记录数
eg:select * from Student limit 3;
指定初始位置是limit的起始位置、记录数
eg:select * from Student limit 2,5;
 
 

Guess you like

Origin www.cnblogs.com/laurarararararara/p/11872353.html