MySQL基礎(一) 快速建表

2021-10-01 18:46:08 字數 3195 閱讀 9105

環境:win10,mysql資料庫,視覺化工具sqlyog

建立資料庫

建立表向表中新增記錄

基礎查詢表中記錄

條件查詢

模糊查詢

create

database db3;

-- 建立資料庫

show

databases

;-- 顯示所有資料庫

use db3;

-- 使用db3資料庫

select

database()

;-- 檢視當前使用的資料庫

create

table student3 (

id int

,-- 編號

name varchar(20

),-- 姓名

age int

,-- 年齡

*** varchar(5

),-- 性別

address varchar

(100),

-- 位址

math int

,-- 數學

english int

-- 英語 );

show

tables

;-- 顯示資料庫中的所有表

describe student3;

-- 查詢某錶結構

insert

into student3 (id,name,age,***,address,math,english)

values(1

,'馬雲',55

,'男'

,'杭州',66

,78),

(2,'馬化騰',45

,'女'

,'深圳',98

,87),

(3,'馬景濤',55

,'男'

,'香港',56

,77),

(4,'柳岩',20

,'女'

,'湖南',76

,65),

(5,'柳青',20

,'男'

,'湖南',86

,null),

(6,'劉德華',57

,'男'

,'香港',99

,99),

(7,'馬德',22

,'女'

,'香港',99

,99),

(8,'德瑪西亞',18

,'男'

,'南京',56

,65);

select

*from student3;

-- 查詢表中所有記錄

select name,age from student3;

-- 查詢姓名和年齡

select address from student3;

-- 查詢位址

select

distinct address from student3;

-- distinct查詢並去除重複結果集

/*計算表的value*/

select name,math,english,math+english from student3;

-- 四則運算、計算列表值

select name,math,english,math + ifnull(english,0)

from student3;

-- ifnull(a,0) 用來解決 null+值 等於null 的情況

/*起別名*/

select name,math,english as 英語,math + ifnull(english,0)

as 分數 from student3;

-- as關鍵字 起別名

select name,math 數學,english 英語,math + ifnull(english,

0) 分數 from student3;

-- as關鍵字 可以省略

select

*from student3 where age >=20;

select

*from student3 where age =20;

select

*from student3 where age !=20;

-- <> !=

-- 查詢年齡大於等於20 小於等於30

select

*from student3 where age >=

20&& age <=30;

select

*from student3 where age >=

20and age <=30;

select

*from student3 where age between

20and30;

-- 查詢年齡在22 ,18 ,25 歲的資訊

select

*from student3 where age =

22or age =

18or age =25;

select

*from student3 where age in(22

,18,25

);-- 查詢英語成績為null

select

*from student3 where english is

null

;select

*from student3 where english is

notnull

;

-- 單個任意字元:_

-- 多個任意字元:%

select

*from student3 where name like

'馬%'

;-- 姓馬

select

*from student3 where name like

'_化%'

;-- 第二個字是:化

select

*from student3 where name like

'___'

;-- 名字為三個字的人

select

*from student3 where name like

'%德%'

;-- 名字含有德的人

MySQL學習一 建表

目標 建立三張表,學生表student sid,name,gender 課程表course cid,name 分數mark mid,sid,cid,gender 要求sid,cid,mid為主鍵,student姓名只能為f或m mark中sid cid為外來鍵,且組合唯一以保證每個學生每門課只能有乙...

mysql建立使用者表 mysql 建庫建表建使用者

1.建立資料庫 create database school 2.使用資料庫 use school 3.建立使用者 create user jame localhost identified by jame 4.授權使用者 注意這裡是用了 哦,可以自己講school也替換成 號 grant sele...

mysql建表思路 MySQL 建表思路

思想 硬碟如倉庫,表如倉庫中貨架 常用與不常用等分類 欄位如貨物 尺寸是固定或變動 訪問貨物涉及到貨架的佔位 效率。資料型別選用,建表思路,正規化 資料型別特點 資料型別的速度關係 最快 整形 date,time char,enum varchar text blob 最慢 char 與 varch...