/usr/local/mysql/bin/mysql -u root -p
然后输入数据库密码即可
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
| demo01 |
| hhh |
| information_schema |
| itcaststore |
| mydatabase |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
10 rows in set (0.01 sec)
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| demo |
| demo01 |
| hhh |
| information_schema |
| itcaststore |
| mydatabase |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
10 rows in set (0.18 sec)
注意:其实写法一和写法二是一样的。
因为使用箭头标记 -> 不是 SQL 语句的一部分,它仅仅表示一个新行,如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写 SQL 语句,SQL 语句的命令结束符为分号 ;
mysql> create database demo02;
Query OK, 1 row affected (0.10 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
| demo01 |
| demo02 |
| hhh |
| information_schema |
| itcaststore |
| mydatabase |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
11 rows in set (0.01 sec)
创建数据库demo02
mysql> drop database hhh
-> ;
Query OK, 0 rows affected (0.24 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
| demo01 |
| demo_03 |
| information_schema |
| itcaststore |
| mydatabase |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
10 rows in set (0.01 sec)
这样hhh这个无用的数据库就删除掉了
使用demo01数据库
mysql> use demo01
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> use demo01
Database changed
mysql> show tables;
Empty set (0.00 sec)
查询到这个demo01数据库里面没有表
mysql> create table if not exists `student` (
-> `id` int unsigned auto_increment,
-> `age` VARCHAR(100) NOT NULL,
-> `sex` VARCHAR(100) NOT NULL,
-> PRIMARY KEY(`id`)
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (0.30 sec)
mysql> show tables;
+------------------+
| Tables_in_demo01 |
+------------------+
| student |
+------------------+
1 row in set (0.03 sec)
创建一个名叫student的表,有三个字段:id、age、sex,将id设为主键,存储引擎为InnoDB,默认编码为utf8。
CREATE TABLE table_name (column_name column_type);
mysql> explain student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| age | varchar(100) | NO | | NULL | |
| sex | varchar(100) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
INSERT INTO table_name ( field1, field2,...fieldN)
VALUES
( values, values2,...valueN);
如果数据是字符型,必须使用单引号或者双引号,如:“value”。
varchar也是字符串类型,是变长字符串;char是定长字符串。
mysql> INSERT INTO student
-> (id, age, sex)
-> VALUES
-> (1, "20", "男");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student (id, age, sex) VALUES (2, 21, "男");
Query OK, 1 row affected (0.00 sec)
这里的第二次插入的时候,age我用的是整型的21,也没有发生报错,可能是会自动转成varchar类型存储到表中(不确定)。
mysql> SELECT * from student
-> ;
+----+-----+-----+
| id | age | sex |
+----+-----+-----+
| 1 | 20 | 男 |
| 2 | 21 | 男 |
+----+-----+-----+
2 rows in set (0.01 sec)
查询student表中 age 在20到25之间的学生信息,显示这些学生信息的id、age、sex字段,并将这些信息按照id字段逆序排序
mysql> select id, age, sex
-> from student
-> where age between 20 and 25
-> order by id desc;
+----+-----+-----+
| id | age | sex |
+----+-----+-----+
| 7 | 20 | 女 |
| 6 | 20 | 男 |
| 5 | 20 | 女 |
| 4 | 23 | 女 |
| 3 | 22 | 男 |
| 2 | 21 | 男 |
| 1 | 20 | 女 |
+----+-----+-----+
7 rows in set (0.00 sec)
mysql> DELETE FROM student where id=2;
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+-----+-----+
| id | age | sex |
+----+-----+-----+
| 1 | 20 | 男 |
+----+-----+-----+
1 row in set (0.01 sec)
删除id=2的学生的信息
DELETE FROM table_name WHERE Clause
● 如果没有指定 WHERE 子句,MySQL 表中的所有记录都将被删除。
● 可以在单个表中一次性删除记录。
mysql> update student set sex="女"
-> where id=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-----+-----+
| id | age | sex |
+----+-----+-----+
| 1 | 20 | 女 |
+----+-----+-----+
1 row in set (0.00 sec)
将id为1的学生信息的性别修改为“女”
UPDATE table_name SET field1=new_value1, field2=new_value2
WHERE Clause
● 可以同时更新一个或多个字段
mysql> drop table student;
Query OK, 0 rows affected (0.10 sec)
mysql> show tables;
Empty set (0.00 sec)
删除student这个表后,查询一下看看是否还存在,显示Empty,就说明数据库中没有表了(自己建了一个,然后再把它删除),那就说明删除成功了。
是的。在一个终端上删除数据库,另一个终端上,也会显示该数据库被删除。
下面删除了demo_03这个数据库。
system clear
mysql> show engines
-> ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.02 sec)
mysql> show create table staffs;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| staffs | CREATE TABLE `staffs` (
`id` int DEFAULT NULL,
`name` char(10) DEFAULT NULL,
`age` int DEFAULT NULL,
KEY `id_name_age_index` (`id`,`name`,`age`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
SHOW CREATE TABLE table_name;
mysql> alter table staffs engine = myisam;
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table staffs;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| staffs | CREATE TABLE `staffs` (
`id` int DEFAULT NULL,
`name` char(10) DEFAULT NULL,
`age` int DEFAULT NULL,
KEY `id_name_age_index` (`id`,`name`,`age`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
alter table table_name engine = engine_name;
mysql> show variables like 'datadir';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| datadir | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.37 sec)
表结构和表数据的文件都会存放在这个目录里。
(base) dns@SUNNY-MacBook-Air mysql % sudo -i
SUNNY-MacBook-Air:~ root# ls /usr/local/mysql/data/demo
t_lark_task_1.ibd t_schedule_cfg.ibd t_schedule_pos.ibd
opt作为后缀的文件存放的会是默认的字符集和字符校验规则
frm作为后缀的文件存放的会是表结构
ibd作为后缀的文件存放的会是表数据