Mysql常用语法总结
生活随笔
收集整理的這篇文章主要介紹了
Mysql常用语法总结
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Mysql常用語法總結(jié)如下:
#連接mysql數(shù)據(jù)庫(Dos下面) mysql -u root -p 123 #創(chuàng)建數(shù)據(jù)庫 create database myschool; #創(chuàng)建表 drop table student create table student ( id int comment '編號', name CHAR(10) comment '姓名' )charset =utf8drop table if exists student; #修改表名 alter table student rename students; #添加字段 alter table students add phone int ; #修改字段 alter table students change phone phones int ; select * from students; #刪除字段 alter table students drop phones; #添加主鍵 alter table student add constraint p_id primary key student (id); #添加外鍵 alter table student add constraint fk_gradeid foreign key (gradeid) references grade (gradeid); select * from student #插入數(shù)據(jù)(單條) insert into student (id,name) values(1,'張三'); #插入多條數(shù)據(jù) insert into student values(5,'李四1'),(6,'王五1'),(7,'趙六1'),(8,'李四2'),(9,'王五2'),(10,'趙六2'); #將查詢結(jié)果插入到新表中(第一種) #新表必須提前創(chuàng)建 create table students (id int ,name CHAR(10) )insert into students(id,name) select * from student #將查詢結(jié)果插入到新表中(第二種) #新表無需提前創(chuàng)建 create table studentss (select * from student); #修改數(shù)據(jù) update student set name = '李四' where id = 1 #刪除數(shù)據(jù) delete from student where id = 1; #truncate語句刪除后講重置自增列,表結(jié)構(gòu)及其字段、約束、索引保持不變,執(zhí)行速度比delete快 truncate table student; #分頁查詢 select * from student where id in(1,2,3,4,5,6,7,8,9) group by name order by id desc limit 2, 5 ###########聚合函數(shù)########### #平均值 select AVG(id) from student; #總記錄數(shù) select COUNT(1) from student; #求和 select SUM(id) from student; #求最大值 select MAX(id) from student; #求最小值 select MIN(id) from student; ##################常用函數(shù)################# # 字符串連接 select CONCAT('my','s','ql') #字符串替換 SELECT INSERT('這是SQL Server數(shù)據(jù)庫',3,10,'MySQL'); # 將字符串轉(zhuǎn)為小寫 SELECT LOWER('MySQL'); #將字符串轉(zhuǎn)為大寫 SELECT UPPER('MySQL'); #字符串截取 SELECT SUBSTRING('JavaMySQLOracle',5,5); ###################日期函數(shù)################# #獲取當(dāng)前日期 SELECT CURDATE(); #獲取當(dāng)前時間 SELECT CURTIME(); #獲取當(dāng)前日期和時間 SELECT NOW(); #返回日期date為一年中的第幾周 SELECT WEEK(NOW()); #返回日期date的年份 SELECT YEAR(NOW()); #返回時間time的小時值 SELECT HOUR(NOW()); #返回時間time的分鐘值 SELECT MINUTE(NOW()); #返回日期參數(shù)date1和date2之間相隔的天數(shù) SELECT DATEDIFF(NOW(),'2008-8-8'); #計算日期參數(shù)date加上n天后的日期 SELECT ADDDATE(NOW(),5); ###############數(shù)學(xué)函數(shù)#################### #返回大于或等于數(shù)值x的最小整數(shù) SELECT CEIL(2.3) #返回小于或等于數(shù)值x的最大整數(shù) SELECT FLOOR(2.3) #返回0~1間的隨機數(shù) SELECT RAND() #################子查詢################### #查看年齡比李斯文小的學(xué)生,要求顯示這些學(xué)生的信息 select * from student where borndate> (select borndate from student where student name ='李斯文').#創(chuàng)建一個年級表 create table grade ( id int , name varchar(50) ) #student表與grade表進行關(guān)聯(lián) select * from student s join grade g on s.gradeid = g.gradeid select * from student s,grade g where s.gradeid = g.gradeid #in子句 select * from student where id in (1,2,3,4) #not in子句 select * from student where id not in (1,4)總結(jié)
以上是生活随笔為你收集整理的Mysql常用语法总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习软件编程对电脑的配置要求高吗?
- 下一篇: 冬至快乐