前言:
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;
總結如上,希望自己用到的時候方便查找~~如果對你有幫助的話,點贊👍吧~