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增删改--添加主外键、添加属性、删除主外键、改表名、获取系统当前时间等的更多相关文章

  1. MySQL数据库安装,MySQL数据库库的增删改查,表的增删改查,表数据的基本数据类型

    一 MySQL的安装 MySQL现在属于甲骨文公司,所以和java语言匹配度较高,同时甲骨文公司的另一种数据库为Oracle,两者同为关系型数据库,即采用关系模型来组织数据,以行和列的方法来存储数据的 ...

  2. 02 . Mysql基础操作及增删改查

    SQL简介 SQL(Structured Query Language 即结构化查询语言) SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语句四大 ...

  3. mysql 的基本操作总结--增删改查

    本文只是总结一下mysql 的基本操作,增删改查,以便忘记的时候可以查询一下 1.创建数据库 语法:CREATE DATABASES 数据库名; 例子: CREATE DATABASES studen ...

  4. 48.Python中ORM模型实现mysql数据库基本的增删改查操作

    首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...

  5. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  6. MySQL的DML语言(增删改)

    MySQL的DML语言(增删改) 补充说明,外键:不要使用外键,一切外键概念都在应用层解决. 补充说明,数据库的列,也就是字段名,尽量带上飘符号` 数据库存在的意义:数据存储和数据管理. 数据库:行( ...

  7. 使用 NodeJS+Express+MySQL 实现简单的增删改查

    关于node.js暂时记录如下,以后有时间一定学习 文章来自简书,作者:sprint,2016-07 使用 Node.js + Express+MySQL 实现简单的增删改查 https://www. ...

  8. php总结8——mysql函数库、增删改

    8.1 mysql函数库 php的函数   .php中用来操作mysql函数库的函数 常用函数 mysql_connect("主机名称/ip","用户名",&q ...

  9. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)

    day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库:    简称:DataBase ---->DB    数据库即存放数据的仓库, ...

  10. Python进阶----数据库引擎(InnoDB),表的创建,mysql的数据类型,mysql表的约束

    Python进阶----数据库引擎(InnoDB),表的创建,mysql的数据类型,mysql表的约束 一丶MySQL的存储引擎 什么是存储引擎:    MySQL中的数据用各种不同的技术存储在文件( ...

随机推荐

  1. What is HHVM?

    What is HHVM? HHVM is an open-source virtual machine designed for executing programs written in Hack ...

  2. Pointers to classes (From the note of my firend)

     Pointers to classes Objects can also be pointed to by pointers: Once declared, a class becomes a ...

  3. XXX系统发展综述(SSH+Jquery EasyUI)

    一个.该项目总体介绍 前一段时间的工作.我大概花了两三个月开发Web管理信息系统.用于框架集Struts2.3.1+Spring3.0+Hibernate3+Jquery EasyUI1.3.5.业务 ...

  4. 移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇

    根据前一篇文章<移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇>所述,因为Appium和MonkeyRunner有一个共同点--代码控制流程都是在客户端实现的. ...

  5. EXCEL导入GridView,然后再汇入数据库.

    原文:EXCEL导入GridView,然后再汇入数据库. 近日项目中有一个多笔料号要输入,我做了一个用javascript复制输入框的功能,可以输入多笔料号. 但是使用者反馈,料号太多,可能几百个料号 ...

  6. Linux多命令顺序运行

  7. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  8. RPL协议介绍

    RPL是IPv6 Routing Protocol for Low-Power and Lossy Networks的简称. 低功耗及有损网络(LLN)是一类内部链接和路由器都受限的网络,该网络下的路 ...

  9. Buildroot阅读笔记

    之前有写一篇文章:http://www.cnblogs.com/tfanalysis/p/3625430.html理清如何make menuconfig的问题,现在今天在无意间多注意了一下buildr ...

  10. NHibernate 数据查询之QueryOver<T>

    NHibernate 数据查询之QueryOver<T>   一.限制运算符 Where:筛选序列中的项目WhereNot:反筛选序列中的项目 二.投影运算符 Select:创建部分序列的 ...