liunx系统下MYSQL数据库超详解及解析

第一篇:

目录:

1.mysql是什么?


2.mysql数据库命令详解


一. mysql是什么?                                                                         


1.MySQL DBA:是一个开放源代码的数据库管理系统

分为四个阶段人工管理阶段,文件系统阶段,数据库系统阶段,高级数据库阶段

数据库形式分为:层次式数据库,网络式数据库,关系式数据库

2.数据库的特点:实现数据共享,减少数据冗余,采用特定的数据类型,具有较高的数据独立性,具有统一的数据控制功能。

3.数据库系统解析:

数据库系统有三个主要的组成部分:数据库,数据库管理系统,数据库应用系统:

数据库:用于存储数据的地方

数据库管理系统:用于管理数据库的软件

数据库应用系统:为了提高数据库系统的处理能力所使用的的管理数据库的软件补充

SQL语言

二.   数据库命令解析 

1.创建表的结构                                                                   

#创建数据表,查看数据表结构,修改数据表,删除数据表
创建数据表格式
create table   表名
(
字段名1,数据类型   【列级别约束条件】  默认值,
字段名2,数据类型   【列级别约束条件】  默认值,
... ...
【表级别约束条件】
);

创建表,必须要指定信息
1.要创建表的名称,不区分大小写,不能使用SQL语言中的关键字,drop,alter,insert等
2.数据表中每一列字段的名称和数据类型,如果创建多列,用逗号隔开
创建表,必须要指定信息
mysql> show databases;     #查看表数据
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.05 sec)

mysql> create database test_db;          #创建test_db表格
Query OK, 1 row affected (0.05 sec)

mysql> show databases;          #查看表格信息
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db            |
+--------------------+
5 rows in set (0.01 sec)
mysql> use test_db;             #使用test_db; 表格
Database changed

mysql> create table tb_emp1    #创建 tb_emp1表格
    -> (
    -> id  int(11),
    -> name varchar(25),
    -> deptid int(11),
    -> salary float);
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_emp1           |
+-------------------+
1 row in set (0.01 sec)

2. 主键约束                                                                                     

#
#主键能够唯一的标识表中的一条记录,可以结合外键来定义不同数据表之间的关系
1.书写格式:
字段名   数据类型  primary key 默认值
2.mysql> create table tb_emp2
    -> (  
    -> id  int(11)  primary key,
    -> name varchar(25),
    -> deptid  int(11),
    -> salary  float);
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_emp1           |
| tb_emp2           |
+-------------------+
2 rows in set (0.03 sec)
在定义完所有的列之后指定主键
[constraint  <约束名>]   primary   key   字段名

多字段联合组成;

primary key [字段1,字段2,.....字段n]
mysql> create table tb_emp4
    -> (
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    -> primary key(name,deptid)
    -> );
Query OK, 0 rows affected, 1 warning (0.06 sec)

外键

字段,主要作用是保证数据引用的完整性。保持数据的一致性,完整性

[constraint <外键名>] foreign key 字段名1[,字段名2,.....]
references  <主表名>  主键列1[,主键列2,.....]
mysql> create table tb_dept1
    -> (
    -> id int(11) primary key,
    -> name varchar(22) not null,
    -> location varchar(50)
    -> );
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> create table tb_emp5
    -> (
    -> id int(11) primary key,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    -> constraint fk_emp_dept1 foreign key(deptid) r

非空约束

字段名   数据类型   not  null

mysql> create table tb_emp6
    -> (
    -> id int(11) primary key,
    -> name varchar(25) not null,
    -> deptid int(11),
    -> salary float
    -> );
Query OK, 0 rows affected, 2 warnings (0.05 sec)

唯一性约束:可以为空值 主键:不允许为空

字段名 数据类型 unique
mysql> create table tb_dept2
    -> (
    -> id int(11) primary key,
    -> name varchar(22) unique,
    -> location varchar(50)
    -> );
Query OK, 0 rows affected, 1 warning (0.05 sec)

猜你喜欢

转载自blog.csdn.net/qq_56628318/article/details/125106870