資料表的基本操作

2021-10-03 06:00:14 字數 1440 閱讀 7203

資料表

create table 表名(

id int(5) unsingned zerofill primary key auto_increment, :(5)包含5個位元組流id資料的大小

name char(10) unique, :(10)個字元

age int not null default 0,

height float(5,2) ,

desc varchar(20)

);primary key : 主鍵,乙個表中只能有乙個,作用讓該字段值,唯一且不為空

auto_increment :編號自動增長,只能作用在數字型別欄位上

unique : 唯一性約束,字段值不能重複

not null : 表示非空

default : 預設的值

float (5,2) : 數字佔5位,小數點保留2位

1.查詢資料

1.  查詢所有列

select * from 表名;

2. 查詢指定列

select 列1,列2,... from 表名;

dml

2.插入資料

全表字段插入

insert into 表名 values (…)

部分列插入

insert into 表名 values(值,…); :注意:所有的支付按都給值,如果有預設不給自,也可以使用default佔位

全表插入多條資料

insert into 表名 values(…),(…)…;

全表插入指定資料

insert into 表名(列1,…) values(值1,…),(值1,…)…;

注意:主鍵列是自動增長,但是在全列插入時需要佔位,通常使用空值(0或者null或者default)

在全列插入時,如果欄位列有預設值可以使用 default 來佔位,插入後的資料就是之前設定的預設值

3.修改資料

全表跟

update 表名 set 字段 = 值;
update t_user set age = 25;

條件跟新(指定name)

update 表名 set 字段 = 值 where 條件;

update t_user set age = 35,where name = rose;

將原欄位基礎上更新值

update t_user set age = age + 5; :可+/-/*//

4.刪除資料

delete from 表名 where 條件;

刪除滿足條件的資料

將名字rose刪除

delete from t_user where name = rose;

刪除所有資料

delete from 表名;

delete from t_user;

資料表的基本操作

和資料庫操作一樣,在資料表在操作中,也是是圍繞增刪改查來操作。但是在這一章節主要講解資料表的增刪改操作。查詢操作是資料庫中最為重要的操作。會單獨一章節講解。資料表是由若干個字段組成的,每個字段表示不同型別的資料。所以在建立表的時候,需要為每個字段指定相應的資料型別。作用 查詢資料就是通過客戶端通過 ...

資料表的基本操作

建立資料庫,sql語句為 create database 資料庫名 檢視資料庫是否成功,sql語句如下 show databases 選擇資料庫 use 資料庫名 sql語句create table用於建立資料表其基本語法 建立student表,sql語句如下 create table studen...

資料表的基本操作

資料表的基本操作 1.資料表的建立 先建立乙個資料庫,給這個資料庫起名,我給他取名為zjj 然後就可以創造表了,我給這個表取名為ww create table ww id int 11 name varchar 25 deptid int 11 salary float 括號中定義該錶欄位名 字段資...