mysql表的操作

2021-10-17 04:30:36 字數 3778 閱讀 2937

sql的簡介

dql在mysql管理軟體中,ddl已經定義了資料庫的結構,那麼如何對其中的資料進行管理呢,可以通過sql語句中的dml語言來實現對資料的操作,接下來要詳細介紹dml語言。

語句作用

insert

實現資料的插入

update

實現資料的更新

delete

實現資料的刪除

插入資料insert

# insert into 表名 values (值1,值2.....);

insert into t2 values (1,"張三",19)

;

當不在庫中的表時,一定要將庫和表一起寫出:

insert into sqlproject01.t2 values (2,"李四",24)

;

當插入的時候要插入很多的內容:

insert into t2 values (3,"aaa"

),(4,"bbb"

),(5,"eee"

);

逗號代表的是並列關係,小數點代表的是上下級關係。比如:

庫名.表名 -->上下級關係

值1,值2---->並列關係

如何在表中插入某幾個列的內容:

insert into school.student1 (id,name) values (1,"lisi"

),(2,"zhangsan"

);

只在部分列中插入內容

更新資料update

更新表中的資料:

# 格式:update 表名 set 列名=值 條件 值=值1;

update school.student1 set name=

"張三" where id=1;

;

刪除資料
# 格式:delete from 表名 where condition

delete from school.student1 where name=

"張三"

;

在mysql管理軟體中,可以通過sql語句中的dql語言來實現資料的查詢操作select.

create table t1 (id int,name varchar(20),age int)

;

插入資料:

insert into t1 values (1,"張三",18)

;insert into t1 values (2,"李四",23)

;insert into t1 values (2,"王五",20)

;

# 格式:select 列名1,列名2..... from t1;

select * from t1;

# *代表所有

select host,id,name,***,age from t1;

select * from t1 where name=

"張三"

;select name,post from employee5 where post=

"sale"

;

select name,salary,salary* 14 from employee5;
and並列條件,多個條件組織到一起

查詢hr組中工資大於1000的人

select name,salary from employee where post=

"hr" and salary > 1000;

or滿足乙個就可以查詢出來

select name,salary from employee where salary=6000 or salary=8000;
between and查詢乙個區間

select name,salary from employee where salary between 5000 and 15000;

select name,salary,office from employee where between 5000 and 15000;

# not between and 在範圍之外

select name,salary,office from employee where not between 5000 and 15000;

# in 是什麼什麼

select name,salary from employee where salary in

(4000,5000,6000)

;# not in不在範圍之內

select name,salary from employee where salary not in

(4000,5000,6000)

;

is null檢視沒有描述的

select name,job_description from employee where job_description is null;
is not null檢視非空的

select name,job_description from employee where job_description is not null;
like模糊查詢

# %代表的是任意長度的任意字元

select * from employee where name like "a%"

;# _代表乙個長度的任意字元

select * from employee where name like "a____"

;

查詢排序
以年齡公升序作為排列

order by 預設的是公升序

select * from employee order by age asc;
以年齡降序作為排列

select * from employee order by age desc;
限制查詢數量

select * from employee order by salary desc limit 5;

mysql 表的操作 mysql 表的操作

建立表 檢視表結構 修改表 刪除表 1.建立表 建立表之前選定資料庫 use testx create table table2 屬性名 資料型別 約束 屬性名 資料型別 約束 約束 primary key 該屬性 欄位設為此表主鍵 foreign key 該屬性 欄位為該表外來鍵,即另乙個表的主鍵...

mysql操作表 MySQL表的操作(一)

在建立表之前,首先要指明表在哪個資料庫中建立,也就是要指明命令所要操作的資料庫 用use語句選擇資料庫,一般格式 use 資料庫名 建立表的語法格式如下 例如選擇在linda資料庫中建立乙個use1表 use linda create table use1 id int,name varchar 2...

mysql表操作約束 MySQL操作表的約束

完整性 指資料庫的準確性和一致性。約束 是在表中定義的用於維護資料庫完整性的一些規則。主鍵 給某乙個欄位來唯一標識所有記錄,值是唯一的,非空的 外來鍵 多個表之間參照的完整性。一 設定非空約束 use教學管理資料庫 show tables create table專業表 專業編號char 3 not...