create table test(
-> tid int(10) not null,
-> tname varchar(100) not null,
-> tdate datetime not null default '0000-00-00',
-> primary key (tid));
查看表结构
desc test
添加列alter table test add(tage int(3));
修改列的默认值alter table test alter tage set default '0';
去掉列的默认值alter table test alter tage drop default;
删除列alter table test drop column tage;
插入数据insert into test(tid,tname,tdate) value(1,'yangjuqi','2008-03-21');
查询数据select * from test;
模糊查询select * from test where tname like '%杨%';
修改数据update test set tname='张三' where tid='2';
删除数据delete from test where tid='2';
删除表drop table test;
重命名表alter table test rename testbak;
分页查询(limit 起始行,取多少行)select * from testbak limit 2,1;
显示数据库版本select version();
显示当前时间select current_date;
修改用户密码MySQLadmin -uroot -proot password yangjuqi
将查询出的数据写入文件select * from test into outfile "d:/test.txt" fields terminated by ",";
查看数据库状态status;
MySQL基本操作查看所有编码show variables like 'character_set_%';
导入sql文件命令source d:/MySQL.sql;
mysql> grant all on oldboy.* to oldboy@'localhost' identified by '123456';
方法二:
mysql> create user oldoy@'localhost' identified by 'oldboy123';
mysql> grant all on oldboy.* to oldboy@'localhost';
create table `test` (
`id` int(4) not null, `
name` varchar(20) not null)
ENGINE=InnoDB
DEFAULT CHARSET=gbk;
</pre><span style="font-family:'Microsoft YaHei',Verdana,Helvetica,Arial,sans-serif; font-size:14px; line-height:25px; text-indent:28px; background-color:rgb(234,239,240)"> 删除表中的所有数据,并查看。</span><span style="background-color:rgb(234,239,240)">mysql> TRUNCATE oldboy.test</span><pre>
在把 id 列设置为主键
mysql>alter table test change id id int primary key auto_increment;
创建索引:
alter table test add index 索引名(索引字段);或者create index 索引名 on 表名(字段);
eg:在手机字段上对前 8 个字符创建普通索引
create index shouji on test(shouji(8));或者alter table test add index shouji(shouji(8));
创建联合索引
联合索引:Name 列的前 6 个字符以及手机列的前 8 个字符组建联合索引alter table test add index index_name_shouji(name(6),shouji(8));或者create index index_name_shouji on test(name(6),shouji(8));
删除索引:
eg:删除以上建立的索引
alter table test drop index shouji;
通过以下一些命令可以查看相关索引信息
通过命令desc 表名可以大概得知存在哪些索引:
通过show index from 表名;可以得知创建的具体索引名,以及有没有进行截取字段索引
通过show index from 表名\G;可以得知跟上述一样的情况,更直观!
有一道不错的题目跟大家分享以下!
大家如果有其它解法也可以评论下,分享下!
create table t
(A date,
B varchar2(2));
insert into t values(str_to_date('05/05/2005','%m/%d/%Y'),'W');//str_to_date()是mysql特有的,不同数据库转换日期函数是不同的!
insert into t values(str_to_date('05/05/2005','%m/%d/%Y'),'W');
insert into t values(str_to_date('05/05/2005','%m/%d/%Y'),'f');
insert into t values(str_to_date('05/05/2005','%m/%d/%Y'),'f');
insert into t values(str_to_date('05/10/2005','%m/%d/%Y'),'W');
insert into t values(str_to_date('05/10/2005','%m/%d/%Y'),'f');
insert into t values(str_to_date('05/10/2005','%m/%d/%Y'),'f');
查询结果如下:
</pre><img src="https://img-blog.csdn.net/20150920221439301" alt="" />如何实现编程如下结果:<pre>
</pre><img src="https://img-blog.csdn.net/20150920221509127" alt="" /><pre>
方法:
select
a,
sum(case when b='W' then 1 else 0 end) NumofW,
sum(case when b='f' then 1 else 0 end) Numoff
from t
group by a
order by a
欢迎提出其它更好的方法!
顺便普及一下:
</pre><pre>
格式 | 描述 |
---|---|
%a | 缩写星期名 |
%b | 缩写月名 |
%c | 月,数值 |
%D | 带有英文前缀的月中的天 |
%d | 月的天,数值(00-31) |
%e | 月的天,数值(0-31) |
%f | 微秒 |
%H | 小时 (00-23) |
%h | 小时 (01-12) |
%I | 小时 (01-12) |
%i | 分钟,数值(00-59) |
%j | 年的天 (001-366) |
%k | 小时 (0-23) |
%l | 小时 (1-12) |
%M | 月名 |
%m | 月,数值(00-12) |
%p | AM 或 PM |
%r | 时间,12-小时(hh:mm:ss AM 或 PM) |
%S | 秒(00-59) |
%s | 秒(00-59) |
%T | 时间, 24-小时 (hh:mm:ss) |
%U | 周 (00-53) 星期日是一周的第一天 |
%u | 周 (00-53) 星期一是一周的第一天 |
%V | 周 (01-53) 星期日是一周的第一天,与 %X 使用 |
%v | 周 (01-53) 星期一是一周的第一天,与 %x 使用 |
%W | 星期名 |
%w | 周的天 (0=星期日, 6=星期六) |
%X | 年,其中的星期日是周的第一天,4 位,与 %V 使用 |
%x | 年,其中的星期一是周的第一天,4 位,与 %v 使用 |
%Y | 年,4 位 |
%y | 年,2 位 |
日期类型:
DateTime YYYY-MM-DD HH:MM:SS 从1000-01-01 00:00:00 到9999-12-31 23:59:49
date YYYY-MM-DD 从1000-12-31 至 9999-12-31
timestamp YYYYMMDDHHMMSS
time HH:MM:SS
year YYYY
下面的脚本使用 DATE_FORMAT() 函数来显示不同的格式。我们使用 NOW() 来获得当前的日期/时间:
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p') DATE_FORMAT(NOW(),'%m-%d-%Y') DATE_FORMAT(NOW(),'%d %b %y') DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
结果类似:
Dec 29 2008 11:45 PM 12-29-2008 29 Dec 08 29 Dec 2008 16:25:46.635 str_to_date()的格式同 DATE_FORMAT一样。 如: select str_to_date('09/01/2009','%m/%d/%Y') select str_to_date('20140422154706','%Y%m%d%H%i%s') select str_to_date('2014-04-22 15:47:06','%Y-%m-%d %H:%i:%s')
mysql 字符集设置查看
1列出MYSQL支持的所有字符集:
2.当前MYSQL服务器字符集设置
3.当前MYSQL服务器字符集校验设置
4.显示某数据库字符集设置
5.显示某数据表字符集设置
6.修改数据库字符集
alter database 数据库名default character set 'utf8';
7.修改数据表字符集
alter table 表名default character set 'utf8';
8.建库时指定字符集
create database 数据库名character set gbk collate gbk_chinese_ci;
9.建表时指定字符集
CREATE TABLE `mysqlcode` (
`id` TINYINT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`content` VARCHAR( 255 ) NOT NULL
) engine = MYISAM CHARACTER SET gbk COLLATE gbk_chinese_ci;
如果不加以上代码,那么即便MYSQL编译安装时指定的编码是UTF8,那么在建库时其默认编码仍是LATIN1,而由于字符集的继承性,库中的表也是LATIN1的了。