码迷,mamicode.com
首页 > 其他好文 > 详细

111

时间:2018-06-09 00:05:37      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:record   自动   enum   music   value   大小写   time   记录   one   

SQL(DDL、DML、DQL、DCL)
名词解释
运行数据库的服务器
数据库表的管理单元
表记录数据的管理单元
管理字段单元
字段类型,约束,长度
字符,数字日期,日期
约束不能为空,自动增长(序列)
技术分享图片

定义库
库名字不能为数字,区分大小写
创建库
create database xianyu 大写
查看库
show databases
进入库
use xianyu; 进入库大写
SELECT datebases 查看路径大写
DROP DATABASE删除
系统位置
/usr/local/mysql
/usr/local/mysql/data
练习
登录数据库
1、[root@hsl ~]# mysql -uroot -p‘QianFeng@123‘
查看数据库
2、mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
创建库
mysql> CREATE DATABASE xianyu;
Query OK, 1 row affected (0.00 sec)
进入库
mysql> use xianyu
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
查看当前库位置表
mysql> show tables;
+------------------+
| Tables_in_xianyu |
+------------------+
| test1 |
+------------------+
1 row in set (0.00 sec)
整数类型测试:tinyint,int
tinyint为127 7位2进制
INT有符号型最大2147483647
作用:用于存储用户的年龄、游戏的Level、经验值等。
1、创建一个表
mysql> create table test2(tinyint_test tinyint, int_test int );
创建一个表 test2(field列名 type字段类型 名字 type)
2、查看表结构
mysql> desc test2;
+--------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| tinyint_test | tinyint(4) | YES | | NULL | |
| int_test | int(11) | YES | | NULL | |
+--------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
Field字段名称(列名) TYPE字段类型(字?数?日?) NULL KEY DEFAULT EXTRA 略
3、插入数值
mysql> insert into test1 values(111,111); 以此插入
Query OK, 1 row affected (0.00 sec)
插入不正常信息
mysql> insert into test1(tinyint_test) values(128); 指定插入了的数据
ERROR 1264 (22003): Out of range value for column ‘tinyint_test‘ at row 1
查看插入正常信息
mysql> select * from test1;
+--------------+----------+
| tinyint_test | int_test |
+--------------+----------+
| 111 | 111 |
+--------------+----------+
1 row in set (0.00 sec)
删除表名
drop table 表名
删除表内容
delete from 表名
约束条件unsigned限定只能存正值(无符号)
mysql> create table test2(tinyint_test tinyint unsigned,int_test int unsigned); #定义unsigned
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test2(tinyint_test) values(255) #tianyint 为255因为unsigned占一位,原因8位2进制
Query OK, 1 row affected (0.00 sec)
mysql> insert into test2(int_test) values(2144444444); #insert之内
Query OK, 1 row affected (0.00 sec)
mysql和mariadb不同。
mysql提示输入错误
mariadb会输入0到表中。
但结果是肯定的,无符号只能输入正值
整数型只是显示宽度
mysql> create table t1 (id1 int, id2 int(6));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(222222222,222222222);#插入大于INT宽度限制的值,仍然可以存储。但是不能超过上限2147483647!!
数据零填充
mysql> create table t2( id1 int zerofill, in2 int(6) zerofill ); #添加表
mysql> insert into t2 values(2,222);#写入数据
mysql> select * from t2;#查看信息
+------------+--------+
| id1 | in2 |
+------------+--------+
| 0000000002 | 000222 |
+------------+--------+
1 row in set (0.00 sec)
浮点数类型测试:float
创建
mysql> create table test4(float_test float(5,2));#创建test表,一共5位,3位正数,小数2位
Query OK, 0 rows affected (0.02 sec)
查看
mysql> desc test4 ;#查看表类型
+------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| float_test | float(5,2) | YES | | NULL | |
+------------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
插入
mysql> insert into test4 values(111.258) ;
Query OK, 1 row affected (0.01 sec)
查询 数值会进位
mysql> select * from test4;
+------------+
| float_test |
+------------+
| 111.22 |
| 111.25 |
| 111.26 |
+------------+
3 rows in set (0.00 sec)
时间和日期类型测试:year、date、time、datetime、timestamp
作用:用于存储用户的注册时间,文章的发布时间,文章的更新时间,员工的入职时间等
日期date和时间time类型测试
创建
mysql> create table test_time( d date, t time ,dt datetime);
查看
mysql> desc test_time
-> ;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
插入
mysql> insert into test_time values(now(),now(),now()) #插入时间now
-> ;
Query OK, 1 row affected, 1 warning (0.01 sec)
查看
mysql> select * from test_time #字符断会相应截取时间date,time,datetime
-> ;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2018-06-06 | 06:54:19 | 2018-06-06 06:54:19 |
+------------+----------+---------------------+
1 row in set (0.00 sec)
年YEAR类型测试
创建
mysql> create table t3(born_year year);
查看
mysql> desc t3 ;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| born_year | year(4) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
插入数据#12为20年,80为默认19年
mysql> insert into t3 values(12),(80);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看
mysql> select * from t3;
+-----------+
| born_year |
+-----------+
| 2012 |
| 1980 |
+-----------+
2 rows in set (0.00 sec)
字符串类型测试:CHAR、VARCHAR
用于存储用户的姓名、爱好、发布的文章等
字符、变长字符
mysql> create table vc ( v varchar(4), c char(4) ); #
查看
mysql> desc vc
-> ;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| v | varchar(4) | YES | | NULL | |
| c | char(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
插入数据
mysql> insert into vc values(‘a‘,‘a‘);
Query OK, 1 row affected (0.00 sec)
mysql> insert into vc values(‘ab ‘,‘ab ‘);
Query OK, 1 row affected (0.01 sec)
查询
mysql> select * from vc;
+------+------+
| v | c |
+------+------+
| a | a |
| ab | ab |
+------+------+
2 rows in set (0.00 sec)
函数直观
mysql> select concat(v,‘=‘), concat(c,‘=‘) from vc;
+---------------+---------------+
| concat(v,‘=‘) | concat(c,‘=‘) |
+---------------+---------------+
| a= | a= |
| ab = | ab= |
+---------------+---------------+
2 rows in set (0.01 sec)
二进制字符 了解
字符串类型测试:BINARY、VARBINARY
BINARY 和 VARBINARY类似于CHAR 和 VARCHAR,不同的是它们包含二进制字符而不包含
非二进制字符串
mysql> create table binary_t (c binary(3));
Query OK, 0 rows affected (0.03 sec)
mysql> desc binary_t;
+-------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| c | binary(3) | YES | | NULL | |
+-------+-----------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into binary_t set c=‘aaa‘;
Query OK, 1 row affected (0.00 sec)
mysql> select *,hex(c) from binary_t;
+------+--------+
| c | hex(c) |
+------+--------+
| aaa | 616161 |
+------+--------+
1 row in set (0.00 sec)
枚举类型、集合类型:ENUM类型,SET测试
创建表
mysql> use school
mysql> create table student3(
name varchar(50),
sex enum(‘m‘,‘f‘),
hobby set(‘music‘,‘book‘,‘game‘,‘disc‘)
);
Query OK, 0 rows affected (0.31 sec)
查看表
mysql> show create table student3\G
*************************** 1. row ***************************
Table: student3
Create Table: CREATE TABLE `student3` (
`name` varchar(50) DEFAULT NULL,
`sex` enum(‘m‘,‘f‘) DEFAULT NULL,
`hobby` set(‘music‘,‘book‘,‘game‘,‘disc‘) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
添加数据
mysql> insert into student3 values (‘tom‘,‘m‘,‘book,game‘);
Query OK, 1 row affected (0.01 sec)
mysql> insert into student3 values(‘tom‘,‘m‘,‘film‘)
-> ;
查看
mysql> select * from student3;
+------+------+-----------+
| name | sex | hobby |
+------+------+-----------+
| tom | boy | book,game |
+------+------+-----------+
1 row in set (0.00 sec)
mysql -e
测试
TINYINT 127数字 7位2进程
INT 21亿 数字
创建表
create
show tables 查看
select * from xiany.test1插入数据

111

标签:record   自动   enum   music   value   大小写   time   记录   one   

原文地址:https://www.cnblogs.com/hsl520/p/9157893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!