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) ...
随机推荐
- Vsphere日记03.ESXi5.5.client
3.Vsphere ESXi 5.5 client Vsphere Client介绍 1.Vsphere Client定义 Vsphere client隶属于Vsphere套件,主要用于远程管理ESX ...
- bat脚本批处理打war打包
@echo =========================================== @echo 描述:打包脚本 @echo 作者:霍建国 @echo 日期:2018-03-13 @ec ...
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...
- Laravel5.1 -控制器(初步了解)
首先道个歉 这篇笔记是前两天就应该写的,可大K有点事儿要忙 就耽误了,今天抽空学了学控制器,并写个笔记分享下. 为什么要使用控制器 像我们之前写一些逻辑呢都是在Route(路由)中,搞得Route文件 ...
- Android-NDK编译
(2013-12-19 21:48:21 其实一切还是先看看官网的好,乱百度浪费时间.... http://developer.android.com/tools/sdk/ndk/index.htm ...
- Bootstrap(Web前端CSS框架)
官方定义: Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile fi ...
- NET Framework 4.5新特性 (一) 数据库的连接加密保护。
NET Framework 4.5 ado.net数据库连接支持使用SecureString内存流方式保密文本. 一旦使用这类操作,文本加密是私有不能共享的,并在不再需要时从计算机内存中删除. S ...
- Kubectl工具常用命令
创建namesapce kubectl create namespace {name} 注意:name只能为小写字母.数字和-的组合,且开头结尾为字母,一般格式为my-name 123-abc等. 创 ...
- 华盟网公开课 office 宏 主讲Alex
powershell github: veil-evasion macroshop unicorn https://github.com/trustedsec/unicorn metasploit ...
- iOS 成员变量和属性的区别
一. 成员变量 1.成员变量的作用范围: @public:在任何地方都能直接访问对象的成员变量 @private:只能在当前类的对象方法中直接访问,如果子类要访问需要调用父类的get/set方法 @p ...