思考题答案、在Win32平台上运行分发包中的setup.exe程序,安装MySQL系统后,就自动初始化授权表。对于Unix平台,运行脚本mysql_install_db,之前应该首先切换到启动mysqld的用户,例如mysql:#sumysql$mysql_install_db安装授权表之后修改root用户的密码,由于初始化后存在两个从localhost和任意主机连接的root用户,所以推荐使用下面的办法:$mysql-urootmysql(由于现在不存在密码,因此不比提供-p选项)mysqlUPDATEuserSETpassword=PASSWORD(“mypass”)WHEREUser=root;3、使用如下命令更改密码:shellmysqladmin-uroot-ppassword‘newpass’EnterPassword:*******出现EnterPassword的提示后输入原来的密码oldpass即可。读者可以尝试其它所有本章介绍的方法。4、首先以root用户的身份连接到服务器:shellmysql-uroot-pEnterpassword:*******出现Enterpassword提后输入root用户的密码,然后即进入mysql客户机的交互模式,可以看到下面的提示:WelcometotheMySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis4toserverversion:3.23.25-beta-logType'help;'or'\h'forhelp.Type'\c'toclearthebuffermysql然后发布查询,直接键入题目中的语句:mysqlSELECTUser,HostFROMmysql.user;MySQL金典教程应该有类似于下面的结果:+-------+-----------+|User|Host|+-------+-----------+|root|%||admin|localhost||root|localhost|+-------+-----------+5、在全局选项文件(Unix上位于/etc/my.cnf,Windows上位于c:\my.cnf)中加入下面的几行:[mysql]user=rootpassword然后在运行mysql客户程序,就不必提供参数:shellmysqlEnterPassword:********你可以查看当前的连接,以确定是否是如此:mysqlSHOWPROCESSLIST;+----+------+-----------+------+---------+------+-------+------------------+|Id|User|Host|db|Command|Time|State|Info|+----+------+-----------+------+---------+------+-------+------------------+|4|root|localhost|NULL|Query|0|NULL|SHOWPROCESSLIST|+----+------+-----------+------+---------+------+-------+------------------+第三章2、连接服务器的命令为:shellmysql-hdatabase.horst.zoo.net-uroot-ptestEnterPassword:******如果使用选项文件,将下面几行加入全局选项文件中:[mysql]host=database.horst.zoo.netuser=rootpassword然后可以直接运行mysql,不比提供连接参数:思考题答案*******3、创建表的语句为:CREATETABLEpet(nameCHAR(30),ownerCHAR(30),speciesCHAR(10),sexENUM(“M”,”F”)NOTNULL,birthDATE,deathDATE)4、如下录入文件pet.txt,段与段用制表符分隔FluffyHaroldcatF1993-02-04\NClawsGwencatM1994-03-17\NBuffyHarolddogF1989-05-13\NChirpyGwenbirdF1998-09-11\NFangBennydogM1990-08-27\NBowserDianedogM1990-08-311995-07-29WhistlerGwenbird\N1997-12-09\NSlimBennysnakeM1996-04-29\NPuffballDianehamsterF1999-03-30\N然后连接服务器,发布查询:mysqlLOADDATAINFILE“pet.txt”INTOTABLEpet;由于缺省时,LOADDATA语句以特殊字符’\n’作为记录的结束,这和Unix系统的文本编辑器以’\n’为换行符是一致的,但是在Windows系统中,换行符是’\r\n’,因此,如果是在Windows系统编辑的文件,那么上面的语句不能成功的录入数据,要做如下的修改:mysqlLOADDATAINFILE“pet.txt”INTOTABLEpet-LINESTERMINATEDBY‘\r\n’;如果使用mysqlimport程序,相应的命令行为:shellmysqlimporttest‘pet.tt’或者使用:shellmysqlimporttest‘pet.tt’--lines-terminated-by=’\r\n’MySQL金典教程、创建表:mysqlCREATETABLEex4-(-dataFLOAT,-birthDATETIME-);录入数据:mysqlINSERTex4VALUES(RAND(),NOW());多录入几个数据,现有的数据为:mysqlselect*fromex4;+----------+---------------------+|data|birth|+----------+---------------------+|0.830329|2001-01-0121:21:10||0.531143|2001-01-0121:21:12||0.164729|2001-01-0121:21:13||0.230213|2001-01-0121:21:14|+----------+---------------------+data列的平均值:mysqlSELECTAVG(data)ASaverageFROMex4;+------------------+|average|+------------------+|0.43910377845168|+------------------+data列的总和:mysqlSELECTSUM(data)ASsumofdataFROMex4;+-----------------+|sumofdata|+-----------------+|1.7564151138067|+-----------------+mysqlSELECTMAX(data)ASmaxofdata,MIN(data)ASminofdata-FROMex4;+------------------+------------------+|maxofdata|minofdata|思考题答案|0.83032947778702|0.16472874581814|+------------------+------------------+data列降序排列:mysqlSELECT*FROMex4ORDERBYdataDESC;+----------+---------------------+|data|birth|+----------+---------------------+|0.830329|2001-02-1821:21:10||0.531143|2001-02-1821:21:12||0.230213|2001-02-1821:21:14||0.164729|2001-02-1821:21:13|+----------+---------------------+2、使用标准SQL模式匹配:mysqlselect*fromex4wherebirthlike2001-01-01%;+----------+---------------------+|data|birth|+----------+---------------------+|0.830329|2001-01-0121:21:10||0.531143|2001-01-0121:21:12||0.164729|2001-01-0121:21:13||0.230213|2001-01-0121:21:14|+----------+---------------------+使用扩张正则表达式模式匹配:mysqlselect*fromex4wherebirthREGEXP^2001-01-01;+----------+---------------------+|data|birth|+----------+---------------------+|0.830329|2001-01-0121:21:10||0.531143|2001-01-0121:21:12||0.164729|2001-01-0121:21:13||0.230213|2001-01-0121:21:14|+----------+---------------------+3、为student表创建索引:mysqlALTERTABLEstudent-ADDPRIMARYKEY(id),MySQL金典教程(english,chinese,history);这样查看创建的索引:mysqlSHOWINDEXFROMstudent;+---------+------------+----------+--------------+-------------+---|Table|Non_unique|Key_name|Seq_in_index|Column_name|...+---------+------------+----------+--------------+-------------+---|student|0|PRIMARY|1|id|...|student|1|mark|1|english|...|student|1|mark|2|chinese|...|student|1|mark|3|history|...+---------+------------+----------+--------------+-------------+---为pet表创建索引,这次使用另一种方法:mysqlCREATEINDEXnameONpet(name(10),owner(10));查看创建的索引的情况:mysqlSHOWINDEXFROMpet;+-------+------------+----------+--------------+-------------+-|Table|Non_unique|Key_n