Mysq常用语句2
一)
創建,刪除和最基本查詢:
顯示數據庫 ? ? mysql->show databases;
創建數據庫 ? ? mysql->create database db;
刪除數據庫 mysql->drop database db;
選擇數據庫 ? ? mysql->use db
創建表 ? ? ? ? mysql->create table mytable(name varchar(20),sex(char(1),birth date);
刪除表 mysql->drop table mytable;
顯示表的內容 ? mysql->show tables;
顯示表的結構 ? mysql->describe mytable;
更新:
1、對列的操作:
在一個表中增加一條字段 mysql->alter table yourtable add ?name varchar(20)not null;
刪除一個字段 ?mysql->alter table yourtable drop name ;
2、對行的操作:
插入一條記錄 ? mysql->insert into mytable values('summer','m','1983-08-24');
刪除一條記錄 ?mysql->delete from mytable where name='summer';
修改一條記錄 ?mysql->update mytable set sex='vm' where name='summer';
插入多條記錄 ? mysql->insert into mytable ?select *from yourtable;(
這種形式的INSERT 語句中,新行的數據值不是在語句正文中明確地指定的.而是語句中指定的一個數據庫查詢. 該查詢的邏輯限制:
查詢不能含有ORDER BY子句. 查詢結果應含有與INSERT語句中列數目相同的列,且數據類型必須逐列兼容. )
簡單查詢:
1.在查詢結果中顯示列名
a.用as關鍵字:select name as '姓名' ? from students order by age
b.直接表示:select name '姓名' ? from students order by age
(二)
(1). 查詢語句:
? ? ? ? ? select username,uid from supesite.supe_userspaces where catid='91';
? ? ? ? ? select T1.p_w_picpath from supesite.supe_spacep_w_picpaths AS T1 INNER JOIN supesite.supe_spaceitems AS T2 ON ? ? ? ? ? ? T1.itemid = T2.itemid where T2.username = '".$username."' LIMIT 1;
? ? (2).插入語句:
? ? ? ? ? insert into cdb_members (username,password) values ('$username','$passwd');
? ? (3).更新語句:
? ? ? ? ?update vpopmail.vpopmail set pw_privilege='1' where pw_name='haha';
? ? (4).修改表結構語句:
? ? ? ? ?alter table vpopmail ? ? add pw_haha int (10) default null;
? ? ? ? ?alter table vpopmail ? ? drop pw_haha;
? ? ? ? ?alter table haha ? ? add uid int (10) not null auto_increment, add primary key (uid);
? ? (5). 創建表 數據庫:
? ? ? ? ? create table lian (a int,b char(10));
? ? ? ? ? create database jie;
? ? (6) .刪除數據庫 表 記錄:
? ? ? ?drop database jie;
? ? ? ?drop table lian;
? ? ? ?delete from lian where username='dd';
? ? (7) mysql 備份
? ? ? ? mysqldump --all-databases > all_databases.sql
? ? (8) mysql 恢復
? ? ? ? ?mysql < all_databases.sql
? ? (9) 創建mysql帳戶
? ? ? ? ?mysql> grant all privileges on *.* to 'lianbinjie'@'localhost'
? ? ? ? ? -> identified by '840611';
? ? ? ?mysql> GRANT SELECT,UPDATE ON *.* TO 'monty'@'%' (可以網絡訪問的賬戶)
? ? ? ? ?-> ? ? ? IDENTIFIED BY '840611';
? ?(10) ? ?更改已有帳戶的密碼
? ? ? ? mysql> grant all privileges on *.* to 'lianbinjie'@'localhost'
? ? ? ? ? ?-> identified by '840611';
? ? ? ? ?mysql> flush privileges;
.精確查找:
a.用in限定范圍:select * from students where native in ('湖南', '四川')
b.between...and:select * from students where age between 20 and 30
c. 比較測試符:(包括=,<>,<,<=,>,>=)select * from students where name = '李山'
d.like:select * from students where name ?like ?'李%' (注意查詢條件中有“%”,則說明是部分匹配,而且還有先后信息在里面,即查找以“李”開頭的匹配項。所以若查詢有“李”的所有對象,應該命令:'%李% ';若是第二個字為李,則應為'_李%'或'_李'或'_李_'。)
e.[]匹配檢查符:select * from courses where cno like '[AC]%' (表示或的關系,與"in(...)"類似,而且"[]"可以表示范圍,如:select * from courses where cno like '[A-C]%')注:關于這個字符我在mysql里用的時候mysql把它當兩個普通自符處理的。
[^]stockname like '[^F-M]%' --------- (^排除指定范圍)
a.count()求總數,如:select count(*) ?from students (求學生總人數)
b.avg(列)求平均,如:select avg(mark) ?from grades where cno=’B2’
c.max(列)和min(列),求最大與最小
轉載于:https://blog.51cto.com/study86/1331591
總結
- 上一篇: (转)Win7下如何硬盘安装Ubuntu
- 下一篇: 使用客户端对象模型读取SharePoin