《MYSQL教程MySQL的常用命令集锦》要点:
本文介绍了MYSQL教程MySQL的常用命令集锦,希望对您有用。如果有疑问,可以联系我们。
MYSQL学习下面是我们经常会用到且非常有用的MySQL命令.下面你看到#表示在Unix命令行下执行命令,看到mysql>表示当前已经登录MySQL服务器,是在mysql客户端执行mysql命令.
登录MySQL,如果连接远程数据库,需要用-h指定hostname.
MYSQL学习登录MySQL,如果连接远程数据库,需要用-h指定hostname.
MYSQL学习
# [mysql dir]/bin/mysql -h hostname -u root -p
MYSQL学习创建一个数据库.
MYSQL学习
mysql> create database [databasename];
MYSQL学习列出所有数据库.
MYSQL学习
mysql> show databases;
MYSQL学习切换到一个数据库.
MYSQL学习
mysql> use [db name];
MYSQL学习显示一个数据库的所有表.
MYSQL学习
mysql> show tables;
MYSQL学习查看数据表的字段格式.
MYSQL学习
mysql> describe [table name];
MYSQL学习删除一个数据库.
MYSQL学习
mysql> drop database [database name];
MYSQL学习删除一个数据表.
MYSQL学习
mysql> drop table [table name];
MYSQL学习显示一个数据表的所有数据.
MYSQL学习
mysql> SELECT * FROM [table name];
MYSQL学习返回指定数据表的各列信息.
MYSQL学习
mysql> show columns from [table name];
MYSQL学习使用值“whatever”过滤显示选定的某些行.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
MYSQL学习显示所有包含name为”Bob”和phone number为“3444444”的记录.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
MYSQL学习显示所有不包含name为”Bob”和phone number为“3444444”的记录,并以phone_number字段排序.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
MYSQL学习显示所有的name以字母“bob”开头和phone number为“3444444”的记录.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
MYSQL学习显示name以字母“bob”开头和phone number为“3444444”的第1至第5条记录.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
MYSQL学习使用正则表达式查找记录.使用“正则表达式二进制”强制区分大小写.此命令查找以a开头的任何记录.
MYSQL学习
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
MYSQL学习返回唯一不同的记录.
MYSQL学习
mysql> SELECT DISTINCT [column name] FROM [table name];
MYSQL学习以升序或降序显示选定的记录.
MYSQL学习
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
MYSQL学习返回行数.
MYSQL学习
mysql> SELECT COUNT(*) FROM [table name];
MYSQL学习统计指定列值的总和.
MYSQL学习
mysql> SELECT SUM(*) FROM [table name];
MYSQL学习联结表.
MYSQL学习
mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
MYSQL学习新建一个用户.以root登录.切换到mysql数据库,创建用户,刷新权限.
MYSQL学习
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
MYSQL学习从unix命令行更改用户暗码.
MYSQL学习
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
MYSQL学习
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
MYSQL学习
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
MYSQL学习
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
MYSQL学习
# mysqladmin -u root password newpassword
MYSQL学习
# mysqladmin -u root -p oldpassword newpassword
MYSQL学习
# mysql -u root -p
MYSQL学习
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;
MYSQL学习
mysqldump -h database_ip -u Username --password=123456 --opt databasename > backup-file.sql
mysqldump -h database_ip -d -u Username --password=123456 databasename >database_structure.sql
MYSQL学习
# mysql -u root -p
MYSQL学习
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
MYSQL学习
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
MYSQL学习
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
MYSQL学习
mysql> DELETE from [table name] where [field name] = 'whatever';
MYSQL学习
mysql> flush privileges;
MYSQL学习
mysql> alter table [table name] drop column [column name];
MYSQL学习
mysql> alter table [table name] add column [new column name] varchar (20);
MYSQL学习
mysql> alter table [table name] change [old column name] [new column name] varchar (50);
MYSQL学习
mysql> alter table [table name] add unique ([column name]);
MYSQL学习
mysql> alter table [table name] modify [column name] VARCHAR(3);
MYSQL学习
mysql> alter table [table name] drop index [colmn name];
MYSQL学习
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
MYSQL学习
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
MYSQL学习
# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
MYSQL学习
# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
MYSQL学习
# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
MYSQL学习
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
MYSQL学习
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
MYSQL学习
select title from book into outfile '/tmp/outfile.txt';
MYSQL学习
select * from people where peopleId in (select peopleId from people group by
peopleId having count(peopleId) > 1);
MYSQL学习
select * from phome_ecms_wma where title in (select distinct title from phome_ecms_wma);
MYSQL学习
select *,count(distinct title) INTO OUTFILE '/tmp/table.bak' from phome_ecms_wma group by title;
delete from phome_ecms_wma;
LOAD DATA INFILE '/tmp/table.bak' REPLACE INTO TABLE phome_ecms_wma character set utf8;
MYSQL学习
SELECT *FROM url ORDER BY RAND() LIMIT 5;
MYSQL学习
mysql> show variables like "character_set%";
MYSQL学习
mysql> alter table table_name change last_action last_action datetime NOT NULL default '0000-00-00 00:00:00';
MYSQL学习
mysql> ALTER TABLE host ADD ks_mac VARCHAR(100);
MYSQL学习
mysql> ALTER TABLE table_name DROP field_name;
MYSQL学习
mysql>alter table t1 rename t2;
MYSQL学习
mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);
mysql> alter table tablename add index emp_name (name);
MYSQL学习
mysql> alter table tablename add primary key(id);
MYSQL学习
mysql> alter table tablename add unique emp_name2(cardnumber);
MYSQL学习
mysql>alter table tablename drop index emp_name;
MYSQL学习
mysql> GRANT ALL PRIVILEGES ON database_test.* to root@192.168.1.9 IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
欢迎参与《MYSQL教程MySQL的常用命令集锦》讨论,分享您的想法,维易PHP学院为您提供专业教程。