数据库SQL(基础代码)
啟動、連接、斷開和停止MySQL服務器
??通過系統服務器和命令提示符(DOS)都可以啟動、連接斷開和停擊MySQL,操作非常簡單。
1.啟動、停止MySQL服務器
??啟動、停止MySQL服務器,的方法有兩種:系統服務器和命令提示符(DOS)。
(1)通過系統服務器啟動、停止MySQL服務器
??如果MySQL設置為Windows服務,則可以通過選擇“開始”→“搜索”→搜索“命令提示符” 命令打開Windows服務管理器。
(2)還可以通過軟件啟動MySQL服務器,比如(Navicat、SQL Server、Oracle、Sybase、DB2)
2. 啟動數據庫
??為了保護MySQL數據庫密的密碼,可以采用如圖2.26所示的密碼輸入方式。如果密碼在-p后直接給出,那么密碼就以明文顯示,例如:
Mysql – u root – h127.0.0.1 – p root
按Enter鍵后再輸入密碼(以加密的方式顯示),然后按Enter鍵即可成功連接MySQ服務器。
3.斷開MySQL服務器
??連接到MySQL服務器后,可以通過在MySQL提示符下輸入exit或者quit命令斷開MySQL連接,格式如下。
4.基礎命令
(1)通過CREATE DATABASE語句創建一個名稱為db_admin1的數據庫
(2)通過CREATE DATABASE 語句創建一個名為db_test的數據庫,并指定字符集為UTF8
create database db_test character set=utf8;(3)使用SHOW命令查看MySQL服務器中的所有數據庫名稱
show databases;(4)選擇名稱為db_admin的數據庫,設置其為當前默認的數據庫
use db_admin;(5)通過DROP DATABASE 語句刪除名為db_admin的數據庫
drop database db_admin;(6)創建一個學生信息表student,該表包括id(學號)、sname(姓名)、sage(年齡)
create table student(id int,sname varchar(30),sage int);(7)插入數據
insert into student-> values(1,'李四',98),-> (2,'王五',99),-> (3,'趙六',97);(8)使用SHOW COLUMNS語句查看數據表student的表結構
show columns from student;(9)使用DESCRIBE語句查看數據表student的表結構
desc student;(10)簡寫查看數據表student中sname字段信息
desc student sname;(11)將數據表student中的字段sage刪除
alter table student drop sage;(12)實現將數據表student更名為tb_student
alter table student rename to tb_student;(13)實現向數據表student中添加一個新字段email,類型為varchar(50),not null
alter table student add(email varchar(50) not null);(14)將數據表student的字段名sname修改為username
alter table student change sname username varchar(100);(15)對數據表student進行重命名,更名后的數據表為tb_student
alter table student rename to tb_student;1.查詢數據:
(1)查詢student表所有數據
(2)查詢student表id為3的數據
select * from student where id=3;(3)查詢student表中score字段中60-80之間的數據
select * from student where score BETWEEN 60 and 80;(4)查詢student表中男性并且score大于80的數據
select * from student where sex='男' and score >80(5)查詢student表中男性或者score大于80的數據
select * from student where sex='男' or score >80(6)查詢student表中id為1、2、3的數據
select * from student where id in(1,2,3)(7)查詢student表中名字為 “王某某” 的信息
select * from student where name like '王%'(8)查詢student表中名字為 “某某王” 的信息
select * from student where name like '%王'(9)查詢student表中名字為 ”某王某” 中的數據
select * from student where name like '%王%'2.分組查詢 GROUP BY
select count(*) as 數量,sex as 性別 from student GROUP BY sex HAVING avg(score)>90排序 order by
倒序
正序
select * from student ORDER BY score分頁 LIMIT 0,5 第一個參數是索引,從0開始;第二個參數是顯示幾條數據
select * from student LIMIT 1,53.刪除語句
單條刪除
刪除所有數據
delete from student;4.聚合數據
count() 統計數量
min() 最小值
select min(score) from student;max()最大值
select max(score) from student;sum()求和
select sum(score) from student;avg()求平均值
select avg(score) from student;5.子查詢
select * from student where sid=(select tid from teacher WHERE tname='樊老師')6.多表聯查
兩個表
三個表
select 表1.字段,表2.字段,表3.字段 FROM 表1 inner join 表2 on 表1.字段=表2.字段 inner join 表3 on 表1.字段=表3.字段 where 條件7.修改數據
update 表名 set 字段=要修改的數據 where 字段=字段名總結
以上是生活随笔為你收集整理的数据库SQL(基础代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 各种应用收集
- 下一篇: mysql时间区间效率_对于sql中使用