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中的数据用各种不同的技术存储在文件( ...
随机推荐
- 最新HTML BroadcastChannel API引荐
HTML BroadcastChannel API 当前浏览器中只有Firefox38唯一能支持BroadcastChannel API(在编写本文的时间点),而Firefox38官方宣称要到2015 ...
- SVN:One or more files are in a conflicted state
解决代码冲突 如果commit时出现"You have to update your work copy first."红色警告,说明版本库中的此文件已经被其他人修改了. 请先点& ...
- C#创建服务及使用程序自动安装服务
.NET创建一个即是可执行程序又是Windows服务的exe 不得不说,.NET中安装服务很麻烦,即要创建Service,又要创建ServiceInstall,最后还要弄一堆命令来安装和卸载. 今天给 ...
- Bootstrap的栅格系统
Bootstrap的栅格系统 上一节:ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用 源码下载:点我下载 要做一个完整的系统,除了需要MVC这样的B/S框架及EF这 ...
- C#6.0 中的那些新特性
C#6.0 中的那些新特性 前言 VS2015在自己机器上确实是装好了,费了老劲了,想来体验一下跨平台的快感,结果被微软狠狠的来了一棒子了,装好了还是没什么用,应该还需要装Xarmain插件,配置一些 ...
- JQuery在Ajax的Post提交中国乱码的解决方案
介绍: 在JQuery的Ajax POST要求,一个要求.中国的背景之中,乱码,如何解决呢? 问题简介: var regid = $('#oregion').combobox('getValue'); ...
- Ubuntu12.04环境搭建遇到的问题和建议(一个)
后的新公司需要在Ubuntu12.04在结构Android开发环境,在这个过程中,我们还是会遇到很多问题,这里记录.为了方便自己的未来,有人谁需要参考.从网络! 1. Q:在终端: sudo apt- ...
- 开发环境准备:Ruby on Rails开发环境配置
开发环境准备:Ruby on Rails开发环境配置 前情回顾 上次讲到Vmware虚拟机的安装配置以及Scientific Linux 6.X系统的安装.这回我们的主要任务是在Linux操作系统上完 ...
- Smarty注释代码
所有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的. 例如,我们假定你在使用默认定界符. 在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变 ...
- beanutils设置参数和获取参数
public class Employee implements DynaBean { private String firstName="李"; private Str ...