DATETIME类型就是按字符串的方式存储的,而TIMESTAMP是按整型存储的
使用DATETIME则只占8个字节....TIMESTAMP是使用INT的,为此只占4个字节
UNIX时间戳转换为日期用函数: FROM_UNIXTIME()
> select FROM_UNIXTIME(1156219870); > select FROM_UNIXTIME(1423017157,'%Y%m'); #201502 > select FROM_UNIXTIME(create_time,'%Y%m') from user_operation;
日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()
Select UNIX_TIMESTAMP('2006-11-04 12:23:00');
#修改root 密码: mysqladmin -u root -poldpassword password 'newpassword'; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); #重置密码: 在my.cnf添加一行: skip-grant-tables 重启Mysql后使用mysql -uroot -p 命令登入Mysql并修改密码
1.新建一个用户并给予相应数据库的权限 grant select,insert,update,delete,create,drop privileges on database.* to user@localhost identified by 'passwd'; grant all privileges on database.* to user@localhost identified by 'passwd'; 2.刷新权限 flush privileges; 3. 显示授权 show grants; 4. 移除授权 revoke delete on *.* from 'jack'@'localhost'; 5. 删除用户 drop user 'jack'@'localhost'; 从表中删除用户: delete from user where user='sun'; 查看其他 MySQL 用户权限: show grants for dba@localhost;
show engine innodb status\G show processlist; show global status like '%connections'; show variables like '%innodb%'; #查看innodb相关配置参数 show status like '%innodb%'; #查看innodb相关的运行时参数 status show global status like 'com_select'; show global status like 'com_insert'; #当open_files大于open_files_limit值时,MYSQL数据库就会发生卡住的现象 show global status like 'open_files'; show variables like 'open_files_limit'; mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; show table status like "user"\G #查看user表信息
#启动的时候 shell> mysqld --character-set-server=latin1 \--collation-server=latin1_swedish_ci
#新建库 CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;
Example:
CREATE TABLE t1 ( ... ) CHARACTER SET latin1 COLLATE latin1_danish_ci; CREATE TABLE t1 ( col1 VARCHAR(5) CHARACTER SET latin1 COLLATE latin1_german1_ci ); ALTER TABLE t1 MODIFY col1 VARCHAR(5) CHARACTER SET latin1 COLLATE latin1_swedish_ci;
To change the character set (and collation) for all columns in an existing table, use...
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name [COLLATE collation_name];
创建表:
CREATE TABLE students(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) UNIQUE KEY NOT NULL,Age TINYINT UNSIGNED INDEX,Gender CHAR(1) NOT NULL) [ENGINE={MyISAM | InnoDB }];
也可以这样写(区别在于单独定义主键,唯一键和索引):
CREATE TABLE students(Id INT NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) NOT NULL,Age TINYINT UNSIGNED,Gender CHAR(1) NOT NULL,PRIMARY KEY(id),UNIQUE KEY(name),INDEX(age))
快速创建一张大表:
create table tmp(id int(3) unsigned,name varchar(32))engine=myisam DEFAULT CHARSET=UTF8; insert into tmp values(1,'aaa'); insert into tmp values(2,'bbb'); insert into tmp values(3,'ccc'); insert into tmp select id,name from tmp;
查询出一张表的数据后创建新表(字段定义会丢失,数据会保留):
CREATE TABLE test SELECT * FROM students WHERE Id>5;
复制表结构+复制表数据:
mysql> create table t3 like t1; mysql> insert into t3 select * from t1;
修改表:
ALTER TABLE tb_name MODIFY #修改字段定义 CHANGE #可以修改字段名和字段定义 ADD DROP
给表添加字段:
ALTER TABLE students ADD (course VARCHAR(100),teacher CHAR(20));
添加惟一键:
ALTER TABLE students ADD UNIQUE KEY Name;
修改字段:
修改course字段为Course字段,并放在Name字段之后(修改字段需要带上新的字段的定义)ps:MODIFY只能修改字段定义 ALTER TABLE students CHANGE course Course VARCHAR(100) [AFTER Name]; alter table user MODIFY new1 VARCHAR(10); //修改一个字段的类型
修改数据:
update shared_ftp(表) set ip(字段)='198.55.120.151' where ip='204.74.216.91'; #字段精准全部替换 update people set firstname=REPLACE(firstname,'sun','Sun') #部分替换
删除表中所有记录: truncate table table1; 效率高,不记录日志信息 delete table table1; 删除一个表: drop table table1; delete from 表名 where id=1; #删除一条记录
创建索引
CREATE INDEX index_name ON TABLE (col_name[(length)] [ASC|DESC]) [USING {BTREE|HASH}];
删除索引 DROP INDEX index_name ON TBNAME; 查看表状态: SHOW STATUS LIKE 'TBNAME'; 查看表的索引: SHOW INDEXES FROM TBNAME;
CREATE INDEX 可对表增加普通索引和UNIQUE索引 CREATE INDEX index_name ON table_name(column_list) CRTATE UNIQUE INDEX index_name ON table_name(column_list) table_name为需要增加索引的表名,column_list指出对哪些列进行索引,多列时用逗号分隔
alter table t1 add index (id), add index (name); 创建2个索引 alter table t1 add index index_name (id,name); 创建多列索引 alter table user_operation drop index create_time,add index create_time_uid (u_id,create_time); 同时进行删除和添加
重命名表:
RENAME TABLE old_table TO tmp_table, new_table TO old_table, tmp_table TO new_table;