数据库启动并进行初始安全设置之后,我们就可以登录数据库了。
登录数据库
[root@bogon ~]# mysql -p
Enter password: 输入设置的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.10 MySQL CommunityServer (GPL)
Copyright (c) 2000, 2015, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
mysql>
创建、删除和查看数据库
创建数据库
语法:create database dbname;
mysql> create database xiaowei;
Query OK, 1 row affected (0.02 sec)
删除数据库
语法:drop database dbname;
mysql> drop database xiaowei;
Query OK, 0 rows affected (0.07 sec)
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
选择要操作的数据库
mysql> use xiaowei;
Database changed
创建表
mysql> create table t1(
-> id int,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.04 sec)
删除表
mysql> drop table t1;
Query OK, 0 rows affected (0.04 sec)
查看表
mysql> show tables;
+-------------------+
| Tables_in_xiaowei |
+-------------------+
| t1 |
+-------------------+
1 row in set (0.00 sec)
查看数据表结构
语法:desc tablename;
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.08 sec)
详细查看数据表结构
语法:show create table tablename;
语法:show create table t1\G; 格式化输出
mysql> show create table t1\G;
*************************** 1. row***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
插入数据
Insert into t1 set
Id=1,
Name=’tube’
;
批量插入数据
mysql> Insert into t1 values(2,'kevin'),(3,'mark');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看表中的数据
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | NULL |
| 1 | xiaon |
| 2 | kevin |
| 3 | mark |
+------+-------+
4 rows in set (0.00 sec)
本文出自 “阳光的蜗牛” 博客,谢绝转载!