前言:
SQL 是用于通路和處理資料庫的标準的計算機語言。
什麼是 SQL?
SQL 指結構化查詢語言
SQL 使我們有能力通路資料庫
SQL 是一種 ANSI 的标準計算機語言
編者注:ANSI,美國國家标準化組織
#顯示資料庫
show databases;
#判斷是否存在資料庫test_mysql,有的話先删除
drop database if exists test_mysql;
#建立資料庫
create database test_mysql;
#删除資料庫
drop database test_mysql;
#使用該資料庫
use test_mysql;
#顯示資料庫中的表
show tables;
#先判斷表是否存在,存在先删除
drop table if exists student;
#建立表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
)default charset=utf8;
#删除表
drop table student;
#檢視表的結構
describe student; #可以簡寫為desc student;
#插入資料
insert into student values(null,'test','2018-10-2');
#查詢表中的資料
select * from student;
select id,name from student;
#修改某一條資料
update student set name='jack' where id=4;
#删除資料
delete from student where id=8;
# and 且
select * from student where date>'2018-1-2' and date<'2018-12-1';
# or 或
select * from student where date<'2018-11-2' or date>'2018-12-1';
#between
select * from student where date between '2018-1-2' and '2018-12-1';
#in 查詢制定集合内的資料
select * from student where id in (1,3,5);
#排序 asc 升序 desc 降序
select * from student order by id asc;
#分組查詢 #聚合函數
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as 'Avg' from student;
select count(*) from student; #統計表中總數
select count(sex) from student; #統計表中性别總數 若有一條資料中sex為空的話,就不予以統計~
select sum(id) from student;
#查詢第i條以後到第j條的資料(不包括第i條)
select * from student limit 2,5; #顯示3-5條資料
#修改資料
update student set name='test' where id=2;
update student set name='花花',sex='女' where id=2
delete from student where id=2;
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table student rename to test_1;
#向表中增加一個字段(列)
#格式:alter table tablename add columnname type;/alter table tablename add(columnname type);
alter table student add age varchar(20) set default '1'; #set default 設定預設值
#修改表中某個字段的名字
alter table tablename change columnname newcolumnname type; #修改一個表的字段名
alter table student change name test_name varchar(50);
#去掉表中字段age的預設值
alter table student alter age drop default;
#去掉表中字段age
alter table student drop column age;
#删除表中主鍵
alter table student drop primary key;
#表中增加主鍵
#alter table add primary key (column1,column2,....,column)
alter table student add primary key (student_id);
#用文本方式将資料裝入資料庫表中(例如D:/mysql.txt)
load data local infile "D:/mysql.txt" into table MYTABLE;
#導入.sql檔案指令(例如D:/mysql.sql)
source d:/mysql.sql; #或者 /. d:/mysql.sql;
總結如上,希望自己用到的時候友善查找~~如果對你有幫助的話,點贊吧~
作者:
擱淺出處:
http://www.cnblogs.com/xiaoxi-3-/如果對您有幫助,請關注我的同名簡書:
https://www.jianshu.com/u/da1677475c27本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。