mysql常用動作陳述式

來源:互聯網
上載者:User

標籤:mysql資料庫   tom   匯入資料   roc   ges   val   ext   語句   alt   

mysql常用動作陳述式

 

 

1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 

2.列出資料庫:

1.show databases; 

3.選擇資料庫:

1.use databases_name; 

4.列出資料表:

1.show tables; 

5.顯示表格列的屬性:

1.show columns from table_name;   2.describe table_name; 

6.匯出整個資料庫:

1.mysqldump -u user_name -p database_name > /tmp/file_name 

例如:mysqldump -u root -p test_db > d:/test_db.sql

7.匯出一個表:

1.mysqldump -u user_name -p database_name table_name > /tmp/file_name 

例如:mysqldump -u root -p test_db table1 > d:/table1.sql

8.匯出一個資料庫結構:

1.mysqldump -u user_name -p -d --add-drop-table database_name > file_name 

例如:mysqldump -u root -p -d --add-drop-table test_db > test_db.sql

9.匯入資料庫:

1.source file_name;   2.或   3.mysql -u user_name -p database_name < file_name 

例如:

source /tmp/bbs.sql;

source d:/bbs.sql;

mysql -u root -p bbs < "d:/bbs.sql"

mysql -u root -p bbs < "/tmp/bbs.sql"

10.將文字檔匯入資料表中(excel與之相同)

1.load data infile "tables.txt" into table table_name; 

例如:

load data infile "/tmp/bbs.txt" into table bbs;

load data infile "/tmp/bbs.xls" into table bbs;

load data infile "d:/bbs.txt" into table bbs;

load data infile "d:/bbs.xls" into table bbs;

11.將資料表匯出為文字檔(excel與之相同)

1.select * into outfile "path_file_name" from table_name; 

例如:

select * into outfile "/tmp/bbs.txt" from bbs;

select * into outfile "/tmp/bbs.xls" from bbs where id=1;

select * into outfile "d:/bbs.txt" from bbs;

select * into outfile "d:/bbs.xls" from bbs where id=1;

12.建立資料庫時先判斷資料庫是否存在:

1.create database if not exists database_name; 

例如:create database if not exists bbs

13.建立資料庫:

1.create database database_name; 

例如:create database bbs;

14.刪除資料庫:

1.drop database database_name; 

例如:drop database bbs;

15.建立資料表:

1.mysql> create table <table_name> ( <column 1 name> <col. 1 type> <col. 1 details>,<column 2 name> <col. 2 type> <col. 2 details>, ...); 

例如:create table (id int not null auto_increment primary key,name char(16) not null default "jack",date_year date not null);

16.刪除資料表中資料:

1.delete from table_name; 

例如:

delete from bbs;

delete from bbs where id=2;

17.刪除資料庫中的資料表:

1.drop table table_name; 

例如:

drop table test_db;

rm -f database_name/table_name.* (linux下)

例如:

rm -rf bbs/accp.*

18.向資料庫中添加資料:

1.insert into table_name set column_name1=value1,column_name2=value2; 

例如:insert into bbs set name="jack",date_year="1993-10-01";

1.insert into table_name values (column1,column2,...); 

例如:insert into bbs ("2","jack","1993-10-02")

1.insert into table_name (column_name1,column_name2,...) values (value1,value2); 

例如:insert into bbs (name,data_year) values ("jack","1993-10-01");

19.查詢資料表中的資料:

1.select * from table_name; 

例如:select * from bbs where id=1;

20.修改資料表中的資料:

1.update table_name set col_name=new_value where id=1; 

例如:update bbs set name="tom" where name="jack";

21.增加一個欄位:

1.alter table table_name add column field_name datatype not null default "1"; 

例如:alter table bbs add column tel char(16) not null;

22.增加多個欄位:(column可省略不寫)

1.alter table table_name add column filed_name1 datatype,add column filed_name2 datatype; 

例如:alter table bbs add column tel char(16) not null,add column address text;

23.刪除一個欄位:

1.alter table table_name drop field_name; 

例如:alter table bbs drop tel;

24.修改欄位的資料類型:

1.alter table table_name modify id int unsigned;//修改列id的類型為int unsigned    2.alter table table_name change id sid int unsigned;//修改列id的名字為sid,而且把屬性修改為int unsigned 

25.修改一個欄位的預設值:

1.alter table table_name modify column_name datatype not null default ""; 

例如:alter table test_db modify name char(16) default not null "yourname";

26.對錶重新命名:

1.alter table table_name rename as new_table_name; 

例如:alter table bbs rename as bbs_table;

1.rename table old_table_name to new_table_name; 

例如:rename table test_db to accp;

27.從已經有的表中複製表的結構:

1.create table table2 select * from table1 where 1<>1; 

例如:create table test_db select * from accp where 1<>1;

28.查詢時間:

1.select now(); 

29.查詢目前使用者:

1.select user(); 

30.查詢資料庫版本:

1.select version(); 

31.建立索引:

1.alter table table1 add index ind_id(id);   2.create index ind_id on table1(id);   3.create unique index ind_id on table1(id);//建立唯一性索引 

32.刪除索引:

1.drop index idx_id on table1;   2.alter table table1 drop index ind_id; 

33.聯合字元或者多個列(將id與":"和列name和"="串連)

1.select concat(id,‘:‘,name,‘=‘) from table; 

34.limit(選出10到20條)

1.select * from bbs order by id limit 9,10; 

(從查詢結果中列出第幾到幾條的記錄)

35.增加一個管理員帳號:

1.grant all on *.* to [email protected] identified by "password"; 

36.建立表是先判斷表是否存在

1.create table if not exists students(……); 

37.複製表:

1.create table table2 select * from table1; 

例如:create table test_db select * from accp;

38.授於使用者遠端存取mysql的許可權

1.grant all privileges on *.* to "root"@"%" identified by "password" with grant option; 

或者是修改mysql資料庫中的user表中的host欄位

1.use mysql;   2.select user,host from user;   3.update user set host="%" where user="user_name"; 

39.查看目前狀態

1.show status; 

40.查看當前串連的使用者

1.show processlist; 

(如果是root使用者,則查看全部的線程,得到的使用者串連數同show status;裡的 Threads_connected值是相同的)

mysql常用動作陳述式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.