mysql_1
1.mysql> select NOW();等效于select user()\g
+---------------------+
| NOW() |
+---------------------+
| 2015-11-07 10:29:04 |
+---------------------+
1 row in set (0.00 sec)
mysql> select NOW(),USER(),VERSION();
+---------------------+----------------+-----------------+
| NOW() | USER() | VERSION() |
+---------------------+----------------+-----------------+
| 2015-11-07 10:32:18 | root@localhost | 5.5.35-1ubuntu1 |
+---------------------+----------------+-----------------+
1 row in set (0.00 sec)
mysql> select NOW(),USER(),VERSION()\g
+---------------------+----------------+-----------------+
| NOW() | USER() | VERSION() |
+---------------------+----------------+-----------------+
| 2015-11-07 10:35:37 | root@localhost | 5.5.35-1ubuntu1 |
+---------------------+----------------+-----------------+
1 row in set (0.00 sec)
mysql> select NOW(),USER(),VERSION()\G
*************************** 1. row ***************************
NOW(): 2015-11-07 10:35:41
USER(): root@localhost
VERSION(): 5.5.35-1ubuntu1
1 row in set (0.00 sec)
取消
mysql> select user(),
-> now(),
-> \c
mysql>
创建数据库
mysql> create database sampdb;
Query OK, 1 row affected (0.02 sec)
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use sampdb; $mysql sampdb -u root -p
$mysql -h hostname -u root -p sampdb
mysql> select database();
Database changed
+------------+
| database() |
+------------+
| sampdb |
+------------+
1 row in set (0.00 sec)
插入数据
mysql> insert into member values(last_insert_id(), 'james','john','s'),(last_insert_id(),'bush','pack','s');
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into member(lastname,firstname,status) values('hacker','johner','s'),('busher','packer','s');
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
alter add命令用来增加表的字段。
alter add命令格式:alter table 表名 add字段 类型 其他;
例如,在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0:
mysql> alter table MyClass add passtest int(4) default '0';
1) 加索引
mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);
例子: mysql> alter table employee add index emp_name (name);
2) 加主关键字的索引
mysql> alter table 表名 add primary key (字段名);
例子: mysql> alter table employee add primary key(id);
3) 加唯一限制条件的索引
mysql> alter table 表名 add unique 索引名 (字段名);
例子: mysql> alter table employee add unique emp_name2(cardnumber);
4) 删除某个索引
mysql> alter table 表名 drop index 索引名;
例子: mysql>alter table employee drop index emp_name;
5) 增加字段
mysql> ALTER TABLE table_name ADD field_name field_type;
6) 修改原字段名称及类型
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
7) 删除字段
MySQL ALTER TABLE table_name DROP field_name;
mysqldump命令用来备份数据库。
mysqldump命令在DOS的[url=file://\\mysql\\bin]\\mysql\\bin[/url]目录下执行。
1) 导出整个数据库(导出文件默认是存在mysql\bin目录下)
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u user_name -p123456 database_name > outfile_name.sql
2) 导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u user_name -p database_name table_name > outfile_name.sql
3) 导出一个数据库结构
mysqldump -u user_name -p -d –add-drop-table database_name > outfile_name.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table
导入数据库
mysql> source /home/scictor/Desktop/mysql/sampdb_member.sql
$mysql -u root -p databasename < db.sql
Query OK, 1 row affected (0.01 sec)
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.10 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 9 rows affected (0.04 sec)
Records: 9 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
4) 带语言参数导出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql
例如,将aaa库备份到文件back_aaa中:
[root@test1 root]# cd /home/data/mysql
[root@test1 mysql]# mysqldump -u root -p --opt aaa > back_aaa
-- MySQL dump 10.13 Distrib 5.5.35, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: sampdb
-- ------------------------------------------------------
-- Server version 5.5.35-1ubuntu1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `member`
--
DROP TABLE IF EXISTS `member`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `member` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lastname` varchar(20) NOT NULL,
`firstname` varchar(20) NOT NULL,
`status` varchar(2) NOT NULL,
`birthday` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `member`
--
LOCK TABLES `member` WRITE;
/*!40000 ALTER TABLE `member` DISABLE KEYS */;
INSERT INTO `member` VALUES (1,'james','john','s','0000-00-00'),(2,'bush','pack','s','0000-00-00'),(3,'hacker','johner','s','0000-00-00'),(4,'busher','packer','s','0000-00-00');
/*!40000 ALTER TABLE `member` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2015-11-07 16:03:48