MySQL common function script

Common function script

1. Export the entire database  

mysqldump -u 用户名 -p –default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)  

mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql  

2. Export a table  

mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名  

mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql    

3. Export a database structure  

mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:wcnc_db.sql  

-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table 

4. Import database  

A:常用source 命令  

进入mysql数据库控制台,  

如mysql -u root -p  

mysql>use 数据库  

然后使用source命令,后面参数为脚本文件(如这里用到的.sql)  

mysql>source wcnc_db.sql  

B:使用mysqldump命令  

mysqldump -u username -p dbname < filename.sql  

C:使用mysql命令  

mysql -u username -p -D dbname < filename.sql 

start and exit  

1. Enter MySQL: Start MySQL Command Line Client (DOS interface of MySQL), and directly enter the password during installation. The prompt at this time is: mysql>  

2. Exit MySQL: quit or exit  

 library operation  

1. Create a database  

Command: create database <database name>  

For example: create a database named sqlroad  

mysql> create database sqlroad;  

2. Display all databases  

Command: show databases (note: there is an s at the end)  

mysql> show databases;  

3. Delete the database  

Command: drop database <database name>  

For example: delete the database named sqlroad  

mysql> drop database sqlroad;  

4. Connect to the database  

Command: use <database name>  

For example: if the sqlroad database exists, try to access it: 

mysql> use sqlroad;  

Screen prompt: Database changed  

5. View the currently used database  

mysql> select database();  

6. Table information contained in the current database: 

mysql> show tables; (note: there is an s at the end)  

 Table operation, a database should be connected before the operation  

1. Create a table  

命令:create table <表名> ( <字段名> <类型> [,..<字段名n> <类型n>]);  

mysql> create table MyClass(  

> id int(4) not null primary key auto_increment,  

> name char(20) not null,  

> sex int(4) not null default ’′,  

> degree double(16,2));   

  2. Obtain the table structure  

命令:desc 表名,或者show columns from 表名  

mysql>DESCRIBE MyClass  

mysql> desc MyClass;  

mysql> show columns from MyClass;   

3. Delete table  

命令:drop table <表名>  

例如:删除表名为 MyClass 的表  

mysql> drop table MyClass;   

4. Insert data  

命令:insert into <表名> [( <字段名>[,..<字段名n> ])] values ( 值 )[, ( 值n )]  

例如,往表 MyClass中插入二条记录, 这二条记录表示:编号为的名为Tom的成绩为.45, 编号为 的名为Joan 的成绩为.99,编号为 的名为Wang 的成绩为.5.  

mysql> insert into MyClass values(1,’Tom’,96.45),(2,’Joan’,82.99), (2,’Wang’, 96.59);   

5. Query the data in the table  

1)、查询所有行  

命令:select <字段,字段,...> from < 表名 > where < 表达式 >  

例如:查看表 MyClass 中所有数据  

mysql> select * from MyClass;  

2)、查询前几行数据  

例如:查看表 MyClass 中前行数据  

mysql> select * from MyClass order by id limit 0,2;  

或者:  

mysql> select * from MyClass limit 0,2;    

6. Delete the data in the table  

命令:delete from 表名 where 表达式  

例如:删除表 MyClass中编号为 的记录  

mysql> delete from MyClass where id=1;    

  7. Modify the data in the table: update table name set field = new value,...where condition  

mysql> update MyClass set name=’Mary’where id=1;     

8. Add fields to the table:  

命令:alter table 表名 add字段 类型 其他;  

例如:在表MyClass中添加了一个字段passtest,类型为int(4),默认值为  

mysql> alter table MyClass add passtest int(4) default ’′  
     

9. Change the table name:  

命令:rename table 原表名 to 新表名;  

例如:在表MyClass名字更改为YouClass  

mysql> rename table MyClass to YouClass;  

更新字段内容  

update 表名 set 字段名 = 新内容  

update 表名 set 字段名 = replace(字段名,’旧内容’, 新内容’)  

update article set content=concat(‘  ’,content);   

  Field types and database operations

1. INT[(M)] type: normal-sized integer type  

2. DOUBLE[(M,D)] [ZEROFILL] type: normal size (double precision) floating point number type  

3. DATE date type: the supported range is -01-01 to -12-31. MySQL displays DATE values ​​in YYYY-MM-DD format, but allows you to assign values ​​to DATE columns using strings or numbers  

4. CHAR(M) type: fixed-length string type, when stored, always fill the right side with spaces to the specified length  

5. BLOB TEXT type, the maximum length is (2^16-1) characters. 

6. VARCHAR type: variable length string type  

7. Import database table  

创建.sql文件  

先产生一个库如auction.c:mysqlbin>mysqladmin -u root -p creat auction,会提示输入密码,然后成功创建。  

导入auction.sql文件  

c:mysqlbin>mysql -u root -p auction < auction.sql。  

通过以上操作,就可以创建了一个数据库auction以及其中的一个表auction。   

   8. modify database  

在mysql的表中增加字段:  

alter table dbname add column userid int(11) not null primary key auto_increment;  

这样,就在表dbname中添加了一个字段userid,类型为int(11)。    

  9. Authorization of mysql database  

mysql>grant select,insert,delete,create,drop  

on *.* (或test.*/user.*/..)  

to 用户名@localhost  

identified by ‘密码’;  

如:新建一个用户帐号以便可以访问数据库,需要进行如下操作:  

mysql> grant usage  

  -> ON test.*  

  -> TO testuser@localhost;  

  Query OK, 0 rows affected (0.15 sec)  

  此后就创建了一个新用户叫:testuser,这个用户只能从localhost连接到数据库并可以连接到test 数据库。下一步,我们必须指定testuser这个用户可以执行哪些操作:  

  mysql> GRANT select, insert, delete,update  

  -> ON test.*  

  -> TO testuser@localhost;  

  Query OK, 0 rows affected (0.00 sec)  

  此操作使testuser能够在每一个test数据库中的表执行SELECT,INSERT和DELETE以及UPDATE查询操作。现在我们结束操作并退出MySQL客户程序:  

  mysql> exit    

DDL operation

1: Use the SHOW statement to find out what databases currently exist on the server: 

mysql> SHOW DATABASES;  

2. Create a database MYSQLDATA  

mysql> Create DATABASE MYSQLDATA;  

3: Select the database you created  

mysql> USE MYSQLDATA; (Press the Enter key when Database changed appears, indicating that the operation is successful!)  

4: Check what tables exist in the current database  

mysql> SHOW TABLES;  

5: Create a database table  

mysql> Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));  

6: Display the structure of the table: 

mysql> DESCRIBE MYTABLE;  

7: Add records to the table  

mysql> insert into MYTABLE values (“hyq”,”M”);  

8: Load the data into the database table in text mode (eg D:/mysql.txt)  

mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt”INTO TABLE MYTABLE;  

9: Import the .sql file command (eg D:/mysql.sql)  

mysql>use database;  

mysql>source d:/mysql.sql;  

10: Delete table  

mysql>drop TABLE MYTABLE;  

11: Empty table  

mysql>delete from MYTABLE;  

12: Update the data in the table  

mysql>update MYTABLE set sex=”f”where name=’hyq’;

Guess you like

Origin blog.csdn.net/JACK_SUJAVA/article/details/130508574