mysql基础命令及基本操作

基础命令

show databases;   //查看所有库

帮助
(help show)   (help SHOW CREATE DATABASE)

create database if not exists test1  //创建库

character set utf8   //字符集  
show character set;       //查看字符集

collate utf8_icelandic_ci;  //校验规则名  
SHOW COLLATION;    //查看校验规则名

show create database test1;   //查看库的信息

drop database  test1;    //删除库

use test1;   //进入库

create table test (id int,name varchar(20));        //创建表格

show tables;                                                //查看当前数据库的数据表格

drop table test;                                              //删除表格

insert into test values (1,'zhsan'),(2,'lisi');         //向表格里添加数据

select * from test;                                     //查看表格里所有的数据

delete from test where id=2;                  //删除表格里某个数据

数据库存储引擎

数据库存储引擎是数据库底层软件组件,数据库管理理系统(DBMS)使⽤用数据引擎进⾏行行创建、查询、更更新和删除数据操作。不不同的存储引擎提供不不同的存储机制、索引技巧、锁定⽔水平等功能,使⽤用不不同的存储引擎,还可以获得特定的功能。现在许多不不同的数据库管理理系统都⽀支持多种不不同的数据引擎。MySQL的核⼼心就是存储引擎。

MySQL存储引擎简介

MySQL提供了了多个不不同的存储引擎,包括处理理事务安全表的引擎和处理理⾮非事务安全表的引擎。在MySQL中,不不需要在整个服务器器中使⽤用⼀一种引擎,针对具体要求可以对每⼀一个表使⽤用不不同的存储引擎。MySQL5.5⽀支持的存储引擎有:InnoDB、MyISAM、Memory等。查看引擎的命令⽤用

mysql> SHOW ENGINES\G
*************************** 1. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: CSV
     Support: YES
     Comment: CSV storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: Archive storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: NO
     Comment: Federated MySQL storage engine
Transactions: NULL
          XA: NULL
  Savepoints: NULL
9 rows in set (0.01 sec)

查看默认存储引擎:

mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.07 sec)

一、存储引擎简介

  1. 存储引擎说白了了就是数据存储的格式,不不同的存储引擎功能不不同,占用的空间大小不不同,读取性能也不不同
  2. 数据库存储引擎是数据库底层软件组件,不不同的存储引擎提供不不同的存储机制
  3. 在 MySQL 中,不不需要在整个服务器器中使⽤用同⼀一种存储引擎,可以对每⼀一个表使⽤用不不同的存储引擎
  4. MySQL 支持多种存储引擎,如 InnoDB 、MyISAM 、Memory 、Merge 、Archive 、CSV 、Federated 等等

数据表操作

创建表的语法形式

数据表属于数据库,在创建数据表之前,应该使用语句“USE <数据库名>” 指定操作是从哪个数据库中进行,如果没有选择数据库,会报错

语法:

CREATE TABLE <表名> (字段名1,数据类型 [列级别约束条件] [默认值],字段名2,数据类型 [列级别约束条件] [默认值]... ... ):

例如:

mysql> create table tb_emp1
    -> (
    -> id int(11),
    -> name varchar(25),
    -> deptid int(11),
    -> salary float
    -> );

使用主键约束

主键约束要求主键列的数据唯一,并且不允许为空。

  1. 单字段主键
    语法:
字段名数据类型PRIMARY KEY [默认值]

例如:

mysql> create table tb_emp2
    -> (
    -> id int(11) primary key,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float
    -> );

2.在定义完所有列之后指定主键语法:

[CONSTRAINT <约束名>] PRIMARY KEY [字段名]

例如:

mysql> CREATE TABLE tb_emp3
 -> (
 -> id INT(11),
 -> name VARCHAR(25),
 -> deptId INT(11),
 -> salary FLOAT,
 -> PRIMARY KEY(id)
 -> );
Query OK, 0 rows affected (0.01 sec)

3.多字段联合主键
语法:

PRIMARY KEY [字段1,字段2,....]

例如:

mysql> CREATE TABLE tb_emp4
 -> (
 -> name VARCHAR(25),
 -> deptId INT(11),
 -> salary FLOAT,
 -> PRIMARY KEY(name,deptId)
 -> );
Query OK, 0 rows affected (0.00 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 (0.00 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) REFERENCES tb_dept1(id)
 -> );
Query OK, 0 rows affected (0.06 sec)
使用非空约束

非空约束指字段的值不能为空。
语法:

字段名 数据类型 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 (0.06 sec)

或者 语法:

[CONSTRATIN <约束名>] UNIQUE (<字段名>

例如:

mysql> CREATE TABLE tb_dept3
 -> (
 -> id INT(11) PRIMARY KEY,
 -> name VARCHAR(22),
 -> location VARCHAR(50),
 -> CONSTRAINT STH UNIQUE(name)
 -> );
Query OK, 0 rows affected (0.00 sec)
使用默认约束

默认约束指定某列的默认值。
语法:

字段名 数据类型 DEFAULT 默认值

例如:

mysql> CREATE TABLE tb_emp7
 -> (
 -> id INT(11) PRIMARY KEY,
 -> name VARCHAR(25) NOT NULL,
 -> deptId INT(11) DEFAULT 1111,
 -> salary FLOAT,
 -> info VARCHAR(50)
 -> );
Query OK, 0 rows affected (0.00 sec)
设置表的属性值自动增加

语法:

字段名 数据类型 AUTO_INCREMENT

例如:

mysql> CREATE TABLE tb_emp8
 -> (
 -> id INT(11) PRIMARY KEY AUTO_INCREMENT,
 -> name VARCHAR(25) NOT NULL,
 -> deptId INT(11),
 -> salary FLOAT
 -> );
Query OK, 0 rows affected (0.00 sec)

插入数据验证:

mysql> INSERT INTO tb_emp8(name,salary)
 -> VALUES('lucy',1000),('lura',1200),('kevin',1500);
 Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

查看:

mysql> SELECT * FROM tb_emp8;
+----+-------+--------+--------+
| id | name | deptId | salary |
+----+-------+--------+--------+
| 1 | lucy | NULL | 1000 |
| 2 | lura | NULL | 1200 |
| 3 | kevin | NULL | 1500 |
+----+-------+--------+--------+
3 rows in set (0.00 sec)

查看数据表结构

查看表基本结构语句DESCRIBE
语法:

DESCRIBE 表名; 或 DESC 表名;

例如:

mysql> DESCRIBE tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

或者

mysql> DESC tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

查看表详细结构语句

语法:

SHOW CREATE TABLE <表名\G>

例如:

mysql> SHOW CREATE TABLE tb_emp1\G
*************************** 1. row ***************************
 Table: tb_emp1
Create Table: CREATE TABLE `tb_emp1` (
 `id` int(11) DEFAULT NULL,
 `name` varchar(25) DEFAULT NULL,
 `deptId` int(11) DEFAULT NULL,
 `salary` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

修改数据表

修改表名
语法:

ALTER TABLE <旧表名> RENAME [TO] <新表名>.

例如:

mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| tb_dept1 |
| tb_dept3 |
| tb_emp1 |
| tb_emp2 |
| tb_emp3 |
| tb_emp4 |
| tb_emp5 |
| tb_emp7 |
| tb_emp8 |
+----------------+
9 rows in set (0.00 sec)
mysql> ALTER TABLE tb_dept3 RENAME tb_deptment3;
Query OK, 0 rows affected (0.05 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| tb_dept1 |
| tb_deptment3 |
| tb_emp1 |
| tb_emp2 |
| tb_emp3 |
| tb_emp4 |
| tb_emp5 |
| tb_emp7 |
| tb_emp8 |
+----------------+
9 rows in set (0.00 sec)

修改字段的数据类型

语法:

ALTER TABLE <表名> MODIFY <字段名> <数据类型>

例如:

mysql> DESC tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> ALTER TABLE tb_dept1 MODIFY name VARCHAR(30);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改字段名

语法:

ALTER TABLE <表名> CHANGE<旧字段名><新字段名> <新数据类型>

例如:

mysql> ALTER TABLE tb_dept1 CHANGE location loc VARCHAR(50);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

添加字段

语法:

ALTER TABLE <表名> ADD <新字段名><数据类型> [约束条件] [FIRST|AFTER 已存在字段名]

例如:

mysql> DESC tb_dept1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> ALTER TABLE tb_dept1 ADD column1 VARCHAR(12) not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

或者

mysql> ALTER TABLE tb_dept1 ADD column2 INT(11) FIRST;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column2 | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

或者

mysql> alter table tb_dept1 add cloumn3 int(11) after name;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column2 | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

删除字段

语法:

ALTER TABLE <表名> DROP <字段名>

例如:

mysql> ALTER TABLE tb_dept1 DROP column2;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
修改字段的排列位置

语法:

ALTER TABLE <表名> MODIFY <字段名> <数据类型> FIRST | AFTER <字段2> 

例如:

mysql> ALTER TABLE tb_dept1 MODIFY column1 VARCHAR(12) FIRST;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column1 | varchar(12) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

或者

mysql> ALTER TABLE tb_dept1 MODIFY column1 VARCHAR(12) AFTER loc;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC tb_dept1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
| column1 | varchar(12) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
更改表的存储引擎

语法:

ALTER TABLE <表名> ENGINE=<更改后的存储引擎>

例如:

mysql> SHOW CREATE TABLE tb_deptment3\G
*************************** 1. row ***************************
 Table: tb_deptment3
Create Table: CREATE TABLE `tb_deptment3` (
 `id` int(11) NOT NULL,
 `name` varchar(22) DEFAULT NULL,
 `location` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `STH` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> ALTER TABLE tb_deptment3 ENGINE=MyISAM;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE tb_deptment3\G
*************************** 1. row ***************************
 Table: tb_deptment3
Create Table: CREATE TABLE `tb_deptment3` (
 `id` int(11) NOT NULL,
 `name` varchar(22) DEFAULT NULL,
 `location` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `STH` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
删除表的外键约束

语法:

ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>

例如:

mysql> CREATE TABLE tb_emp9
 -> (
 -> id INT(11) PRIMARY KEY,
 -> name VARCHAR(25),
 -> deptId INT(11),
 -> salary FLOAT,
 -> CONSTRAINT fk_emp_dept FOREIGN KEY (deptId) REFERENCES tb_dept1(id)
 -> );
Query OK, 0 rows affected (0.04 sec)
mysql> SHOW CREATE TABLE tb_emp9\G
*************************** 1. row ***************************
 Table: tb_emp9
Create Table: CREATE TABLE `tb_emp9` (
 `id` int(11) NOT NULL,
 `name` varchar(25) DEFAULT NULL,
 `deptId` int(11) DEFAULT NULL,
 `salary` float DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `fk_emp_dept` (`deptId`),
 CONSTRAINT `fk_emp_dept` FOREIGN KEY (`deptId`) REFERENCES `tb_dept1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> ALTER TABLE tb_emp9 DROP FOREIGN KEY fk_emp_dept;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE tb_emp9\G
*************************** 1. row ***************************
 Table: tb_emp9
Create Table: CREATE TABLE `tb_emp9` (
 `id` int(11) NOT NULL,
 `name` varchar(25) DEFAULT NULL,
 `deptId` int(11) DEFAULT NULL,
 `salary` float DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `fk_emp_dept` (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

删除数据表

删除没有被关联的表
语法:

DROP TABLE [IF EXISTS]表1,表2...

例如:

mysql> DROP TABLE IF EXISTS tb_dept2;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| tb_dept1 |
| tb_deptment3 |
| tb_emp1 |
| tb_emp2 |
| tb_emp3 |
| tb_emp4 |
| tb_emp5 |
| tb_emp7 |
| tb_emp8 |
| tb_emp9 |
+----------------+
10 rows in set (0.00 sec)

删除被其他表关联的主表

先创建表tb_dept2

mysql> create table tb_dept2
 -> (
 -> id INT(11) PRIMARY KEY,
 -> name VARCHAR(22),
 -> location VARCHAR(50)
 -> );
Query OK, 0 rows affected (0.08 sec)

创建表tb_emp

mysql> CREATE TABLE tb_emp
 -> (
 -> id INT(11) PRIMARY KEY,
 -> name VARCHAR(25),
 -> deptId INT(11),
 -> salary FLOAT,
 -> CONSTRAINT fk_emp_dept FOREIGN KEY(deptId) REFERENCES tb_dept2(id)
 -> );
Query OK, 0 rows affected (0.09 sec)

直接删除父表tb_dept2

mysql> DROP TABLE tb_dept2;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint
fails

可以看到在外键约束时,主表不能直接删除。

mysql> ALTER TABLE tb_emp DROP FOREIGN KEY fk_emp_dept;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0

解除关联子表tb_dept的外键约束

mysql> DROP TABLE tb_dept2;
Query OK, 0 rows affected (0.05 sec)

表就可以被删除

猜你喜欢

转载自blog.csdn.net/hjyhjy0/article/details/110053198