mysql数据表基本操作

MySQL常用命令集合

基本命令

一、数据库基本操作

  • 查看所有数据库
show databases ;
  • 创建数据库-Book
create database Book ;
  • 删除数据库-Book
drop database Book ;
  • 查看默认存储引擎
show variables like 'storage_engine' ;

二、数据表基本操作

  • 进入需要操作的数据库-test_db

use test_db ;
  • 创建数据表-tb_emp1

MariaDB [test_db]> create table tb_emp1
    -> (
    -> id int(11),
    -> name varchar(25),
    -> deptId int(11),
    -> salary float
    -> );
Query OK, 0 rows affected (0.00 sec)

  • 查看所有数据表

MariaDB [test_db]> show tables ;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_emp1           |
+-------------------+
1 row in set (0.00 sec)

-### 创建数据表的同时定义主键

  • 语法:字段名 数据类型 primary key [默认值]
MariaDB [test_db]> create table tb_emp2
    -> (
    -> id int(11) primary key,
    -> name varchar(25),
    -> deptId int(11),
    -> salary float
    -> );
Query OK, 0 rows affected (0.00 sec)
  • 定义完所有列后定义主键

  • 语法:[constraint <约束名>] primary key [字段名]
MariaDB [test_db]> create table tb_emp3
    -> (
    -> id int(11),
    -> name varchar(25),
    -> depId int(11),
    -> salary float,
    -> primary key(id)
    -> );
Query OK, 0 rows affected (0.00 sec)

  • 多字段联合主键

  • 语法:primary key [字段1, 字段2,字段n]
MariaDB [test_db]> 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] peferences <主表名> 主键列1 [,主键列2]
  • 外键名:为定义的外键约束的名称,一个表中不能有相同名称的外键
  • 字段名:表示子表需要添加外键约束的 字段列
  • 主表名:即被子表外键所依赖的表的名称
  • 主键列:表示主要中定义的主键列,或都列组合
  • eg:定义数据表tb_emp5,在tb_emp5创建外键约束
create table tb_dept1
    -> (
    -> id int(11) primary key,
    -> name varchar(22) not null,
    -> location varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)
 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.01 sec)
  • 使用非空约束

  • 语法:字段名 数据类型 not null
create table tab_emp6
    -> (
    -> id int(11) primary key ,
    -> name varchar(25) not null ,
    -> deptId int(11),
    -> salary float 
    -> );
Query OK, 0 rows affected (0.00 sec)

- 使用唯一性约束

  • 语法:字段名 数据类型 unique
create table tb_dept2
    -> (
    -> id int(11) primary key ,
    -> name varchar(22) unique ,
    -> location varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)

- 使用默认约束

语法:字段名 数据类型 default 默认值

create table tb_emp7
    -> (
    -> id int(11) primary key ,
    -> name varchar(25) not null ,
    -> deptId int(11) default 111 ,
    -> salary float 
    -> );
Query OK, 0 rows affected (0.00 sec)

- 设置表的属性值自动增加

语法:字段名 数据类型 auto_increment

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.01 sec)

insert into tb_emp8 (name,salary) values('lucy',1000),('lura',1200),('kevin',1500);
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)

  • 查看表的基本结构-desc或desccribe
desc tb_emp8;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(25) | NO   |     | NULL    |                |
| deptId | int(11)     | YES  |     | NULL    |                |
| salary | float       | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

注:NULL:可以存储空值
key:表示是否已编制索引。
PRI:表示主键
UNI:表示唯一值
MUL:表示可重复出现
Default:是否有默认值及值是多少
Extra:可获取的与给定列有关的附加信息

查看表的详细信息

show create table tb_emp8 ;

三、修改数据表

- 修改表名

语法:alter table <旧表名> rename [to] <新表名>

alter table tab_emp6 rename tb_emp6 ;

- 修改字段数据类型

  • 语法:alter table <表名> modify <字段名> <数据类型>
alter table tb_dept1 modify name varchar(30);
Query OK, 0 rows affected (0.01 sec)               
Records: 0  Duplicates: 0  Warnings: 0

- 修改字段名

  • 语法:alter tbale <表名> change <旧字段名> <新字段名> <新数据类型>
 alter table tb_dept1 change location loc varchar(50);
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0

- 添加字段

  • 语法:alter table <表名> add <新字段名> <数据类型> [约束条件] [first | after 已存在的字段名] ;
alter table tb_dept1 add managerId int(10) ;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> 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    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

  • 添加有完整性约束的字段
MariaDB [test_db]>alter table tb_dept1 add column1 varchar(12) not null ;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0
MariaDB [test_db]> 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    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 在表的第一列添加一个字段
alter table tb_dept1 add column2 int(11) first ;
Query OK, 0 rows affected (0.01 sec)               
Records: 0  Duplicates: 0  Warnings: 0
  • 在name列后添加一个int类型字段
alter table tb_dept1 add column3 int(11) after name ;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0

- 删除字段

  • alter table <表名> drop <字段名> ;
alter table tb_dept1 drop column2 ;
Query OK, 0 rows affected (0.03 sec)               
Records: 0  Duplicates: 0  Warnings: 0
  • 修改字段排列位置
MariaDB [test_db]> 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    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

MariaDB [test_db]> alter table tb_dept1 modify column1 varchar(12) first ;
Query OK, 0 rows affected (0.01 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> 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    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
  • 修改字段到表的指定列之后
MariaDB [test_db]> alter table tb_dept1 modify column1 varchar(12) after loc ;
Query OK, 0 rows affected (0.01 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> 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    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
  • 更换表的存储引擎
  • 语法:alter table <表名> engine=<更改后的存储引擎名>
alter table tb_detpment3 engine=myisam;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0
MariaDB [test_db]> show create table tb_detpment3 \G
*************************** 1. row ***************************
       Table: tb_detpment3
Create Table: CREATE TABLE `tb_detpment3` (
  `id` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
  • 删除表的外键约束
  • 语法:alter table <表名> drop foreign key <外键约束名>
MariaDB [test_db]> 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=latin1
1 row in set (0.00 sec)

MariaDB [test_db]> alter table tb_emp9 drop foreign key fk_emp_dept;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> 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=latin1
1 row in set (0.00 sec)

  • 删除没有关联的表
  • drop table [if exists] 表1 ,表2,…,表n;
 drop table if exists tb_dept2 ;
Query OK, 0 rows affected (0.00 sec)

  • 删除有关联的表
MariaDB [test_db]> drop table tb_dept2;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
MariaDB [test_db]> alter table tb_emp drop foreign key fk_emp_dept;
Query OK, 0 rows affected (0.00 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test_db]> drop table tb_dept2 ;
Query OK, 0 rows affected (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL数据表基本操作包括创建数据表、插入数据、查询数据、更新数据和删除数据。创建数据表时,需要指定表名和字段名,并为每个字段指定数据类型和约束条件。可以使用CREATE TABLE语句来创建数据表。插入数据时,使用INSERT INTO语句,指定要插入的数据和表名。查询数据可以使用SELECT语句,可以指定查询条件和排序规则。更新数据可以使用UPDATE语句,根据指定的条件更新字段的数值。删除数据可以使用DELETE FROM语句,根据指定的条件删除数据行。这些基本操作可以帮助用户在MySQL数据库中进行数据管理和交互,是数据库操作的基础知识。 另外,MySQL数据表基本操作还包括对表结构的修改和索引的创建。可以使用ALTER TABLE语句对表结构进行修改,包括添加、修改和删除字段等操作。创建索引可以加快数据的检索速度,可以使用CREATE INDEX语句来创建索引。除此之外,还可以使用DESCRIBE语句来查看数据表的结构,使用SHOW TABLES语句来查看数据库中的数据表列表。这些操作可以帮助用户更好地管理和维护MySQL数据库中的数据表。 总之,对于MySQL数据表基本操作,包括创建数据表、插入数据、查询数据、更新数据、删除数据、修改表结构和创建索引等操作,是数据库管理和交互的基础,对于数据库开发和管理都是非常重要的知识点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值