SQL之mysql常用操作语句(入门级)

1.进入数据库

mysql-uroot-p

mysql-hlocalhost-uroot-pdatabase_name

2.列出数据库

showdatabases;

3.选择数据库:

usedatabases_name;

4.列出数据表:

showtables;

5.显示表格列的属性

showcolumnsfromtable_name;

describetable_name;

6.导出整个数据库:

mysqldump-uuser_name-pdatabase_name>/tmp/file_name

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

7.导出一个表:

mysqldump-uuser_name-pdatabase_nametable_name>/tmp/file_name

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

8.导出一个数据库结构:

mysqldump-uuser_name-p-d--add-drop-tabledatabase_name>file_name

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

9.导入数据库:

sourcefile_name;

或 mysql-uuser_name-pdatabase_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与之相同)loaddatainfile"tables.txt"intotabletable_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与之相同)

select*intooutfile"path_file_name"fromtable_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.创建数据库时先判断数据库是否存在:

createdatabaseifnotexistsdatabase_name;

例如:create database if not exists bbs

13.创建数据库:

createdatabasedatabase_name;

例如:create database bbs;

14.除数据库:

dropdatabasedatabase_name;

例如:drop database bbs;

15.创建数据表:

mysql>createtable<table_name>(<column1name><col.1type><col.1details>,<column2name><col.2type><col.2details>,...);

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

16.删除数据表中数据:

deletefromtable_name;

例如:

delete from bbs;

delete from bbs where id=2;

17.删除数据库中的数据表:

droptabletable_name;

例如:

drop table test_db;

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

例如:

rm -rf bbs/accp.*

18.向数据库中添加数据:

insertintotable_namesetcolumn_name1=value1,column_name2=value2;

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

insertintotable_namevalues(column1,column2,...);

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

insertintotable_name(column_name1,column_name2,...)values(value1,value2);

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

19.查询数据表中的数据:

select*fromtable_name;

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

20.修改数据表中的数据:updatetable_namesetcol_name=new_valuewhereid=1;

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

21.增加一个字段:altertabletable_nameaddcolumnfield_namedatatypenotnulldefault"1";

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

22.增加多个字段:(column可省略不写)

altertabletable_nameaddcolumnfiled_name1datatype,addcolumnfiled_name2datatype;

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

23.删除一个字段:altertabletable_namedropfield_name;

例如:alter table bbs drop tel;

24.修改字段的数据类型:

altertabletable_namemodifyidintunsigned;//修改列id的类型为intunsigned altertabletable_namechangeidsidintunsigned;//修改列id的名字为sid,而且把属性修改为intunsigned

25.修改一个字段的默认值:

altertabletable_namemodifycolumn_namedatatypenotnulldefault"";

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

26.对表重新命名:

altertabletable_namerenameasnew_table_name;

例如:alter table bbs rename as bbs_table;

renametableold_table_nametonew_table_name;

例如:rename table test_db to accp;

27.从已经有的表中复制表的结构:

createtabletable2select*fromtable1where1<>1;

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

28.查询时间:

selectnow();

29.查询当前用户:

selectuser();

30.查询数据库版本:

selectversion();

31.创建索引:

altertabletable1addindexind_id(id);

createindexind_idontable1(id);

createuniqueindexind_idontable1(id);//建立唯一性索引

32.删除索引:

dropindexidx_idontable1;

altertabletable1dropindexind_id;

33.联合字符或者多个列(将id与":"和列name和"="连接)

selectconcat(id,':',name,'=')fromtable;

34.limit(选出10到20条)

select*frombbsorderbyidlimit9,10;

(从查询结果中列出第几到几条的记录)

35.增加一个管理员账号

grantallon*.*touser@localhostidentifiedby"password";

36.创建表是先判断表是否存在

createtableifnotexistsstudents(……);

37.复制表:

createtabletable2select*fromtable1;

例如:create table test_db select * from accp;

38.授于用户远程访问mysql的权限

grantallprivilegeson*.*to"root"@"%"identifiedby"password"withgrantoption;

或者是修改mysql数据库中的user表中的host字段

usemysql;

selectuser,hostfromuser;

updateusersethost="%"whereuser="user_name";

39.查看当前状态

showstatus;

40.查看当前连接的用户

showprocesslist;

41.常用函数 ifnull ==(oracle, nvl)

select ifnull(b,0) from c;

SQL之mysql常用操作语句(入门级)

42.sum(...) over( ),对所有行求和

43.sum(...) over( order by ...)

44.sum(...) over( partition by... order by ... )

45.percent_rank() over(partition by init_date,interval_type order by drawdown) 数据占的百分占比

46.