日期:2014-05-16  浏览次数:20675 次

MYSQL完全手册学习笔记(第四章)
MYSQL完全手册学习笔记


SQL基础

User 库名——使用某个库

Show 表名——显示表的各个字段

Select count (*) from callslog  ——显示一共有多少条记录


mysql> create database toys;——建立个数据库
Query OK, 1 row affected (0.02 sec)

mysql> use toys;
Database changed
mysql> show tables;
Empty set (0.01 sec)

mysql> create table user(id int(11) not null auto_increment,primary key(id));
建立个表

mysql> create table status(id int(11) not null,video_id tinyint(11) not null);
Query OK, 0 rows affected (0.08 sec)

mysql> alter table status add (hyht varchar(255));
Query OK, 0 rows affected (0.28 sec)
Records: 0  Duplicates: 0  Warnings: 0

增加字段

mysql> alter table status rename haha;
Query OK, 0 rows affected (0.03 sec)


Alter的用法
mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));
重命名表,从t1到t2:
mysql> ALTER TABLE t1 RENAME t2;
为了改变列a,从INTEGER改为TINYINT NOT NULL(名字一样),并且改变列b,从CHAR(10)改为CHAR(20),同时重命名它,从b改为c:
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
增加一个新TIMESTAMP列,名为d:
mysql> ALTER TABLE t2 ADD d TIMESTAMP;
在列d上增加一个索引,并且使列a为主键:
mysql> ALTER TABLE t2 ADD INDEX (d), ADD PRIMARY KEY (a);
删除列c:
mysql> ALTER TABLE t2 DROP COLUMN c;
增加一个新的AUTO_INCREMENT整数列,命名为c:
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
           ADD INDEX (c);
注意,我们索引了c,因为AUTO_INCREMENT柱必须被索引,并且另外我们声明c为NOT NULL,因为索引了的列不能是NULL。 当你增加一个AUTO_INCREMENT列时,自动地用顺序数字填入列值。


mysql> insert into t2 (a,c)values(1,'hghg');
Query OK, 1 row affected (0.03 sec)
添加记录

mysql> alter table t2 drop column c;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

删除列


mysql> alter table t2 add (c varchar(255));
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0
增加列

注意:一个表中必须有主键才能在mysql插件中直接编辑,否则edit那项为灰色


mysql> delete from haha where video_id=2;
Query OK, 1 row affected (0.03 sec)
有条件的删除数据


mysql> update haha set hyht='tt'where hyht='1';
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0
更新数据


mysql> select * from haha;
+----+----------+------+
| id | video_id | hyht |
+----+----------+------+
|  1 |        3 | tt   |
|  2 |        3 | erer |
|  3 |        3 | tt   |
|  4 |        1 | tt   |
|  5 |        2 | tt   |
+----+----------+------+
5 rows in set (0.01 sec)

mysql> select hyht from haha
+------+
| hyht |
+------+
| tt   |
| erer |
| tt   |
| tt   |
| tt   |
+------+
5 rows in set (0.00 sec)

mysql> select distinct hyht
+------+
| hyht |
+------+
| tt   |
| erer |
+------+
2 rows in set (0.01 sec)

三个select 查询语句



mysql> select * from haha where video_id>2;
+----+----------+------+
| id | video_id | hyht |
+----+----------+------+
|  1 |        3 | tt   |
|  2 |        3 | erer |
|  3 |        3 | tt   |
+----+----------+------+
3 rows in set (0.00 sec)


my