MySQL basics
@1: MySQL有三大类数据类型, 分别为数字、日期\时间、字符串, 这三大类中又更细致的划分了许多子类型:
数字类型
整数: tinyint、smallint、mediumint、int、bigint
浮点数: float、double、real、decimal
日期和时间
date、time、datetime、timestamp、year
字符串类型
字符串: char、varchar
文本: tinytext、text、mediumtext、longtext
二进制(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob
@2: 在MySQL中创建数据库中的表:
mysql> use db1;
Database changed
mysql> show tables;
Empty set (0.00 sec) mysql> create table users(
-> id int(2) not null primary key auto_increment,
-> username varchar(40),
-> passwd text,
-> email text)default charset=utf8;
Query OK, 0 rows affected (0.05 sec) mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| users |
+---------------+
1 row in set (0.00 sec) mysql> desc users;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| username | varchar(40) | YES | | NULL | |
| passwd | text | YES | | NULL | |
| email | text | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec) mysql> insert into users(username, passwd, email)
-> values("lxw", "123", "lxw.ucas@gmail.com");
Query OK, 1 row affected (0.01 sec) mysql> select * from users;
+----+----------+--------+--------------------+
| id | username | passwd | email |
+----+----------+--------+--------------------+
| 1 | lxw | 123 | lxw.ucas@gmail.com |
+----+----------+--------+--------------------+
1 row in set (0.01 sec)
desc table_name命令用于显示表的结构.
@3: alter table 语句用于创建后对表的修改, 基础用法如下:
添加列 alter table 表名 add 列名 列数据类型 [after 插入位置];
在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
修改列 alter table 表名 change 列名称 列新名称 新数据类型;
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";
将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
删除列 alter table 表名 drop 列名称;
删除 birthday 列: alter table students drop birthday;
重命名表 alter table 表名 rename 新表名;
重命名 students 表为 workmates: alter table students rename workmates;
删除整张表 drop table 表名;
删除 workmates 表: drop table workmates;
删除整个数据库 drop database 数据库名;
删除 samp_db 数据库: drop database samp_db;
@4:
mysql> select * from users;
+----+----------+--------+-------------------+
| id | username | passwd | email |
+----+----------+--------+-------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | lxw | 123 | lxwin@foxmail.com |
| 3 | lxw | 123 | lxwin@foxmail.com |
+----+----------+--------+-------------------+
3 rows in set (0.04 sec) mysql> select * from users where id>2&username="lxw";
Empty set, 3 warnings (0.00 secG mysql> select * from users where id>2 and username="lxw";
+----+----------+--------+-------------------+
| id | username | passwd | email |
+----+----------+--------+-------------------+
| 3 | lxw | 123 | lxwin@foxmail.com |
+----+----------+--------+-------------------+
1 row in set (0.02 sec) mysql> select * from users where username like "%xw%";
+----+----------+--------+-------------------+
| id | username | passwd | email |
+----+----------+--------+-------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | lxw | 123 | lxwin@foxmail.com |
| 3 | lxw | 123 | lxwin@foxmail.com |
+----+----------+--------+-------------------+
3 rows in set (0.00 sec) mysql> update users set username="wxl" where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from users;
+----+----------+--------+-------------------+
| id | username | passwd | email |
+----+----------+--------+-------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | wxl | 123 | lxwin@foxmail.com |
| 3 | lxw | 123 | lxwin@foxmail.com |
+----+----------+--------+-------------------+
3 rows in set (0.00 sec) mysql> select * from users;
+----+----------+--------+---------------------+
| id | username | passwd | email |
+----+----------+--------+---------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | wxl | 456 | wxl24life@gmail.com |
| 3 | lxw | 123 | lxwin@foxmail.com |
+----+----------+--------+---------------------+
3 rows in set (0.00 sec) mysql> update users set username="lfc", passwd="789" where id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from users;
+----+----------+--------+---------------------+
| id | username | passwd | email |
+----+----------+--------+---------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | wxl | 456 | wxl24life@gmail.com |
| 3 | lfc | 789 | lxwin@foxmail.com |
+----+----------+--------+---------------------+
3 rows in set (0.00 sec) mysql> update users set username="lfc", passwd=passwd+1, email=email+1 where id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> desc users;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| username | varchar(40) | YES | | NULL | |
| passwd | text | YES | | NULL | |
| email | text | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec) mysql> select * from users;
+----+----------+--------+---------------------+
| id | username | passwd | email |
+----+----------+--------+---------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | wxl | 456 | wxl24life@gmail.com |
| 3 | lfc | 791 | 1 |
+----+----------+--------+---------------------+
3 rows in set (0.00 sec) mysql> delete from users where id = 3;
Query OK, 1 row affected (0.01 sec) mysql> select * from users;
+----+----------+--------+---------------------+
| id | username | passwd | email |
+----+----------+--------+---------------------+
| 1 | lxw | 123 | lxwin@foxmail.com |
| 2 | wxl | 456 | wxl24life@gmail.com |
+----+----------+--------+---------------------+
2 rows in set (0.00 sec)
where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如
=、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组
合查询。
Reference:
21分钟MySQL入门教程:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#c8
MySQL basics的更多相关文章
- 数据库 —— mySQL的基本操作
学习资源: 0.学习教程 :MySQL 教程(runoob.com) (MySQL Tutorial)turtorialPoint 1.学习帮助手册与平台: MySQL学习平台 英文手册chm ...
- python下的MySQL数据库编程
https://www.tutorialspoint.com/python/python_database_access.htm if you need to access an Oracle dat ...
- 数据库 —— mySQL相关
目录 使用笔记 问题解决 资源链接 1.使用笔记 1.命令行客户端显示无法调整表格显示宽度,可以考虑在查询语句尾后添加 \G; 2.插入语句字符串转时间:link 2.问题解决 1.不能显示插入中文字 ...
- zabbix-3.0.3 mysql表分区的方法
目的:解决mysql空间越来越大,mysql性能出现瓶颈,zabbix会无端出现大量agent超时报警 中间遇到一个mysql问题:5.1版本的mysql不支持分表(其实是支持的,需要重新编译mysq ...
- JDBC连接MySQL 方法 实例及资料收集
JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...
- Percona 开始尝试基于Ceph做上层感知的分布式 MySQL 集群,使用 Ceph 提供的快照,备份和 HA 功能来解决分布式数据库的底层存储问题
本文由 Ceph 中国社区 -QiYu 翻译 英文出处:Using Ceph with MySQL 欢迎加入CCTG Over the last year, the Ceph world drew m ...
- MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0
用zabbix的同学都知道,一台服务器监视几百几千台服务器,一个服务器几十个item,长年下来数据量是很惊人的. 而zabbix自带的housekeeping功能,默认状态下的删除速度完全跟不上数据增 ...
- MySQL vs. MongoDB: Choosing a Data Management Solution
原文地址:http://www.javacodegeeks.com/2015/07/mysql-vs-mongodb.html 1. Introduction It would be fair to ...
- Some current MySQL Architecture writings
Posted on 19/09/2014 by Stewart Smith So, I’ve been looking around for a while (and a few times now) ...
随机推荐
- ModelSim高级使用进阶_1_do文件和批处理文件使用_Camp
https://wenku.baidu.com/view/50fb251914791711cc7917fd.html https://wenku.baidu.com/view/73187dcefe47 ...
- 图像视频编码和FFmpeg(2)-----YUV格式介绍和应用
本文不讲FFmpeg,而是讲YUV图像格式.因为摄像头拍摄出来的原始图像一般都是YUV格式.在FFmpeg中,视频是通过多张YUV图像而得到. YUV图像格式是什么,这个可以看一下维基百科.这个超链接 ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- Visual Studio 调试小技巧-从查看窗口得到更多信息(转)
原文地址:http://blog.csdn.net/cadcisdhht/article/details/5651488
- java & c sharp 的关联
第一.java是真正的与平台无关,c sharp不是,他只是口头上的与平台无关,最后,却要靠别人来实现非微软平台的类库. 第二.java中的类名.class 和c#的 typeof(类名)或者getT ...
- C/C++知识要点4——printf函数以及cout的计算顺序
printf函数的计算顺序:先从右到左压栈,然后从左到右出栈. 例程: #include"stdio.h" int main() { int arr[] = { 1, 2, 3, ...
- openWRT自学---对官方的开发指导文档的解读和理解 记录3:一些常用方法
1.约定 configuration files follow the convention: <name>.conf init files follow the convention: ...
- priority_queue优先队列/C++
priority_queue优先队列/C++ 概述 priority_queue是一个拥有权值观念的queue,只允许在底端加入元素,并从顶端取出元素. priority_queue带有权值观念,权值 ...
- UML结构体系简介
一.UML的结构 UML有3种基本的构造块,分别是事物(元素).关系和图.事物是UML中重要的组成部分.关系把事物紧密联系在一起.图是很多有相互相关的事物的组. 二.UML的事物 UML中的事物也称为 ...
- SVD分解的c++代码(Eigen 库)
使用Eigen 库:进行svd分解,形如 A = U * S * VT. JacobiSVD<MatrixXd> svd(J, ComputeThinU | ComputeThinV); ...