MySql database knowledge review

1. MySql data type

1.1 Value Type

MySql中有很多数值类型,常用的数值类型有:
int    对应Java中的int类型, 占用4个字节
double 对应Java中的double类型, 占用8个字节

String type 1.2

Mysql中包含char和varchar两种字符串类型:
char(n)- 定长字符串
其中n的范围: 0~255 /个字符
name char(10)
  如果存储的数据长度小于定义的最大长度, 剩余的空间会用空格补全, 因此char类型可能会存在一定的空间浪费。
varchar(n) -- 不定长字符串
其中n的范围: 0~255 /个字符 (mysql5.0之前)
0~65535 /个字节(mysql5.0之后)
  如果存储的数据长度小于定义的最大长度, 剩余的空间可以留给别的数据使用, 因此varchar类型不会有空间的浪费。
大文本类型有:
text 0~65535 /个字节
bigtext 4GB

ps: char and varchar What is the difference?
char type varchar type slightly higher than the efficiency of storage, but there is wasted space.

1.3 Date Type

date 格式:年月日
time 格式:时分秒
datetime 格式: 年月日 时分秒
timestamp 时间戳(实际上存储的是从1970-1-1日到指定时间的毫秒值)
datetime的范围: 1000~9999年
timestamp的范围: 1970~2038年
timestamp类型的列可以设置自动更新为当前时间,但是datetime不能设置

2. The operation record table

2.1 Constraints field

  1.主键约束
    如果一个列添加了主键约束,那么该列就是这张表的主键,主键要求唯一且不能为空。
create table person1(
id int primary key,
...
);
  2.唯一约束
    如果为一个列添加了唯一约束,那么该列的值就不能重复,但是可以为null。
create table person2(
name char(10) unique,
...
);
  3.非空约束   
    如果为一个列添加了非空约束,那么该列的值就不能为空,但是可以重复。
create table person3(
gender varchar(2) not null,
...
);

2.2 drop, the difference between delete, truncate?

  ● drop只能删除库和表,不能删除表记录。
  ● delete和truncate是删除表记录,不能删除库和表本身
  ● delete删除表记录时,可以删除表中的某一部分记录,也可以删除表中的所有记录,而且删除时,是一条
     一条删除。
  ● truncate也是删除表记录,但是只能删除所有记录,删除时不是一条一条删除,而是将整张表摧毁重建。

3. MySql common function

1, ifnull (column name, value)
value determines whether the designated column is null, if null, the null value to be replaced by the second argument
2, count (column name | *)
COUNT function is specified column All statistical rows or columns
3, max (column names) and min (column name)
max (column name)
of the specified column selecting the maximum value
min (column name)
minimization of the specified column
4, sum (column name and avg (column name)
SUM (column name)
of the specified column sums
avg (column name)
averaging the specified column
5, CURDATE (), curtime (), sysdate (), now ()
CURDATE ()
Gets the current time, date format
curtime ()
to get the current time, minutes and seconds format too
Sysdate () | now ()
to get the current time in the format year, month, day, hour

4. foreign key relationships between tables and

4.1 foreign key description

 外键就是用于通知数据库两张表数据之间对应关系的一个列, 数据库会通过外键来维护两张表的对
 应关系。

4.2 Add Foreign Key

● add a foreign key to create a table at the same time

CREATE TABLE student(
sid int primary key auto_increment, -- 学生编号
sname VARCHAR(10),                  -- 学生姓名
s_cid INT,                          -- 班级编号
FOREIGN KEY (s_cid) REFERENCES class(cid)  --指定外键
);

● add the foreign key table after construction

//添加外键约束:ALTER TABLE student ADD FOREIGN KEY (s_cid) REFERENCES class(cid);

Table 4.3 Relationship

● one
● many
●-many

4.4 associated with the query, the outer join query

● related inquiries

select * from class, student
where student.s_icd = class.cid;

● left outer join query

  左外连接查询会将左边表中的所有记录都查询出来, 而右边表中只查与左边对应的记录。
select * from class left join student on student.s_icd = class.cid; 

● right outer join query

  右外连接查询会将右边表中的所有记录都查询出来,而左边表中只查与右边对应的记录。
select * from class right join student on student.s_icd = class.cid;
Released six original articles · won praise 11 · views 185

Guess you like

Origin blog.csdn.net/qq_44799169/article/details/104589728