Mysql中,表/列可以改名,database不能改名.
phpMyAdmin似乎有这功能? 他是建新库,把所有表复制到新库,再删旧库完成的.
删除一个数据库:drop database数据库名;
创建一个数据库:create database 数据库名 [charset 字符集]查看一下所有的库,怎么办?
Mysql>Show databases;
选库语句:Use库名
删除表:drop tablestu;
mysql> create table t1(id int,name varchar(10));
mysql> desct1;+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> show create tablet1;+-------+--------------------------+
|Table | CreateTable |
+-------+--------------------------+
| t1 | CREATE TABLE`t1` (
`id`int(11) DEFAULT NULL,
`name`varchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------+
//表的列,3大类,
数值型(全数字):
整型:int,存储数字1占据4个字节,32位00000000 00000000 00000000 00000001,最大2^32-1(42多亿),有符号的-2^31-----2^31-1,
TINYINT:一个字节8位,0-255,-128-127,SMALLINT:2个字节,0-65535,
MEDIUMINT:3个字节INT:4个字节BIGINT:8个字节
zerofill M : zerofill适合于学号编码等固定宽度的数字,可以用0填充至固定宽度,M是填充值多宽。
浮点型:float(M,D)[UNSIGNED][ZEROFILL],M总位数,D是小数点后几位 。 double大小不一样而已。
定点型decimal:更精确。
字符串:char(定长),varchar(变长),text,char(10)最大存10个字符,只给1个字符还是给10个字符长度,内部用空格补充,取出时右侧空格删除。varchar(10)最大10个字符,只给1个字符,还有一个额外数字标注varchar的长度。
20个字符以下用char,以上用varchar.速度上定长会快些。char最大255,varchar最大6万多。text,mediumtext,longtext:可以存60000多字符,
blob,mediumblob,longblob:二进制的,不存图像几乎不用blob,不用考虑字符集了,
ENUM枚举:性别,set(集合):
日期时间:2012-11-11,14:36:12
YEAR(1985)一个字节,Date(1998-12-11), Time(13:13:55),datetime(1998-12-13 13:56:09), 时间戳timestamp,
mysql> alter table t1 add unum tinyintunsigned;
mysql> insert into t1 values (1,'dvdfv',255);
mysql> alter table t1 add number tinyint(6) zerofill;//6位并且0填充,并且是无符号的。
mysql> alter table t1 add gender enum('一','二','三');
mysql> insert into t1 values ('ffr','一');//NULL查询不方便,避免值为null,create table t2 (id int not null default 0,name char(10) not null default '');//主键:
mysql> create table t1 (id int not null primary key,name char(10) not null default '');create table t1 (id int primary key auto_increment,name char(10) not null default '');//表设计:定长不定长分离,常用不常用分离。
mysql> alter table temp drop columnname;
mysql> alter table temp add name2 int not null default 0after subject;
mysql> alter table temp change name11 name111 varchar(3) default '---';----------------------------------------------------------------------------------------------------
//视图 : 虚拟表也是sql的查询结果,可以隐藏数据,视图存储的是单纯的语句,把建视图的语句和查视图的条件合并成查表的语句。也有可能视图语句比较复杂,很难和查询视图的语句合并,mysql可以先执行视图的创建语句,在内存中形成临时表,然后去查询临时表。
mysql> create view good_view as select goods_id,goods_name ,(market_price-shop_price) as less from goods limit 1 , 10;
mysql> select * fromgood_view;
mysql> show tables;//可以把视图查出来,视图和表对人来说没有区别,表改变视图也改变,
视图能不能更新删除添加:视图的每一行与物理表一一对应则可以。视图的行是由表多行经过计算的结果则不行。create algorithm = merge view v7 as select * from goods;//这样每次查视图的时候就是去拼接语句然后查表create algorithm = temp view v7 as select * from goods;//这样视图就是一个临时表create view v7 as select * from goods;//让数据库自己判断方式drop table t1;//删除视图drop view goods_v;//删除视图
mysql> desc good_view;//查看视图的表结构
mysql> show table status;//查看表的详细信息
mysql> show table status where name = 'temp';//查看哪张表的详细信息
mysql> rename table temp to temptemp;//改表的名字
mysql> delete from temptemp where score = 90;//删除数据
Truncate和delete区别:Truncate是删除一张表再重新建立一张表,自增涨的信息也没了。delete之后自增涨信息还有。
删除表:drop tablestu;
goods.frm:声明表的结构,建表时每一列是什么属性
goods.MYD:表内容
goods.MYI:索引信息
这3个文件有的没有,是因为引擎不一样,不指定引擎时现在默认是innodb,
乱码:字符集不一样,例如客户端是GBK的字符集(发送和接收的都是GBK),DB是utf8的(发送和接收的都是Utf8的),中间就要有一个连接转换器。
mysql> set character_set_results=gbk;//设置返回结果的结果集的字符集
mysql> select * from temp;+---------+-------+-------+---------+------+
| subject | name2 | score | name111 | name |
+---------+-------+-------+---------+------+
| ���� | 0 | 50 | 0 | 0 |
| Ӣ�� | 0 | 40 | 0 | 0 |
+---------+-------+-------+---------+------+
mysql> set character_set_results=utf8;
mysql> select * from temp;+---------+-------+-------+---------+------+
| subject | name2 | score | name111 | name |
+---------+-------+-------+---------+------+
| 语文 | 0 | 50 | 0 | 0 |
| 英语 | 0 | 40 | 0 | 0 |
+---------+-------+-------+---------+------+
mysql> set character_set_client=gbk;//设置客户端的字符集,cmd窗口就是客户端,
mysql> set character_set_results=gbk;//设置结果集的字符集
mysql> set character_set_connection=gbk;//设置连接器的字符集-------------------------------------------------------------------------------------------------
索引;数据的目录,能快速定位行所在的位置。
索引文件:树形结构,加速查找,降低了增删改的速度。一般在查询频繁的列上加,在重复率低的列上加。有可能索引文件比数据文件还要大。
索引的创建原则:1:不要过度索引2:在where条件最频繁的列上加.3:尽量索引散列值,过于集中的值加索引意义不大.key:普通索引,加快查询速度,unique:唯一索引,加快查询速度,还能约束数据(数据不能重复)primary:主键索引, 不能重复,把主键当成索引,唯一索引不一定是主键,主键必唯一,但是一张表上,只能有一个主键, 但是可以用一个或多个唯一索引.
fulltext :全文索引中文无效,英文环境下可以针对值中的某个单词,一般用第三方解决方案。
mysql> create table index_t(name char(10),email char(20),key name(name),unique key email(email));//建表的最后,给name加普通索引,索引的名字就叫name,给email加唯一索引名字叫email,
mysql> descindex_t;+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YE | MU | NULL | |
| email | char(20) | YE | UN | NULL | |
+-------+----------+------+-----+---------+-------+
mysql> select * from index_t;//unique key列不能重复+------+--------------+
| name | email |
+------+--------------+
| lisi | [email protected] |
| lisi | [email protected] |
+------+--------------+
mysql> create table t1(id int,name varchar(20),email varchar(20),primary key(id),key name(name),unique key email(email));//主键索引,
mysql> desct1;+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PR | NULL | |
| name | varchar(20) | YE | MU | NULL | |
| email | varchar(20) | YE | UN | NULL | |
+-------+-------------+------+-----+---------+-------+
create tabletest5 (
idint,
usernamevarchar(20),
schoolvarchar(20),
introtext,primary key(id),unique(username),index(school),
fulltext (intro)
) engine myisam charset utf8;
比如邮箱,只有前几个字符不同,后面都差不多的,可以只截取某一列的前几个字符然后建索引。
mysql> create table t1(id int primary key , email varchar(10), unique key email(email(3)));
多列索引,把多列看成一个整体,建立索引。create table t1 (xing char(2),ming char(10), keyxm(xing,ming));
mysql> select * from t1 where xing = '姚闻';+------+------+-----+
| xing | ming | zzz |
+------+------+-----+
| 姚闻 | 文文 | |
+------+------+-----+
mysql> explain select * from t1 where xing = '姚闻';//查看索引是否发挥作用+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys(有可能使用到的索引) | key (使用的索引) | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| 1 | SIMPLE | t1 | NULL | const | xing,xm | xing | 7 | const | 1 | 100 | NULL |
+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
mysql> explain select * from t1 where xing='姚闻' and ming='文文';+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
| 1 | SIMPLE | t1 | NULL | const | xing,ming,xm | xing | 7 | const | 1 | 100 | NULL |
+----+-------------+-------+------------+-------+---------------+------+---------+-------+------+----------+-------+
//冗余索引,某个列上有多个索引,比如建立了xm(xing,ming)联合索引又建立了ming(ming)索引,那么查询xing ming时xm索引发挥作用,查询xing时xm索引发挥作用,查询ming时ming索引发挥作用。ming这列就有2个索引。
showindex from t1;//查看索引+-------+------------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------+
| t1 | 0 | ming | 1 | ming | A | 4 | NULL | NULL | YES | BTREE | | |
| t1 | 1 | xm | 1 | xing | A | 4 | NULL | NULL | YES | BTREE | | |
| t1 | 1 | xm | 2 | ming | A | 4 | NULL | NULL | YES | BTREE | | |
+-------+------------+------+
mysql> show create tablet1;+-------+----------------------------------+
|Table | CreateTable |
+-------+----------------------------------+
| t1 | CREATE TABLE`t1` (
`xing`char(2) DEFAULT NULL,
`ming`char(10) DEFAULT NULL,
`zzz`char(20) NOT NULL DEFAULT '',UNIQUE KEY`ming` (`ming`),KEY`xm` (`xing`,`ming`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------+
mysql> alter table t1 drop index ming;//删除索引,
mysql> alter table t1 add primary key(xing);//增加主键索引,
mysql> alter table t1 drop primary key;//删除主键索引,-----------------------------------------------------------------------------------------------
函数: 列可以当成变量,可以运算。
mysql> select 3*3;+-----+
| 3*3 |
+-----+
| 9 |
+-----+
mysql> select floor(shop_price) from goods;//取整
mysql> select rand()*5+5,floor(shop_price) from goods limit 3;+-------------------+-------------------+
| rand()*5+5 | floor(shop_price) |
+-------------------+-------------------+
| 7.222229537761052 | 1388 |
| 6.174204159690257 | 58 |
| 9.204332338836354 | 68 |
+-------------------+-------------------+
mysql> select left(goods_name,3) from goods limit 3;//左边截3个+--------------------+
| left(goods_name,3) |
+--------------------+
| kd8 |
| ggg |
| htc |
+--------------------+
mysql> select right(goods_name,3) from goods limit 3;//右边截3个+---------------------+
| right(goods_name,3) |
+---------------------+
| 876 |
| ttt |
| 0耳机 |
+---------------------+
mysql> select concat(goods_name,shop_price) from goods limit 3;+-------------------------------+
| concat(goods_name,shop_price) |
+-------------------------------+
| kd8761388.00 |
| gggggttttttt58.00 |
| htc原装5800耳机68.00 |
+-------------------------------+
mysql> selectnow();+---------------------+
| now() |
+---------------------+
| 2017-12-27 16:19:31 |
+---------------------+
--------------------------------------------------------------------------------------------------
事务:
starttransaction;update temp set num=num+100 where id='a';update temp set num=num-100 where id='b';commit;
starttransaction;update temp set num=num+100 where id='a';rollback;
事务提交了是改不了的。