mysql-sql语句

[root@localhost bin]# ./mysqld_safe �Cuser=mysql &
[root@localhost bin]# nmap localhost
[root@localhost bin]# mysql -u root -p
mysql> show databases;
+――――――�C+
| Database |
+――――――�C+
| information_schema |
| discuz |
| mysql |
| phpbb |
| test |
+――――――�C+
5 rows in set (0.04 sec)

mysql> create database db2;
Query OK, 1 row affected (0.00 sec)

mysql> use db2;
Database changed

mysql> select database(); ---查看现在使用的是哪个数据库。
+――――+
| database() |
+――――+
| db2 |
+――――+
1 row in set (0.00 sec)

mysql> create table members (id int not null auto_increment primary key,name char(10) not null,tel char(15),qq int);
Query OK, 0 rows affected (0.01 sec)

mysql> desc members; ---查看表
+――-+―――-+――+―�C+―――+―――――-+
| Field | Type | Null | Key | Default| Extra |
+――-+―――-+――+―�C+―――+―――――-+
| id | int(11) | NO | PRI | NULL |auto_increment|
| name |char(10)| NO | | NULL | |
| tel | char(15) | YES | | NULL | |
| qq | int(11) | YES | | NULL | |
+――-+―――-+――+―�C+―――+―――――-+
4 rows in set (0.00 sec)

mysql> alter table members add addr char(20); ---添加一个字段addr
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table members drop qq; ---删除一个字段qq
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table members modify addr char(30); ---修改数据类型或修饰符
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table members change addr address char(30); 修改字段的名字
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc members;
+―――+―――-+――+―�C+―――+―――――-+
| Field | Type | Null | Key | Default | Extra |
+―――+―――-+――+―�C+―――+―――――-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| tel | char(15) | YES | | NULL | |
| address | char(30) | YES | | NULL | |
+―――+―――-+――+―�C+―――+―――――-+
4 rows in set (0.00 sec)

mysql> alter table members add qq int after name; ---在name后面+一个qq字段
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into members (id ,name,qq,tel,address) values (1,’mike’,12345,876543,’beijing’); ---添加表内容
Query OK, 1 row affected (0.00 sec)

mysql> select * from members; ---查看表里所有内容
+―-+――+――-+――�C+―――+
| id | name | qq | tel | address |
+―-+――+――-+――�C+―――+
| 1 | mike | 12345 | 876543 | beijing |
+―-+――+――-+――�C+―――+
1 row in set (0.00 sec)
mysql> select name,address from members;
+――+―――+
| name | address |
+――+―――+
| mike | beijing |
+――+―――+
1 row in set (0.00 sec)

mysql> insert into members (address,tel,qq,name) values(‘shanghai’,654654,654321,’colin’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from members;
+―-+――-+――�C+――�C+―――-+
| id | name | qq | tel | address |
+―-+――-+――�C+――�C+―――-+
| 1 | mike | 12345 | 876543 | beijing |
| 2 | colin | 654321 | 654654 | shanghai |
+―-+――-+――�C+――�C+―――-+
2 rows in set (0.00 sec)

mysql> insert into members set name=’zhang’,qq=98765432,tel=’010-12345′,address=’shenyang’; ---另一种添加记录
Query OK, 1 row affected (0.00 sec)

mysql> insert into members set name=’zhang’,qq=98765432,tel=’010-12345′,address=’shenyang’;
Query OK, 1 row affected (0.00 sec)

mysql> select * from members;
+―-+――-+―――-+―――�C+―――-+
| id | name | qq | tel | address |
+―-+――-+―――-+―――�C+―――-+
| 1 | mike | 12345 | 876543 | beijing |
| 2 | colin | 654321 | 654654 | shanghai |
| 3 | zhang | 98765432 | 010-12345 | shenyang |
+―-+――-+―――-+―――�C+―――-+
3 rows in set (0.00 sec)

mysql> insert into members values (4,’divel’,253210,88585888,’shenzhen’); ---不指定字段.但数据要和名字一一对应
Query OK, 1 row affected (0.00 sec)

mysql> select * from members;
+―-+――-+―――-+―――�C+―――-+
| id | name | qq | tel | address |
+―-+――-+―――-+―――�C+―――-+
| 1 | mike | 12345 | 876543 | beijing |
| 2 | colin | 654321 | 654654 | shanghai |
| 3 | zhang | 98765432 | 010-12345 | shenyang |
| 4 | divel | 253210 | 88585888 | shenzhen |
+―-+――-+―――-+―――�C+―――-+
4 rows in set (0.00 sec)

mysql> delete from members where id=4; ―删除记录
Query OK, 1 row affected (0.02 sec)

mysql> select * from members;
+―-+――-+―――-+―――�C+―――-+
| id | name | qq | tel | address |
+―-+――-+―――-+―――�C+―――-+
| 1 | mike | 12345 | 876543 | beijing |
| 2 | colin | 654321 | 654654 | shanghai |
| 3 | zhang | 98765432 | 010-12345 | shenyang |
+―-+――-+―――-+―――�C+―――-+
3 rows in set (0.00 sec)

mysql> drop table members; ---删除表
Query OK, 0 rows affected (0.00 sec)

mysql> show tables; ---查看表
Empty set (0.00 sec)

===================================================
简单的算术运算:
mysql> select 8+5;
+―�C+
| 8+5 |
+―�C+
| 13 |
+―�C+
1 row in set (0.02 sec)

mysql> select 2+5,4-7,4*2,5/3;
+―�C+―�C+―�C+――�C+
| 2+5 | 4-7 | 4*2 | 5/3 |
+―�C+―�C+―�C+――�C+
| 7 | -3 | 8 | 1.6667 |
+―�C+―�C+―�C+――�C+
1 row in set (0.00 sec)
mysql> select 2+’a2′;
+――�C+
| 2+’a2′ |
+――�C+
| 2 |
+――�C+
1 row in set, 1 warning (0.00 sec)
.
比较运算 > = >= < <= <> (与shell相反,如果正确返回1,错误返回0)
mysql> select 5 > 3; 返回1,正确
mysql> select 5 > 3,4 =’4a’; 返回1
mysql> select ’4′=’4a’; 返回0
mysql> select ‘a’='A’; 返回1,正确,默认是不区分大小写的
mysql> select binary ‘a’='A’; 返回0,不正确,想区分大小写,可以加上binary,这样就区分大小写
------------------------------------------------------------------
创建一个表
mysql> create table grade(id int not null auto_increment primary key,name char(10) not null,math int,physics int,computer int);
mysql> insert into grade values(1,’mike’,98,87,70),(2,’john’,70,67,50),(5,’tom’,67,57,80);添加3条记录
mysql> desc grade;
mysql> select * from grade; 查看添加的记录(表的内容)
mysql> select * from grade where name=’mike’; 查询名字为mike的记录,默认不区分大小写
mysql> select * from grade where binary name=’MIKE’; 严格查询大写MIKE的记录
Empty set (0.00 sec) 搜索不到,空的
mysql> select * from grade where math > 80; 查询数学成绩大于80的记录
mysql> select name,math+physics+computer from grade;只列出人名和总成绩
mysql> select name,(math+physics+computer)/3 from grade; 查询平均成绩
mysql> select name,(math+physics+computer)/3 from grade where (math+physics+computer)/3>80;查询平均成绩大于80的成绩
逻辑 and 两边条件都满足才成立 or 只有一个条件满足就成立
mysql> select * from grade where math>=60 and physics>=60 and computer >=60; 逻辑与,所有大于60分的成绩
mysql> select * from grade where math<60 or physics <60 or computer<60; 逻辑或 有一课小于60分的成绩
函数 函数只能放在select的后面
mysql> select count(*) from grade;
mysql> select avg(math) from grade; 查看数学成绩的平均成绩
mysql> select avg(math),avg(computer),avg(physics)from grade; 查看数学,计算机,物理的平均成绩
mysql> select sum(math) from grade; 查看数学成绩的总成绩
mysql> select max(computer) from grade; 求计算机科目中最大值
mysql> select min(computer) from grade; 求计算机科目中最小值
------------------------
mysql> select * from grade order by math; 排序(order by)根据math排序,默认是升序排序
mysql> select * from grade order by math desc; (desc 表示倒叙排序)
mysql> select * from grade order by name; name字段也可以排序,根据第一个字母
mysql> select * from grade order by math desc limit 2; 按数学成绩倒叙排序,选择前两名
mysql> select * from grade order by math desc limit 2,2; 第一个2表示从第3条记录开始显示,0表示第一条,第二个2表示显示几条记录
mysql> select name,math+physics+computer from grade order by math+physics+computer desc limit 1;查看总成绩最高的一条记录
----------
修改记录 update(更新)
mysql> update grade set math=88 where id=1; 更新grade表的math=88,当id=1的时候
mysql> update grade set math=88 where name=’john’ and id=2; 可以用and加入逻辑与添加多个条件
-----
mysql> select now(); 显示当前的时间
mysql> select user(); 显示当前由那个用户登录的 此时root是数据库的管理员,不是系统的管理员
[root@ppwn ~]# mysql -u root -h localhost -p -h后面可以跟一个ip,默认情况下只能登录本机,如果远程必须要指定一下
mysql> select * from zb.grade; 查看表必须要指定哪个数据库,可以用.这种方式

=====================================================================

数值型:
整数: 类型 大小 范围(有符号) 范围(无符号) 用途
tinyint 1字节 -128~127 0~255 小整数值
smallint 2字节 -32768~32767

如:
mysql> create table data (fti tinyint,fsi smallint,fmi mediumint,fi int,fbi bigint);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into data values(12345678,12345678,123456789,123456789,12345678987654321);
Query OK, 1 row affected, 3 warnings (0.00 sec)

mysql> select * from data;
+――+――-+―――+―――�C+――――――-+
| fti | fsi | fmi | fi | fbi |
+――+――-+―――+―――�C+――――――-+
| 127 | 32767 | 8388607 | 123456789 | 12345678987654321 |
+――+――-+―――+―――�C+――――――-+
1 row in set (0.00 sec)

INT类型:
UNSIGNED修饰符规定字段只保存正的值,它可以增大这个字段支持的值的范围。
ZEROFILL修饰符规定0(不是空格)可以用于填补输出值。
---------------------------------------------
小数:FLOAT,DOUBLE,DECIMAL
mysql> create table data2 (test float(5,2));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into data2 values(123456.456);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from data2;
+――�C+
| test |
+――�C+
| 999.99 |
+――�C+
1 row in set (0.00 sec)
mysql> insert into data2 values(456.456);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data2;
+――�C+
| test |
+――�C+
| 999.99 |
| 456.46 |
+――�C+
2 rows in set (0.00 sec)
mysql> insert into data2 values(‘make’);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from data2;
+――�C+
| test |
+――�C+
| 999.99 |
| 456.46 |
| 0.00 |
+――�C+
3 rows in set (0.00 sec)
------------------------------------------
字符串类型:
CHAR和VARCHAR类型
CHAR定长
VARCHAR可变的-根据实际长度来分配内存

mysql> create table data3 (id int,name char(7));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data3 values(1,’abcd’),(2,’abcdefghijklmn’);
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1

mysql> select * from data3;
+――+―――+
| id | name |
+――+―――+
| 1 | abcd |
| 2 | abcdefg |
+――+―――+
2 rows in set (0.00 sec)

CHAR类型认可一个可选择的BUNARY修饰符,BUNARY区分大小写。

mysql> create table data4 (name varchar(10) binary);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data4 values(‘mike’),(‘Tom’);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from data4 where name=’tom’;
Empty set (0.00 sec)

mysql> select * from data4 where name=’Tom’;
+――+
| name |
+――+
| Tom |
+――+
1 row in set (0.00 sec)
--------------------------------------------
日期和时间类型:

date类型:
mysql> create table data5 (birthday date);
Query OK, 0 rows affected (0.00 sec)

mysql> desc data5;
+―――-+――+――+―�C+―――+――-+
| Field | Type | Null | Key | Default | Extra |
+―――-+――+――+―�C+―――+――-+
| birthday | date | YES | | NULL | |
+―――-+――+――+―�C+―――+――-+
1 row in set (0.00 sec)

mysql> insert into data5 values(’2009-09-02′);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data5;
+――――+
| birthday |
+――――+
| 2009-09-02 |
+――――+
1 row in set (0.00 sec)

mysql> insert into data5 values(20080909);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data5;
+――――+
| birthday |
+――――+
| 2009-09-02 |
| 2008-09-09 |
+――――+
2 rows in set (0.00 sec)

mysql> insert into data5 values(’96-08-08′); ---年用2位数来表示 0~69表示2000~2069 70~99表示1970~1999
Query OK, 1 row affected (0.00 sec)

mysql> select * from data5;
+――――+
| birthday |
+――――+
| 2009-09-02 |
| 2008-09-09 |
| 0000-00-00 |
| 1996-08-08 |
+――――+
4 rows in set (0.00 sec)

time类型:
mysql> create table data6 (showtime time);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into data6 values(’14:27:01′);
Query OK, 1 row affected (0.00 sec)

mysql> insert into data6 values(201010);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data6;
+―――-+
| showtime |
+―――-+
| 14:27:01 |
| 20:10:10 |
+―――-+
2 rows in set (0.00 sec)

year类型:

mysql> create table data7(Y year);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data7 values(2009),(85),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from data7;
+――+
| Y |
+――+
| 2009 |
| 1985 |
| 2003 |
+――+
3 rows in set (0.00 sec)

year() 函数
mysql> select * from data5;
+――――+
| birthday |
+――――+
| 2009-09-02 |
| 2008-09-09 |
| 0000-00-00 |
| 1996-08-08 |
| 2000-03-12 |
| 2000-11-12 |
+――――+
6 rows in set (0.00 sec)

mysql> select year(birthday) from data5;
+―――――-+
| year(birthday) |
+―――――-+
| 2009 |
| 2008 |
| 0 |
| 1996 |
| 2000 |
| 2000 |
+―――――-+
6 rows in set (0.00 sec)

mysql> select * from data5 where year(birthday) > 2000; ---与where结合
+――――+
| birthday |
+――――+
| 2009-09-02 |
| 2008-09-09 |
+――――+
2 rows in set (0.00 sec)

datetime类型

mysql> create table data8 (dt datetime);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data8 values(’2008-01-01 12:12:09′),(20090110121010);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from data8;
+―――――――+
| dt |
+―――――――+
| 2008-01-01 12:12:09 |
| 2009-01-10 12:10:10 |
+―――――――+
2 rows in set (0.00 sec)

mysql> insert into data8 values(now()); ---插入当前日期和时间
Query OK, 1 row affected (0.00 sec)

mysql> select * from data8;
+―――――――+
| dt |
+―――――――+
| 2008-01-01 12:12:09 |
| 2009-01-10 12:10:10 |
| 2009-09-02 14:36:19 |
+―――――――+
3 rows in set (0.00 sec)

timestamp类型: 时间绰
mysql> create table data9 (ts timestamp);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data9 values();
Query OK, 1 row affected (0.00 sec)

mysql> select * from data9;
+―――――――+
| ts |
+―――――――+
| 2009-09-02 14:39:51 |
+―――――――+
1 row in set (0.00 sec)

mysql> insert into data9 values(null);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data9;
+―――――――+
| ts |
+―――――――+
| 2009-09-02 14:39:51 |
| 2009-09-02 14:40:07 |
+―――――――+
2 rows in set (0.00 sec)

-----------------------------------------------------
复合类型:
MYSQL有2个复合类型:ENUM(枚举)类型和SET类型。
他们的值必须从一个预先定义好的字符串集合中选取。一个ENUM类型允许从一个值集合中选择单个值;SET类型允许从可选集合中选择多个值。

enum类型:只能选择一个值
mysql> create table data10 (gender enum(‘f’,'m’));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into data10 values(‘f’),(‘M’),(‘a’),(‘m’);
Query OK, 4 rows affected, 1 warning (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 1

mysql> select * from data10;
+――�C+
| gender |
+――�C+
| f |
| m |
| |
| m |
+――�C+
4 rows in set (0.00 sec)

mysql> select * from data10 where gender=2;
+――�C+
| gender |
+――�C+
| m |
| m |
+――�C+
2 rows in set (0.00 sec)

mysql> select * from data10 where gender=’m';
+――�C+
| gender |
+――�C+
| m |
| m |
+――�C+
2 rows in set (0.00 sec)

mysql> select * from data10 where gender=0; ---显示错误记录
+――�C+
| gender |
+――�C+
| |
+――�C+
1 row in set (0.00 sec)

set类型:能选择多个值

mysql> create table data11 (test set(‘a’,'b’,'abc’,'d’,'e’));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into data11 values(‘a’),(‘b’),(‘abc’),(‘f’);
Query OK, 4 rows affected, 1 warning (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 1

mysql> select * from data11;
+――+
| test |
+――+
| a |
| b |
| abc |
| |
+――+
4 rows in set (0.00 sec)

==============================================================
字段约束
DEFAULT修饰符
可以使用DEFAULT修饰符为字段指定一个默认值。
创建了连接雇员和部门的表。默认情况下,雇员分配在MIS部门:

mysql> create table data12 (user char(10),dep char(5) default ‘MIS’);
Query OK, 0 rows affected (0.00 sec)

mysql> desc data12;
+――-+―――-+――+―�C+―――+――-+
| Field | Type | Null | Key | Default | Extra |
+――-+―――-+――+―�C+―――+――-+
| user | char(10) | YES | | NULL | |
| dep | char(5) | YES | | MIS | |
+――-+―――-+――+―�C+―――+――-+
2 rows in set (0.00 sec)
mysql> insert into data12 (user) values (‘mike’); ---只定义user,dep会是DEFAULT值MIS.
Query OK, 1 row affected (0.00 sec)

mysql> select * from data12;
+――+――+
| user | dep |
+――+――+
| mike | MIS |
+――+――+
1 row in set (0.00 sec)

mysql> insert into data12 (user,dep) values (‘mike’,'sal’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from data12;
+――+――+
| user | dep |
+――+――+
| mike | MIS |
| mike | sal |
+――+――+
2 rows in set (0.00 sec)

mysql> insert into data12 values(); ---都不指定的情况
Query OK, 1 row affected (0.00 sec)

mysql> select * from data12;
+――+――+
| user | dep |
+――+――+
| mike | MIS |
| mike | sal |
| NULL | MIS |
+――+――+
3 rows in set (0.00 sec)

auto_increment修饰符
只适用于INT(整形)字段,表明MYSQL应该自动为该字段生成一个数(每此在前一个值的基础上加1)。
对于主键(非空,不重复),这个非常有用的。

unique修饰符:
mysql> create table data15 (name char(10) unique);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into data15 values(‘mike’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into data15 values(‘mike’); ---唯一的
ERROR 1062 (23000): Duplicate entry ‘mike’ for key ‘name’
mysql> insert into data15 values();
Query OK, 1 row affected (0.00 sec)

mysql> insert into data15 values(); ---可以添加多个空字符
Query OK, 1 row affected (0.00 sec)

mysql> select * from data15;
+――+
| name |
+――+
| NULL |
| NULL |
| mike |
+――+
3 rows in set (0.00 sec)


你可能感兴趣的:(数据库,discuz,create,databases,phpbb)