mysql轻快入门(3)

2023-01-31 02:01:23 mysql 入门 轻快

有一种情况,你忘记数据库密码啦,你怎么办,砸电脑吗?no.....
请见下:
select host,user,passWord from Mysql.user;
update mysql.user set password=password('123') where name='root';

grant select,insert,update,delete on cissst.* to guest@localhost identified by '123';//这个一般在发布的时候创建一个低级别的账号供用户使用,即创建一个
//guest本地用户 密码123,对数据库cissit下的表有select,update,delete权限

(1)windows
强行重置mysql root密码:
step1:net stop mysql
step2:mysqld --skip-grant-tables 启动mysql服务,但不加载权限检查
step3:再开个窗口输入mysql回车进入mysql 界面
step4:update mysql.user set password=password('mysql') where name='root';
step5:\q
step6:update mysql.user set password=password('123') where name='root';
C:\Documents and Settings\Administrator>netstat -nao |find "3306"
tcp 0.0.0.0:3306 0.0.0.0:0 LISTENING 328
TCP 127.0.0.1:1059 127.0.0.1:3306 TIME_WaiT 0

step7:taskkill -f -pid 328
step8:net start mysql
step9:mysql -uroot -pmysql 完成

(2)linux下差不多
step1:pkill mysql&& pkill mysqld
step2:mysqld --skip-grant-tables 启动mysql服务,但不加载权限检查
step3:再开个窗口输入mysql回车进入mysql 界面
step4:update mysql.user set password=password('mysql') where name='root';
step5:\q
step6:update mysql.user set password=password('123') where name='root';
step 7 :ps aux|grep mysql && pkill mysqld
然后启动mysql即可

备份:
C:\>mysqldump -uroot -pmysql cissst >c:\cissst.sql

还原:
创建一个待还原的新数据库:
sql>create database cissst;
\q
c:>mysql -uroot -pmysql cissst <c:\cissst.sql
windows和liuux差不多

(char)举例

mysql> create table t5(f1 char(4),f2 varchar(4));
Query OK, 0 rows affected (0.16 sec)

mysql> desc t5;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| f1 | char(4) | YES | | NULL | |
| f2 | varchar(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t5 values('hi ','hi ');
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select length(f1),length(f2) from t5;
+------------+------------+
| length(f1) | length(f2) |
+------------+------------+
| 2 | 4 |
+------------+------------+
1 row in set (0.03 sec)

mysql> select concat(f1,'+'),concat(f1,'+') from t5;
+----------------+----------------+
| concat(f1,'+') | concat(f1,'+') |
+----------------+----------------+
| hi+ | hi+ |
+----------------+----------------+
1 row in set (0.00 sec)

mysql> select concat(f1,'+'),concat(f2,'+') from t5;
+----------------+----------------+
| concat(f1,'+') | concat(f2,'+') |
+----------------+----------------+
| hi+ | hi + |
+----------------+----------------+
1 row in set (0.00 sec)
总结:从上面可以看出char与varchar的区别
1.(char)一个定长,不够用空格填充,取出来会去掉右边的空格,因此如果后边本身有的空格便会没有了,但varchar不会,varchar空间利用率更高一些,但并非100%,他还会有一些指示字符串长度的一些东西,但定长速度快

(int)
mysql>create table t1(f1 int,f2 int(3));
Query OK, 0 rows affected (0.22 sec)

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| f1 | int(11) | YES | | NULL | |
| f2 | int(3) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> insert into t1 values(1002100010,1234);
Query OK, 1 row affected (0.03 sec)

mysql> select *from t1;
+------------+------+
| f1 | f2 |
+------------+------+
| 1002100010 | 1234 |
+------------+------+
1 row in set (0.03 sec)

mysql> create table t2(f1 int unsigned zerofill);
Query OK, 0 rows affected (0.08 sec)

mysql> desc t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| f1 | int(10) unsigned zerofill | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t2 values(12);
Query OK, 1 row affected (0.03 sec)

mysql> select *from t2;
+------------+
| f1 |
+------------+
| 0000000012 |
+------------+
1 row in set (0.00 sec)

mysql> insert into t2 values(-12);
ERROR 1264 (22003): Out of range value adjusted for column 'f1' at row 1
mysql> create table t3(id int not null auto_increment primary key);
Query OK, 0 rows affected (0.11 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.02 sec)

mysql> create table t4(id int zerofill);
Query OK, 0 rows affected (0.09 sec)

mysql> desc t4;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(10) unsigned zerofill | YES | | NULL | |
+-------+---------------------------+----
总结:(1)unsigend无符号
(2)int(M) zerofill 只有zerofill写了M才有意义,zerofill默认unsigned

(date)

create table t6(f1 date,f2 datetime,f3 timestamp);

Query OK, 0 rows affected (0.08 sec)

mysql> desc t6;
+-------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-------+
| f1 | date | YES | | NULL | |
| f2 | datetime | YES | | NULL | |
| f3 | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------+-----------+------+-----+-------------------+-------+
3 rows in set (0.02 sec)

mysql> insert into t6(f1,f2) values('1983-01-02','1986-03-01 12:10:11');
Query OK, 1 row affected (0.05 sec)

mysql> select *from t6;
+------------+---------------------+---------------------+
| f1 | f2 | f3 |
+------------+---------------------+---------------------+
| 1983-01-02 | 1986-03-01 12:10:11 | 2014-11-16 09:42:48 |
+------------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> insert into t6 values(now(),now(),null);
Query OK, 1 row affected, 1 warning (0.06 sec)

mysql> select *from t6;
+------------+---------------------+---------------------+
| f1 | f2 | f3 |
+------------+---------------------+---------------------+
| 1983-01-02 | 1986-03-01 12:10:11 | 2014-11-16 09:42:48 |
| 2014-11-16 | 2014-11-16 09:44:11 | 2014-11-16 09:44:11 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| time_zone | SYSTEM |
+---------------+--------+
1 row in set (0.00 sec)

mysql> set time_zone='+9:00';
Query OK, 0 rows affected (0.02 sec)

mysql> select *from t6;
+------------+---------------------+---------------------+
| f1 | f2 | f3 |
+------------+---------------------+---------------------+
| 1983-01-02 | 1986-03-01 12:10:11 | 2014-11-16 10:42:48 |
| 2014-11-16 | 2014-11-16 09:44:11 | 2014-11-16 10:44:11 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> create table t7(sex enum('M','F') default 'M');
Query OK, 0 rows affected (0.08 sec)

mysql> insert into t7 values('m'),(null),(1);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t7;
+------+
| sex |
+------+
| M |
| NULL |
| M |
+------+
3 rows in set (0.00 sec)

mysql> select 9>7;
+-----+
| 9>7 |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
总结:
timestamp 不需要手动填值,它自动获取当前时间,并且时区改变,也会影响它的值,如上,查看时区可以用show variables like 'time_zone';
设置时区用set time_zone='+9:00

 编码

 mysql> \s

mysql Ver 14.12 Distrib 5.0.83, for Win32 (ia32)

Connection id: 1
Current database:
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.0.83-commUnity-nt MySQL Community Edition (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 9 sec

Threads: 1 Questions: 4 Slow queries: 0 Opens: 12 Flush tables: 1 Open tabl
es: 6 Queries per second avg: 0.444
修改编码及校对集
alter database demo character set gbk;
alter table t5 character set gbk;
alter tablet t5 modify f1 char(4) character set gbk;
alter table t1 modify f1 varchar(20) collate=gbk_bin;
可以用下面的命令查看支持的校对集
slect COLLATION_NAME,CHARACTER_SET_NAME where CHARACTER_SET_NAME like '%gbk%';

相关文章