MySQL基础操作之对数据库和表的增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础操作之对数据库和表的增删改查相关的知识,希望对你有一定的参考价值。
数据库管理
登陆mysql
[[email protected] conf]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>
查看数据库服务器所有数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| log |
| logs |
| mydata |
| mysql |
| mysql2 |
| performance_schema |
| temp |
| test |
| zabbix |
| zrlog |
+--------------------+
13 rows in set (0.00 sec)
创建数据库
mysql> create database myss;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| log |
| logs |
| mydata |
| mysql |
| mysql2 |
| myss |
| performance_schema |
| temp |
| test |
| zabbix |
| zrlog |
+--------------------+
14 rows in set (0.01 sec)
删除数据库
mysql> drop database mydata;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| log |
| logs |
| mysql |
| mysql2 |
| myss |
| performance_schema |
| temp |
| test |
| zabbix |
| zrlog |
+--------------------+
13 rows in set (0.00 sec)
创建带有设置字符集的数据库
mysql> create database brlog default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| brlog |
| log |
| logs |
| mysql |
| mysql2 |
| myss |
| performance_schema |
| temp |
| test |
| zabbix |
| zrlog |
+--------------------+
14 rows in set (0.00 sec)
查看某个库的默认字符集
mysql> show create database brlog;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| brlog | CREATE DATABASE `brlog` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create database blog;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| blog | CREATE DATABASE `blog` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
数据表管理
选择数据库
mysql> use brlog;
Database changed
查看数据库里的表
mysql> show tables;
Empty set (0.00 sec)
创建数据表
mysql> create table yy (username varchar(16), passwd int, yy int );
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-----------------+
| Tables_in_brlog |
+-----------------+
| yy |
+-----------------+
1 row in set (0.01 sec)
查看表结构
mysql> desc yy;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(16) | YES | | NULL | |
| passwd | int(11) | YES | | NULL | |
| yy | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
删除表
mysql> create table qq(username varchar(16), passwd int);
Query OK, 0 rows affected (0.00 sec)
mysql> drop table qq;
Query OK, 0 rows affected (0.00 sec)
修改表字段类型
mysql> desc yy;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| username | int(11) | YES | | NULL | |
| passwd | int(11) | YES | | NULL | |
| yy | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
修改表字段名称
mysql> mysql> alter table yy change column pawd int;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc yy;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| username | int(11) | YES | | NULL | |
| pawd | int(11) | YES | | NULL | |
| yy | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.01 sec)
修改表名称
mysql> alter table yy rename to qq;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_brlog |
+-----------------+
| qq |
+-----------------+
1 row in set (0.00 sec)
mysql> desc qq;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| username | int(11) | YES | | NULL | |
| pawd | int(11) | YES | | NULL | |
| yy | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
以上是关于MySQL基础操作之对数据库和表的增删改查的主要内容,如果未能解决你的问题,请参考以下文章