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中的数据用各种不同的技术存储在文件( ...
随机推荐
- http协议报头信息和主体鉴别
http协议报头信息和主体是使用一个空行分开.这是什么空行?简单的说,那是,\r\n\r\n. 所以会server数据的回归\r\n\r\n结果分离,一个是标题信息.它是一个消息的文本. C#例如,下 ...
- MVC验证08-jQuery异步验证
原文:MVC验证08-jQuery异步验证 本文主要体验通过jQuery异步验证. 在很多的教材和案例中,MVC验证都是通过提交表单进行的.通过提交表单,可以很容易获得验证出错信息.因为,无论是客户端 ...
- Impala源代码分析---1
2.Impala源代码分析 參考链接:http://www.sizeofvoid.net/wp-content/uploads/ImpalaIntroduction2.pdf 本章開始进入源代码分析阶 ...
- TextArea中定位光标位置
原文:TextArea中定位光标位置 在项目中,遇到一个场景:希望能在TextArea中输入某条记录中的明细(明细较简单,没有附属信息,只用记录顺序和值即可,譬如用"+"号来作为明 ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
原文:重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 Web ...
- 深入浅出SQL Server 2008 分区函数和分区表
原文:深入浅出SQL Server 2008 分区函数和分区表 当我们数据量比较大的时候,我们需要将大型表拆分为多个较小的表,则只访问部门数据的查询就可以更快的运行,基本原理就是,因为要扫描的数据变的 ...
- jquery 拖动DIV
<html><head> <style type="text/css"> .show{ background:#7cd2f8; width:30 ...
- 使用Windows2003创建DHCP服务器 - 进阶者系列 - 学习者系列文章
Windows 2003提供的DHCP服务还是挺强大的.下面大概介绍下DHCP服务器的配置. 1. 通过控制面板安装DHCP服务 2. 打开DHCP配置项 3. 选择 新建作用域 4. 输入名 ...
- 【WebSocket初探
】
众所周知,socket是编写网络通信应用的基本技术,网络数据交换大多直接或间接通过socket进行.对于直接使用socket的client与服务端,一旦连接被建立则均可主动向对方传送数据,而对于使用更 ...
- [翻译]初识SQL Server 2005 Reporting Services Part 4
原文:[翻译]初识SQL Server 2005 Reporting Services Part 4 这一篇是关于SQL Server 2005 Reporting Services四篇文章中最后一篇 ...