- 最后登录:
- 2024-11-25 08:18<
- 注册时间:
- 2012-8-31 12:10
- 阅读权限:
- 200
- UID:
- 1
- 在线时间:
- 1644 小时
|
- mysql> select 1<<3;
- +------+
- | 1<<3 |
- +------+
- | 8 |
- +------+
- 1 row in set (0.02 sec)
- mysql> select 1<<10;
- +-------+
- | 1<<10 |
- +-------+
- | 1024 |
- +-------+
- 1 row in set (0.00 sec)
- mysql> select 1<<20;
- +---------+
- | 1<<20 |
- +---------+
- | 1048576 |
- +---------+
- 1 row in set (0.00 sec)
- mysql> select 1|2;
- +-----+
- | 1|2 |
- +-----+
- | 3 |
- +-----+
- 1 row in set (0.00 sec)
- mysql> select 1&2;
- +-----+
- | 1&2 |
- +-----+
- | 0 |
- +-----+
- 1 row in set (0.00 sec)
- mysql> select 5&6;
- +-----+
- | 5&6 |
- +-----+
- | 4 |
- +-----+
- 1 row in set (0.00 sec)
- --创建数据库
- mysql> create database fooknet;
- Query OK, 1 row affected (0.00 sec)
- --查看当前用户可以看到所有数据库
- mysql> show databases;
- +----------------------+
- | Database |
- +----------------------+
- | information_schema |
- | test |
- | mysql |
- | fooknet |
- +----------------------+
- 83 rows in set (0.75 sec)
- --删除数据库
- mysql> drop database fooknet;
- Query OK, 0 rows affected (0.03 sec)
- mysql> create database fooknet;
- Query OK, 1 row affected (0.00 sec)
- mysql>
- -- 选择进入fooknet数据库
- mysql> use fooknet;
- Database changed
- --查看当前你在哪个数据库下
- mysql> select database();
- +------------+
- | database() |
- +------------+
- | fooknet |
- +------------+
- 1 row in set (0.00 sec)
- --查看当前数据库中的所有表格。
- mysql> show tables;
- Empty set (0.02 sec)
- mysql>
- --创建留言表格
- mysql> create table liuyan(
- -> id int unsigned not null auto_increment primary key,
- -> title varchar(32) not null,
- -> author varchar(32) not null default 'wu',
- -> addtime datetime,
- -> content text not null);
- Query OK, 0 rows affected (0.08 sec)
- --id 整型 无负号 非空 自增 主键(非空,唯一性、索引列)
- --title 字串(长度32位) 非空
- --author 字串(长度32位) 非空 默认值 “wu”
- --查看留言表结构
- mysql> desc liuyan;
- +---------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | author | varchar(32) | NO | | wu | |
- | addtime | datetime | YES | | NULL | |
- | content | text | NO | | NULL | |
- +---------+------------------+------+-----+---------+----------------+
- 5 rows in set (0.02 sec)
- --以文本方式输出留言表的建表语句
- mysql> show create table liuyan \G;
- *************************** 1. row ***************************
- Table: liuyan
- Create Table: CREATE TABLE `liuyan` (
- `id` int(10) unsigned NOT NULL auto_increment,
- `title` varchar(32) NOT NULL,
- `author` varchar(32) NOT NULL default 'wu',
- `addtime` datetime default NULL,
- `content` text NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- mysql>
- mysql>
- --查看当前数据库中表格
- mysql> show tables;
- +------------------+
- | Tables_in_fooknet |
- +------------------+
- | liuyan |
- +------------------+
- 1 row in set (0.00 sec)
- mysql>
- --删除表格liuyan
- mysql> drop table liuyan;
- Query OK, 0 rows affected (0.00 sec)
- mysql> CREATE TABLE `liuyan` (
- -> `id` int(10) unsigned NOT NULL auto_increment,
- -> `title` varchar(32) NOT NULL,
- -> `author` varchar(32) NOT NULL default 'wu',
- -> `addtime` datetime default NULL,
- -> `content` text NOT NULL,
- -> PRIMARY KEY (`id`)
- -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- Query OK, 0 rows affected (0.05 sec)
- mysql>
- --添加一个测试表格
- mysql> create table aa(id int not null);
- Query OK, 0 rows affected (0.05 sec)
- --查看当前数据库下的表格
- mysql> show tables;
- +------------------+
- | Tables_in_fooknet |
- +------------------+
- | aa |
- | liuyan |
- +------------------+
- 2 rows in set (0.00 sec)
- --删除一个不存在的表格
- mysql> drop table bb;
- ERROR 1051 (42S02): Unknown table 'bb'
- --删除不存在的bb表,不报错
- mysql> drop table if exists bb;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql> drop table if exists aa;
- Query OK, 0 rows affected (0.00 sec)
- mysql> drop table if exists aa;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql>
- mysql>
- --查看表结构
- mysql> desc liuyan;
- +---------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | author | varchar(32) | NO | | wu | |
- | addtime | datetime | YES | | NULL | |
- | content | text | NO | | NULL | |
- +---------+------------------+------+-----+---------+----------------+
- 5 rows in set (0.02 sec)
- --采用标准方式添加一条数据
- mysql> insert into liuyan(id,title,author,addtime,content)
- -> values(1,'news','zhangsan','2012-05-22 12:32:50','hello mysql');
- Query OK, 1 row affected (0.01 sec)
- --查看留言表中的所有数据。(*表示所有列)
- mysql> select * from liuyan;
- +----+-------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- +----+-------+----------+---------------------+-------------+
- 1 row in set (0.00 sec)
- mysql>
- --添加数据(字段名的顺序可以颠倒)
- mysql> insert into liuyan(id,content,addtime,title,author)
- -> values(2,'aaaaa','2012-12-24 12:12:12','info','lisi');
- Query OK, 1 row affected (0.00 sec)
- --只指定部分字段名来添加数据
- mysql> insert into liuyan(title,addtime,content)
- -> values('test','2012-12-22','hello');
- Query OK, 1 row affected (0.00 sec)
- mysql> desc liuyan;
- +---------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | author | varchar(32) | NO | | wu | |
- | addtime | datetime | YES | | NULL | |
- | content | text | NO | | NULL | |
- +---------+------------------+------+-----+---------+----------------+
- 5 rows in set (0.00 sec)
- --不指定字段名添加数据(values中要给所有字段赋值,并顺序一致)
- mysql> insert into liuyan
- -> values(null,'demo','wangwu','2012-09-10 12:34:56','ddddd');
- Query OK, 1 row affected (0.00 sec)
- mysql> select * from liuyan;
- +----+-------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | lisi | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | wu | 2012-12-22 00:00:00 | hello |
- | 4 | demo | wangwu | 2012-09-10 12:34:56 | ddddd |
- +----+-------+----------+---------------------+-------------+
- 4 rows in set (0.00 sec)
- mysql>
- ---批量添加3条数据
- mysql> insert into liuyan
- -> values(null,'tt1','lisi1','2012-02-03 12:21:34','dddd'),
- -> (null,'tt2','lisi2','2012-02-03 12:21:34','eeee'),
- -> (null,'tt3','lisi3','2012-02-03 12:21:34','ffff');
- Query OK, 3 rows affected (0.00 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> select * from liuyan;
- +----+-------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | lisi | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | wu | 2012-12-22 00:00:00 | hello |
- | 4 | demo | wangwu | 2012-09-10 12:34:56 | ddddd |
- | 5 | tt1 | lisi1 | 2012-02-03 12:21:34 | dddd |
- | 6 | tt2 | lisi2 | 2012-02-03 12:21:34 | eeee |
- | 7 | tt3 | lisi3 | 2012-02-03 12:21:34 | ffff |
- +----+-------+----------+---------------------+-------------+
- 7 rows in set (0.00 sec)
- mysql>
- mysql> create table demo(
- -> d1 tinyint not null,
- -> d2 int(4) zerofill not null,
- -> d3 double(4,2) not null,
- -> d4 varchar(4) not null);
- Query OK, 0 rows affected (0.05 sec)
- mysql> desc demo;
- +-------+--------------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+--------------------------+------+-----+---------+-------+
- | d1 | tinyint(4) | NO | | NULL | |
- | d2 | int(4) unsigned zerofill | NO | | NULL | |
- | d3 | double(4,2) | NO | | NULL | |
- | d4 | varchar(4) | NO | | NULL | |
- +-------+--------------------------+------+-----+---------+-------+
- 4 rows in set (0.00 sec)
- mysql> show create table demo \G;
- *************************** 1. row ***************************
- Table: demo
- Create Table: CREATE TABLE `demo` (
- `d1` tinyint(4) NOT NULL,
- `d2` int(4) unsigned zerofill NOT NULL,
- `d3` double(4,2) NOT NULL,
- `d4` varchar(4) NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- mysql>mysql> desc demo;
- +-------+--------------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+--------------------------+------+-----+---------+-------+
- | d1 | tinyint(4) | NO | | NULL | |
- | d2 | int(4) unsigned zerofill | NO | | NULL | |
- | d3 | double(4,2) | NO | | NULL | |
- | d4 | varchar(4) | NO | | NULL | |
- +-------+--------------------------+------+-----+---------+-------+
- 4 rows in set (0.00 sec)
- mysql> insert into demo values(11,22,123.456,"abcdef");
- Query OK, 1 row affected, 2 warnings (0.00 sec)
- mysql> select * from demo;
- +----+------+-------+------+
- | d1 | d2 | d3 | d4 |
- +----+------+-------+------+
- | 11 | 0022 | 99.99 | abcd |
- +----+------+-------+------+
- 1 row in set (0.00 sec)
- mysql> insert into demo values(11,12345,12.3456,"abc");
- Query OK, 1 row affected (0.00 sec)
- mysql> select * from demo;
- +----+-------+-------+------+
- | d1 | d2 | d3 | d4 |
- +----+-------+-------+------+
- | 11 | 0022 | 99.99 | abcd |
- | 11 | 12345 | 12.35 | abc |
- +----+-------+-------+------+
- 2 rows in set (0.00 sec)
- mysql> insert into demo values(128,22,123.456,"abcdef");
- Query OK, 1 row affected, 3 warnings (0.00 sec)
- mysql> select * from demo;
- +-----+-------+-------+------+
- | d1 | d2 | d3 | d4 |
- +-----+-------+-------+------+
- | 11 | 0022 | 99.99 | abcd |
- | 11 | 12345 | 12.35 | abc |
- | 127 | 0022 | 99.99 | abcd |
- +-----+-------+-------+------+
- 3 rows in set (0.00 sec)
- mysql>
- ================================================================
- 表结构的修改
- ================================================================
- --创建一个测试表格
- mysql> create table news(
- -> title varchar(32) not null);
- Query OK, 0 rows affected (0.08 sec)
- --显示表结构
- mysql> desc news;
- +-------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+-------+
- | title | varchar(32) | NO | | NULL | |
- +-------+-------------+------+-----+---------+-------+
- 1 row in set (0.00 sec)
- --为表添加一个主键列
- mysql> alter table news add id int unsigned not null auto_increment primary key
- first;
- Query OK, 0 rows affected (0.09 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- --显示表结构
- mysql> desc news;
- +-------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- +-------+------------------+------+-----+---------+----------------+
- 2 rows in set (0.01 sec)
- mysql> alter table news add content text not null;
- Query OK, 0 rows affected (0.06 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- --显示表结构
- mysql> desc news;
- +---------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | content | text | NO | | NULL | |
- +---------+------------------+------+-----+---------+----------------+
- 3 rows in set (0.02 sec)
- --为表添加一个作者字段 varchar32位,在标题后面
- mysql> alter table news add author varchar(32) not null after title;
- Query OK, 0 rows affected (0.08 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- --显示表结构
- mysql> desc news;
- +---------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | author | varchar(32) | NO | | NULL | |
- | content | text | NO | | NULL | |
- +---------+------------------+------+-----+---------+----------------+
- 4 rows in set (0.02 sec)
- --将字段author修改为username类型varchar(16)
- mysql> alter table news change author username varchar(16) not null;
- Query OK, 0 rows affected (0.08 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- --显示表结构
- mysql> desc news;
- +----------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | username | varchar(16) | NO | | NULL | |
- | content | text | NO | | NULL | |
- +----------+------------------+------+-----+---------+----------------+
- 4 rows in set (0.01 sec)
- --删除一个字段
- mysql> alter table news drop content;
- Query OK, 0 rows affected (0.08 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- --显示表结构
- mysql> desc news;
- +----------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+------------------+------+-----+---------+----------------+
- | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | title | varchar(32) | NO | | NULL | |
- | username | varchar(16) | NO | | NULL | |
- +----------+------------------+------+-----+---------+----------------+
- 3 rows in set (0.01 sec)
- mysql>
- ================================================================
- 修改表数据
- ================================================================
- mysql>
- mysql> select * from liuyan;
- +----+-------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | lisi | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | wu | 2012-12-22 00:00:00 | hello |
- | 4 | demo | wangwu | 2012-09-10 12:34:56 | ddddd |
- | 5 | tt1 | lisi1 | 2012-02-03 12:21:34 | dddd |
- | 6 | tt2 | lisi2 | 2012-02-03 12:21:34 | eeee |
- | 7 | tt3 | lisi3 | 2012-02-03 12:21:34 | ffff |
- +----+-------+----------+---------------------+-------------+
- 7 rows in set (0.00 sec)
- --修改留言表中author字段值为qq(所有数据)
- mysql> update liuyan set author='qq';
- Query OK, 7 rows affected (0.00 sec)
- Rows matched: 7 Changed: 7 Warnings: 0
- mysql> select * from liuyan;
- +----+-------+--------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+--------+---------------------+-------------+
- | 1 | news | qq | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | qq | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | qq | 2012-12-22 00:00:00 | hello |
- | 4 | demo | qq | 2012-09-10 12:34:56 | ddddd |
- | 5 | tt1 | qq | 2012-02-03 12:21:34 | dddd |
- | 6 | tt2 | qq | 2012-02-03 12:21:34 | eeee |
- | 7 | tt3 | qq | 2012-02-03 12:21:34 | ffff |
- +----+-------+--------+---------------------+-------------+
- 7 rows in set (0.00 sec)
- --修改留言表中某列的某几条数据。
- mysql> update liuyan set author='zhangsan' where id in(1,3,5);
- Query OK, 3 rows affected (0.03 sec)
- Rows matched: 3 Changed: 3 Warnings: 0
- mysql> select * from liuyan;
- +----+-------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+-------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | qq | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | zhangsan | 2012-12-22 00:00:00 | hello |
- | 4 | demo | qq | 2012-09-10 12:34:56 | ddddd |
- | 5 | tt1 | zhangsan | 2012-02-03 12:21:34 | dddd |
- | 6 | tt2 | qq | 2012-02-03 12:21:34 | eeee |
- | 7 | tt3 | qq | 2012-02-03 12:21:34 | ffff |
- +----+-------+----------+---------------------+-------------+
- 7 rows in set (0.00 sec)
- --修改某条数据的某几列。
- mysql> update liuyan set title='llllll',author="lisi",content="qwertyuio"
- -> where id=7;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from liuyan;
- +----+--------+----------+---------------------+-------------+
- | id | title | author | addtime | content |
- +----+--------+----------+---------------------+-------------+
- | 1 | news | zhangsan | 2012-05-22 12:32:50 | hello mysql |
- | 2 | info | qq | 2012-12-24 12:12:12 | aaaaa |
- | 3 | test | zhangsan | 2012-12-22 00:00:00 | hello |
- | 4 | demo | qq | 2012-09-10 12:34:56 | ddddd |
- | 5 | tt1 | zhangsan | 2012-02-03 12:21:34 | dddd |
- | 6 | tt2 | qq | 2012-02-03 12:21:34 | eeee |
- | 7 | llllll | lisi | 2012-02-03 12:21:34 | qwertyuio |
- +----+--------+----------+---------------------+-------------+
- 7 rows in set (0.00 sec)
- --将6号信息中的作者改为王五
- mysql>update liuyan set author="wangwu" where id=6;
- mysql> select * from demo;
- +-----+-------+-------+------+
- | d1 | d2 | d3 | d4 |
- +-----+-------+-------+------+
- | 11 | 0022 | 99.99 | abcd |
- | 11 | 12345 | 12.35 | abc |
- | 127 | 0022 | 99.99 | abcd |
- +-----+-------+-------+------+
- 3 rows in set (0.00 sec)
- --将demo表中d1字段的所有值都减一。
- mysql> update demo set d1=d1-1;
- Query OK, 3 rows affected (0.00 sec)
- Rows matched: 3 Changed: 3 Warnings: 0
- mysql> select * from demo;
- +-----+-------+-------+------+
- | d1 | d2 | d3 | d4 |
- +-----+-------+-------+------+
- | 10 | 0022 | 99.99 | abcd |
- | 10 | 12345 | 12.35 | abc |
- | 126 | 0022 | 99.99 | abcd |
- +-----+-------+-------+------+
- 3 rows in set (0.00 sec)
复制代码 |
|