mysql基礎語句

MySQL:衆多關係型數據庫中的一種
倉庫 --數據庫
箱子 --表
數據庫:
進入mysql 命令行: mysql -uroot -p
查看全部數據庫: show databases;
建立數據庫: create database niu charset utf8;
刪除數據庫: drop database niu;
選擇數據庫: use databases;
查看全部表: show tables;
查看建立數據庫的語句:show create database databasename;
查看建立表的語句:show create table tablename;
查看錶結構:desc tablenmae;
表:
約束
#自增加
auto_increment
#非空
not null
#默認值
default 'xx'
#惟一
unique
#指定字符集
charset
#主鍵
primary key
#外鍵
增長兩個表之間的聯繫
增:
#學生表
create table students(
id int auto_increment primary key,
name varchar(10) not null,
sex varchar(3) default '女',
address varchar(50),
phone int not null unique,
age,
);
#成績表
create table scores(
id int auto_increnent primary key,
s_id int not null,
grade float not null,
);
刪:
drop table tablename;
truncate tablename;#快速刪除表
改:
alter table oldtable rename newtable; #改表名
alter table tablename modify name varchar(20);#改表結構
alter table tablename change name newname varchar(20);#改表結構
alter table tablename add age float after name;#新增字段的位置mysql

查:
show create table tablename ;#查看新建表語句
desc table;#查看錶結構
show tables ;#查看全部表sql

數據:

insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
insert into student values('','小明',100,'',120);

turncate tablename; #刪除整表數據,自增加id從頭再來,快速,從磁盤直接刪除,不可恢復
delete from student; 
#刪除整個表的數據,自增加繼續

update student set money=100;#不指定條件,修改全部
update student set money=110 where name='hk';#只改hk
自動提交
取消自動提交   set @@autocommitt=0;
                select @@autocommitt=0;
#自動提交取消後,當前會話顯示已經成功執行,其實後臺並無執行數據庫

查:
select * from students limit 1,5; #從第幾條開始,下面的x條,不包含開始的那一條
SELECT * from students limit 5;查詢5條
SELECT id,stu_name,sex,money,phone from students;#指定查詢的字段
SELECT * from students;#查詢全部的數據
SELECT * from students where sex='男';#指定條件
SELECT * from students where sex='男' and money>100; #多個條件,必須同時知足
SELECT * from students where sex='男' or sex='未知' ; #多個條件,有一個知足便可
SELECT * from students where sex !='男'; #<>也是不等於
SELECT * FROM students where addr like '%東京%';#模糊匹配,%表明的是通配符,必須得用like
SELECT * from students a where a.stu_name like '姚_';#_通配符表示任意一個單字符,姚字後面只能跟一個字
SELECT a.stu_name '學生名稱',a.phone '學生電話' from students as a where a.stu_name='姚遠';#給表起別名,as能夠省略
SELECT * from students a where a.stu_name in ('牛牛','林倩','林遠');# in
SELECT * from students a where a.money BETWEEN 1000 and 10000;#在什麼什麼之間的數據
SELECT * from students ORDER BY money desc;
#order by xxx desc,根據哪一個字段繼續排序,默認是升序,
降序是desc,升序asc
SELECT * from students a where a.addr = '' or a.addr is null; #查詢字段爲空的數據
SELECT DISTINCT a.money from students a ;#去重
SELECT COUNT(*) '學生人數' from students where sex='女'; #統計行數
SELECT MAX(a.money) 錢最多 from students a; #最大值
SELECT min(money) 錢最少 from students;#最小值
SELECT AVG(a.money) 平均多少錢 from students a; #平均數
SELECT sum(a.money) 總共多少錢 from students a;#總和
SELECT sex 性別,count(*) 人數 from students GROUP BY sex; #分組
SELECT
sex 性別,
count(*) 人數,
a.stu_name 名字命令行

FROM
students a  WHERE a.money > 300 GROUP BY a.id HAVING a.stu_name LIKE '姚%';
#若是group by後面有條件的話,必須得用having子句,having子句裏面用到的字段必須出如今select後面,若是group by和order by一塊兒用的話,order by必須寫在group by後面
SELECT *,COUNT(*) from students GROUP BY sex,class; #多個字段進行分組排序

SELECT id,stu_name from students UNION SELECT id,t_name from teacher;
#用來合併兩條select語句的結果,兩條select語句字段數量要一致,而且數據類型也要一致
union和union all的區別就是一個會去重一個不會ip

多表關聯:
SELECT * FROM USER a, accounts b WHERE
a.id = b.user_id
AND a.username = 'niuhy';
-- SELECT * from students a ,scores b where a.id=b.s_id; -- 多表關聯
-- 兩個表裏面都存在的數據查出來
SELECT * from students a LEFT JOIN scores b on a.id=b.s_id;
-- LEFT JOIN會把左邊表全部的數據都查出來,右邊表有匹配的就查出來
SELECT * from students a RIGHT JOIN scores b on a.id=b.s_id;
-- RIGHT JOIN會把右邊表全部的數據都查出來,左邊表有匹配的就查出來
SELECT * from students a inner JOIN scores b on a.id=b.s_id;
-- INNER JOIN兩邊表裏都匹配的數據才查到
子查詢:
把一條sql的結果,做爲另外一條sql的條件
SELECT * from scores a where a.s_id = (SELECT id from students where stu_name='牛牛');rem

把子查詢當成一個表
SELECT
a.grade 成績,
b.stu_name 學生名稱,
b.id 學號
FROM
scores a,
( SELECT id,stu_name FROM students WHERE stu_name = '牛牛') b
WHERE
a.s_id = b.id;
數據庫權限:
mysql數據的權限實質上都是在user表裏控制的
一、grant
#全部的權限 全部數據庫下面的全部表 用戶 用戶ip 
grant all on *.* to 'andashu'@'localhost' IDENTIFIED BY '123456' with grant option;
密碼 #有執行grant語句的權限
grant all on *.* to 'andashu'@'%' IDENTIFIED BY '123456' with grant option;
取消受權:
Revoke select on *.* from dba@localhost;
Revoke all on *.* from andashu@localhost;字符串

二、修改user表的數據
對user表進行增長、修改和刪除
flush privileges;#刷新權限
備份數據庫:
mysqldump -uroot -p123456 db > db.sql
mysqldump -uroot -p123456 -A > all.sql
恢復數據:
mysql -uroot -p123456 db < db.sql
存儲過程:
批量的造數據
delimiter $$; #爲了改結束符
CREATE PROCEDURE big_data1(num int)#表明要造多少條數據 100
BEGIN
DECLARE i int;
set i=0;
WHILE i<num do
insert into students (stu_name,money) VALUES (CONCAT('小明',i),20000);
#CONCAT的做用是鏈接不一樣類型的數據
#把字符串和數字拼接到一塊兒
set i=i+1;
end WHILE;
End
$$;
delimiter;it

call big_data1(500); #調用io

相關文章
相關標籤/搜索