MySQL的数据类型,MySQL增删改--添加主外键、添加属性、删除主外键、改表名、获取系统当前时间等
ls /etc/rc.d/init.d/mysql56
service mysql56 start
ps aux |grep "mysql"|grep "socket=" --color
mysql -S/var/run/mysqld/mysql56.sock
[root@localhost ~]# service mysql56 start
Starting MySQL.. SUCCESS!
[root@localhost ~]# mysql -S/var/run/mysqld/mysql56.sock
vim /etc/my.cnf
作为mysql这个客户端程序的配置文件
[mysql]
socket=/var/run/mysqld/mysql56.sock
prompt=\u@\h>
create table table s(stuID int zerofill);
alter database sx charset=utf8 创建数据库时将字符集设为utf8,这样在sx数据库下建的表的字符集默认都为utf8,主要是避免出现乱码。
alter table s add stuName varchar(32) default 'tom' not null; 主键也是一种特殊的索引,自动增长列必须是索引
alter table s add primary key(stuID) ; 给s表添加主键
alter table s modify stuID int auto_increment; 将s表中的stuID改为自增长变量
alter table s modify stuID int zerofill auto_increment;
alter table s modify stuID int(4) zerofill auto_increment;
mysql> insert into s(stuName) select '';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> set names utf8; 将客户端输入的名字转为utf8,保证客户端与表端字符集一致
Query OK, 0 rows affected (0.00 sec)
mysql> select last_insert_id(); 查询最后一次插入数据的id号
+------------------+
| last_insert_id() |
+------------------+
| 5 |
+------------------+
1 row in set (0.00 sec)
show create table s; 显示创建表的过程
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| s | CREATE TABLE `s` (
`stuID` int(4) unsigned zerofill NOT NULL AUTO_INCREMENT,
`stuName` varchar(32) NOT NULL DEFAULT 'tom',
PRIMARY KEY (`stuID`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> desc s; 显示表结构
+---------+--------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------+----------------+
| stuID | int(4) unsigned zerofill | NO | PRI | NULL | auto_increment |
| stuName | varchar(32) | NO | | tom | |
+---------+--------------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
钱,decimal 一般用oracl数据库
不太精确的用double float4B double8B
utf8下一个字符绝大多数占3个字节
mysql> select length('中国人');
+---------------------+
| length('中国人') |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
| 4 |
+----------------+
1 row in set (0.01 sec)
text数据类型不区分大小写
set数据类型,一般用enum,不用set
set不过是取值范围的限制
mysql> alter table s add ab set('L','M','N','O');
Query OK, 5 rows affected (0.09 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> desc s;
+---------+--------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------+----------------+
| stuID | int(4) unsigned zerofill | NO | PRI | NULL | auto_increment |
| stuName | varchar(32) | NO | | tom | |
| ab | set('L','M','N','O') | YES | | NULL | |
+---------+--------------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into s(ab) values('L');
Query OK, 1 row affected (0.00 sec)
mysql> select * from s ;
+-------+---------+------+
| stuID | stuName | ab |
+-------+---------+------+
| 0001 | tom | NULL |
| 0002 | tom | NULL |
| 0003 | tom | NULL |
| 0004 | | NULL |
| 0005 | ?? | NULL |
| 0006 | tom | L |
+-------+---------+------+
6 rows in set (0.00 sec)
mysql> insert into s(ab) values('L,M,N,O');
Query OK, 1 row affected (0.00 sec)
mysql> select * from s ;
+-------+---------+---------+
| stuID | stuName | ab |
+-------+---------+---------+
| 0001 | tom | NULL |
| 0002 | tom | NULL |
| 0003 | tom | NULL |
| 0004 | | NULL |
| 0005 | ?? | NULL |
| 0006 | tom | L |
| 0007 | tom | L,M,N,O |
+-------+---------+---------+
7 rows in set (0.00 sec)
日期时间
datetime
date
time
year
timestamp
mysql> -- hiredate 2012-1-2 3:4:5
mysql> alter table stu change a hiredate datetime; 将stu表中的a改名为hiredate,数据类型为datetime,即年月日时分秒形式
Query OK, 3 rows affected (0.28 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into stu(hiredate,sno) select '2013-1-2',4;
Query OK, 1 row affected, 1 warning (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into stu(hiredate,sno) select '2013-1-2 03:04:05',5;
Query OK, 1 row affected, 1 warning (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+-----+----------+------+--------+-------+---------------------+
| sno | sname | sage | deptno | score | hiredate |
+-----+----------+------+--------+-------+---------------------+
| 0 | | NULL | NULL | 1.23 | NULL |
| 1 | dsfk5945 | 1515 | NULL | NULL | NULL |
| 2 | dsfk5945 | 1515 | 1 | NULL | NULL |
| 4 | | NULL | NULL | NULL | 2013-01-02 00:00:00 |
| 5 | | NULL | NULL | NULL | 2013-01-02 03:04:05 |
+-----+----------+------+--------+-------+---------------------+
5 rows in set (0.00 sec)
mysql> create table stu2(a datetime,b timestamp); 创建表stu2,a的数据类型为datetime,b的数据类型为timestamp,timestamp会自动获取系统的当前时间
Query OK, 0 rows affected (0.06 sec)
mysql> desc stu2;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| a | datetime | YES | | NULL | |
| b | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
mysql> select current_timestamp; 获取当前时间
+---------------------+
| current_timestamp |
+---------------------+
| 2013-07-18 10:56:52 |
+---------------------+
1 row in set (0.03 sec)
mysql> insert into stu2(a) select '2013-1-2 2:3:4';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from stu2;
+---------------------+---------------------+
| a | b |
+---------------------+---------------------+
| 2013-01-02 02:03:04 | 2013-07-18 10:57:46 |
+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> select now(); 取出当前服务器的时间
+---------------------+
| now() |
+---------------------+
| 2013-07-18 10:58:17 |
+---------------------+
1 row in set (0.00 sec)
mysql> insert into stu2 select now(),now();
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from stu2;
+---------------------+---------------------+
| a | b |
+---------------------+---------------------+
| 2013-01-02 02:03:04 | 2013-07-18 10:57:46 |
| 2013-07-18 10:59:22 | 2013-07-18 10:59:22 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> show variables like '%zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
mysql> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)
设计数据库的时候,三种完整性:
1、实体完整性:通过主键来保证
2、引用的完整性:通过外键来保证
mysql> alter table stu add foreign key(deptno) references dept(deptno); 将dept表中的deptno作为stu表中的外键
3、用户自定义的完整性,域的完整性
create table stu(
stuID int,
age int check(age >=0 and age<=120) --在sqlServer中可以实现,但在Mysql中还实现不了
);
check约束在客户端实现,不在db中实现
mysql> set foreign_key_checks=on;
Query OK, 0 rows affected (0.00 sec)
mysql> set foreign_key_checks=off;
Query OK, 0 rows affected (0.00 sec)
sql
structured query language
client->server
ddl definition create dabetase table alter drop table
dml insert delete update select
dcl control grant deny revoke
sql标准:
create table ...;
ansi,iso/iec
ansi C iso c
sql -86
sql:2003
ansi c iso c
gcc
在mysql中,and的优先级比or高
mysql> create database if not exists sx charset=utf8; 创建数据库的时候就指定字符集,这样在数据库下创建表时,字符集也都是创建数据库时指定的字符集,但是建议在创建表时指定字符集
在Mysql中不能改数据库名,但在微软的sqlServer中可以改
[root@localhost ~]# mysql ds -e "set names utf8;select * from stu;"
+-----+---------------+------+--------+-------+----------+
| sno | sname | sage | deptno | score | hiredate |
+-----+---------------+------+--------+-------+----------+
| 9 | å¼ ä¸‰ | NULL | NULL | NULL | NULL |
mysql> desc dept;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno | int(11) | NO | PRI | NULL | |
| deptname | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create table t1 like dept; 创建与dept结构一致的表t1
Query OK, 0 rows affected (0.15 sec)
mysql> desc t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno | int(11) | NO | PRI | NULL | |
| deptname | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | dsh |
| 2 | sdfg |
| 3 | swgrwg |
+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from t1;
Empty set (0.00 sec)
mysql> create table t2 select * from dept; 创建一个与dept结构一致的表t2并将dept中的数据导入到t2中
Query OK, 3 rows affected (0.10 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> desc t2;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno | int(11) | NO | | NULL | |
| deptname | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t2;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | dsh |
| 2 | sdfg |
| 3 | swgrwg |
+--------+----------+
3 rows in set (0.00 sec)
mysql> create table t3 select * from dept where deptno>2; 创建表时刷选数据导入t3中
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 3 | swgrwg |
+--------+----------+
1 row in set (0.00 sec)
alter table t3 rename table3; 将表名t3改为table3
alter table table3 add deptLeader varchar(32) not null; 添加属性deptLeader
alter table table3 drop deptLeader; 删除属性deptLeader
alter table table3 modify deptName varchar(64) not null default'cc';
alter table table3 change deptno deptID int; 修改属性名称
alter table table3 add primary key(deptID); 添加主键
alter table table3 drop primary key; 删除主键
alter table stu2 engine=myisam; 设置引擎
alter table stu2 charset=utf8; 设置字符集
mysql> insert into dept values(8,'a1'),(9,'a2'),(10,'a3'); (微软的sqlserver不支持这样一次性插入多条数据,逗号隔开,微软的sqlserver2008可以支持多条数据的插入eg:insert into dept values(8,'a1') (9,'a2') (10,'a3');只是中间没逗号而已)
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+-----+---------+------+--------+-------+----------+
| sno | sname | sage | deptno | score | hiredate |
+-----+---------+------+--------+-------+----------+
| 9 | zhaoliu | 20 | 2 | NULL | NULL |
+-----+---------+------+--------+-------+----------+
1 row in set (0.00 sec)
mysql> truncate table stu; 将stu表中的数据都删除
Query OK, 0 rows affected (0.03 sec)
mysql> select * from stu;
Empty set (0.00 sec)
mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | dsh |
| 2 | sdfg |
| 3 | swgrwg |
| 5 | sales |
| 6 | NULL |
| 8 | a1 |
| 9 | a2 |
| 10 | a3 |
+--------+----------+
8 rows in set (0.00 sec)
mysql> select deptno into @a from dept where deptname='dsh';
Query OK, 1 row affected (0.00 sec)
mysql> update stu set age=age*1.1 where deptno=@a;
mysql> select @a;
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
MySQL的数据类型,MySQL增删改--添加主外键、添加属性、删除主外键、改表名、获取系统当前时间等的更多相关文章
- MySQL数据库安装,MySQL数据库库的增删改查,表的增删改查,表数据的基本数据类型
一 MySQL的安装 MySQL现在属于甲骨文公司,所以和java语言匹配度较高,同时甲骨文公司的另一种数据库为Oracle,两者同为关系型数据库,即采用关系模型来组织数据,以行和列的方法来存储数据的 ...
- 02 . Mysql基础操作及增删改查
SQL简介 SQL(Structured Query Language 即结构化查询语言) SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语句四大 ...
- mysql 的基本操作总结--增删改查
本文只是总结一下mysql 的基本操作,增删改查,以便忘记的时候可以查询一下 1.创建数据库 语法:CREATE DATABASES 数据库名; 例子: CREATE DATABASES studen ...
- 48.Python中ORM模型实现mysql数据库基本的增删改查操作
首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...
- python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查
python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...
- MySQL的DML语言(增删改)
MySQL的DML语言(增删改) 补充说明,外键:不要使用外键,一切外键概念都在应用层解决. 补充说明,数据库的列,也就是字段名,尽量带上飘符号` 数据库存在的意义:数据存储和数据管理. 数据库:行( ...
- 使用 NodeJS+Express+MySQL 实现简单的增删改查
关于node.js暂时记录如下,以后有时间一定学习 文章来自简书,作者:sprint,2016-07 使用 Node.js + Express+MySQL 实现简单的增删改查 https://www. ...
- php总结8——mysql函数库、增删改
8.1 mysql函数库 php的函数 .php中用来操作mysql函数库的函数 常用函数 mysql_connect("主机名称/ip","用户名",&q ...
- Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库: 简称:DataBase ---->DB 数据库即存放数据的仓库, ...
- Python进阶----数据库引擎(InnoDB),表的创建,mysql的数据类型,mysql表的约束
Python进阶----数据库引擎(InnoDB),表的创建,mysql的数据类型,mysql表的约束 一丶MySQL的存储引擎 什么是存储引擎: MySQL中的数据用各种不同的技术存储在文件( ...
随机推荐
- ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则
原文:ASP.NET MVC基于标注特性的Model验证:一个Model,多种验证规则 对于Model验证,理想的设计应该是场景驱动的,而不是Model(类型)驱动的,也就是对于同一个Model对象, ...
- C# winform调用WebBrowser经典怪问题总结
原文:C# winform调用WebBrowser经典怪问题总结 最近一直研究网页数据采集,单单采集数据,其实HtmlAgilityPack就足够了. 对HtmlAgilityPack感兴趣的可以到这 ...
- JUnit介绍
8.1.1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试,其设计非常小巧,但功能却非常强 大. 下面是JUnit一些特性的总结: — 提供的API可以让开发人员写出测试结果明 ...
- Python 2.7.3的文件编码问题,print在控制台下面中文乱码问题,以及推荐做法
情况:文件乱码,在cmd上输出print也乱码.解决方案:统一为gbk的简体中文编码方式.步骤如下: 1.每个py文件使用[简体中文(GB2312)- 代码页 936]格式保存,行尾为[Windows ...
- SQL点滴31—SQL语句中@@IDENTITY和@@ROWCOUNT区别
原文:SQL点滴31-SQL语句中@@IDENTITY和@@ROWCOUNT区别 SQL语句中@@IDENTITY和@@ROWCOUNT区别 在一条 INSERT.SELECT INTO 或大容量复制 ...
- Spring源深和六系列 CreateBean过程
blog宗旨:用图说话. 这一章的图讲述了createBean的过程.到这里spring容器就能够完毕IOC的整个过程,拿到我们须要的对象. 下一章我们接着来看一看AOP完毕的过程. 附:文件夹 Sp ...
- [译]Java垃圾回收器的类型
说明:这篇文章来翻译来自于Javapapers 的Types of Java Garbage Collectors 在这部分的教程中我们将讲到可使用的四种不同类型的Java垃圾回收器.垃圾回收是Jav ...
- 网站开发常用jQuery插件总结(二)弹出层插件Lightbox
网站开发过程中,为了增加网站交互效果,我们有时需要在当前页面弹出诸如登陆.注册.设置等窗口.而这些窗口就是层,弹出的窗口就是弹出层.jQuery中弹出层插件很多,但有些在html5+css3浏览器下, ...
- Scala Web 框架——Lift(一)准备工作
[Lift]Scala Web 框架——Lift(一)准备工作 Lift 官方网站:http://liftweb.net/ 下载 http://liftweb.net/download 下载.zip压 ...
- 【转】仿QQ5.0侧滑菜单ResideMenu
本文由孙国威 原创.如需转载,请注明出处! 原文:http://blog.csdn.net/manoel/article/details/39013095 为了后续对这个项目进行优化,比如透明度动画. ...