码迷,mamicode.com
首页 > 数据库 > 详细

MySQL基础操作之对数据库和表的增删改查

时间:2018-05-10 23:31:22      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:MySQL

数据库管理

登陆MySQL

[root@lynn-04 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基础操作之对数据库和表的增删改查

标签:MySQL

原文地址:http://blog.51cto.com/10963213/2115024

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!