mysql -在终端操作数据库(不定时update)
--mysql 学习笔记 -- 关系型数据库 --一、 如何使用终端操作数据库 --1. 如何登陆数据库服务器 fudandandembp:~ futantan$ mysql -u root -p Enter password: --2. 如何查询数据库服务器中所有的数据库? mysql> show databases; --一定要有英文的分号 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> --添加database : 创建数据库,该命令的作用: 如果数据库不存在则创建,存在则不创建。 mysql> create database student; Query OK, 1 row affected (0.01 sec) --3.如何选中某一个数据库进行操作 mysql> select * from admin; ERROR 1046 (3D000): No database selected mysql> mysql> use student; Database changed mysql> --如何在数据库中创建数据库; create database test mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> --如何查看数据哭中所有的数据哭; mysql> show tables; Empty set (0.01 sec) mysql> --创建一个数据表; create TABLE PET( name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | PET | +----------------+ 1 row in set (0.00 sec) mysql> --查看数据表的结构; mysql> describe pet; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.01 sec)
再举一例:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> use test; Database changed mysql> create table test1_tbl( -> test_id int not null auto_increment, -> test_title varchar(100) not null, -> test_author varchar(40) not null, -> submission_date DATE, -> primary key (test_id ) -> )engine=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected, 1 warning (0.23 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | test1_tbl | +----------------+ 1 row in set (0.00 sec) mysql> desc test1_tbl -> ; +-----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+----------------+ | test_id | int(11) | NO | PRI | NULL | auto_increment | | test_title | varchar(100) | NO | | NULL | | | test_author | varchar(40) | NO | | NULL | | | submission_date | date | YES | | NULL | | +-----------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql>
mysql> --查看数据表中的记录; mysql> select * from pet; Empty set (0.01 sec) mysql> --如何在数据表中添加记录呢? INSERT INTO pet VALUES ('jianguo','xiaozhan','yingduan','f','2018-09-05',NULL); mysql> INSERT INTO pet -> VALUES ('jianguo','xiaozhan','yingduan','f','2018-09-05',NULL); Query OK, 1 row affected (0.00 sec) mysql> select * from pet; +---------+----------+----------+------+------------+-------+ | name | owner | species | sex | birth | death | +---------+----------+----------+------+------------+-------+ | jianguo | xiaozhan | yingduan | f | 2018-09-05 | NULL | +---------+----------+----------+------+------------+-------+ 1 row in set (0.00 sec) mysql> --再加一个; mysql> INSERT INTO pet -> VALUES ('wangcai','zhouxingchi','cat','m','2019-08-05',NULL); Query OK, 1 row affected (0.01 sec) mysql> select * from pet; +---------+-------------+----------+------+------------+-------+ | name | owner | species | sex | birth | death | +---------+-------------+----------+------+------------+-------+ | jianguo | xiaozhan | yingduan | f | 2018-09-05 | NULL | | wangcai | zhouxingchi | cat | m | 2019-08-05 | NULL | +---------+-------------+----------+------+------------+-------+ 2 rows in set (0.00 sec)
INSERT 插入多条数据
INSERT INTO table_name (field1, field2,...fieldN) VALUES (valueA1,valueA2,...valueAN),(valueB1,valueB2,...valueBN),(valueC1,valueC2,...valueCN)......;
mysql> --mysql 常用类型有哪些; https://www.runoob.com/mysql/mysql-data-types.html MySQL中定义数据字段的类型对你数据库的优化是非常重要的。 MySQL支持多种类型,大致可以分为三类: 数值 日期/时间 字符串(字符)类型 类型 大小 范围(有符号) 范围(无符号) 用途 TINYINT 1 byte (-128,127) (0,255) 小整数值 SMALLINT 2 bytes (-32 768,32 767) (0,65 535) 大整数值 MEDIUMINT 3 bytes (-8 388 608,8 388 607) (0,16 777 215) 大整数值 INT或INTEGER 4 bytes (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值 BIGINT 8 bytes (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值 FLOAT 4 bytes (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度 浮点数值 DOUBLE 8 bytes (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度 浮点数值 DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值
--举个例子:
mysql> create table testtype(
-> number TINYINT
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> show tables
-> exit;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exit' at line 2
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| PET |
| testtype |
+----------------+
mysql> INSERT INTO testtype VALUE(127);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testtype VALUE(128);
ERROR 1264 (22003): Out of range value for column 'number' at row 1
mysql>
--日期 选择按照格式
日期和时间类型
表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。
每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。
TIMESTAMP类型有专有的自动更新特性,将在后面描述。
类型 大小
( bytes) 范围 格式 用途
DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS 时间值或持续时间
YEAR 1 1901/2155 YYYY 年份值
DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS 混合日期和时间值
TIMESTAMP 4
1970-01-01 00:00:00/2038
结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07
YYYYMMDD HHMMSS 混合日期和时间值,时间戳
--数值和字符串按照大小
字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。该节描述了这些类型如何工作以及如何在查询中使用这些类型。
类型 大小 用途
CHAR 0-255 bytes 定长字符串
VARCHAR 0-65535 bytes 变长字符串
TINYBLOB 0-255 bytes 不超过 255 个字符的二进制字符串
TINYTEXT 0-255 bytes 短文本字符串
BLOB 0-65 535 bytes 二进制形式的长文本数据
TEXT 0-65 535 bytes 长文本数据
MEDIUMBLOB 0-16 777 215 bytes 二进制形式的中等长度文本数据
MEDIUMTEXT 0-16 777 215 bytes 中等长度文本数据
LONGBLOB 0-4 294 967 295 bytes 二进制形式的极大文本数据
LONGTEXT 0-4 294 967 295 bytes 极大文本数据
--删除数据
mysql> select * from pet;
+----------+-------------+----------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------------+----------+------+------------+-------+
| jianguo | xiaozhan | yingduan | f | 2018-09-05 | NULL |
| wangcai | zhouxingchi | cat | m | 2019-08-05 | NULL |
| xiaowang | wangyibo | dog | f | 2018-08-05 | NULL |
| xiaowang | wangyibo | dog | f | 2018-08-05 | NULL |
+----------+-------------+----------+------+------------+-------+
4 rows in set (0.00 sec)
mysql> delete from pet where name='xiaowang';
Query OK, 2 rows affected (0.00 sec)
mysql> select * from pet;
+---------+-------------+----------+------+------------+-------+
| name | owner | species | sex | birth | death |
+---------+-------------+----------+------+------------+-------+
| jianguo | xiaozhan | yingduan | f | 2018-09-05 | NULL |
| wangcai | zhouxingchi | cat | m | 2019-08-05 | NULL |
+---------+-------------+----------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
--如何修改数据
mysql> update pet set name='wangwang' where owner='wangyibo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from pet;
+----------+-------------+----------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------------+----------+------+------------+-------+
| jianguo | xiaozhan | yingduan | f | 2018-09-05 | NULL |
| wangcai | zhouxingchi | cat | m | 2019-08-05 | NULL |
| wangwang | wangyibo | dog | f | 2018-08-05 | NULL |
+----------+-------------+----------+------+------------+-------+
3 rows in set (0.00 sec)
--总结一下:数据记录常见操作
--增加:
INSERT
--删除:
delete
--修改:
update :
--查询:
select
--二、 如何使用可视化工具操作数据库 --三、 如何在编程语言中操作数据库