MySQL的使用 資料表的基本操作

2021-09-12 10:48:39 字數 2588 閱讀 4009

1.掌握如何建立資料表

2.掌握檢視資料表結構的方法

3.掌握如何修改資料表

4.熟悉刪除資料表的方法

建立資料表的語句為create table,語法規則如下:

create  table 《表名》

(欄位名1,資料型別[列級別約束條件] [缺省級],

欄位名2,資料型別[列級別約束條件] [缺省級],

......

[表級別約束條件]

2、多欄位聯合主鍵(主鍵由多個字段聯合組成),語法如下:

primary key [欄位1,欄位2,......,欄位n]
(3)、建立外來鍵的語法如下:

[constraint 《外鍵名》] foreign key  欄位名1 [,欄位名2,...]

references 《主表名》 主鍵列1 [,主鍵列2, ...]

4.使用非空約束,建立語法為:

欄位名 資料型別 not null
5、使用唯一性約束的語法規則如下:

(1)、定義完列之後直接指定唯一約束,語法如下:

欄位名 資料型別 unique
(2)、定義所有列之後指定唯一約束,語法如下:

[constraint 《約束名》] unique(《欄位名》)
6、使用預設約束,語法如下:

欄位名 資料型別 default  預設值
7、設定表的屬性自動增加的語法:

欄位名 資料型別 auto_increment
8、檢視資料表結構:

desc/describe 表名;
9、檢視表詳細結構語句:

show cretae table 《表名》;
10、檢視資料庫中的表是否建立成功:

show tables;
11、檢視資料表的資料:

select * from 《表名》;
修改表名,語法規則如下:

alter table 《舊表名》 rename to 《新錶名》;
2. 修改欄位的資料型別,語法規則如下:

alter table 《表名》 modify 《欄位名》 《資料型別》;
3.修改表中的欄位名,語法規則如下:

alter table 《表名》 change 《舊欄位名》 《新欄位名》 《新資料型別》;
4.新增資料表的字段(預設新增在已有字段後面):

alter table 《表名》 add 《新欄位名》 《資料型別》;
5.新增資料表字段(新增到所有字段前面):

alter table tb_student add 《新欄位名》 int first;
6.新增資料表字段(新增到某個欄位的後面):

alter table 《表名》 add *** 《資料型別》 after 《某欄位名》;

如:alter table tb_student add *** char(3) after st_name;

7.刪除資料表中的某個字段:

alter table 《表名》drop 《欄位名》;
資料表基本操作的作業:        

-- 建立乙個zxh_db的資料庫

create database zxh_db;

-- 使用zxh_db資料庫

use zxh_db;

-- 建立乙個文章表

create table zxh_article_type

( at_id int primary key auto_increment,

at_name varchar(250) not null

)engine=innodb;

-- 建立乙個zxh_article文章表

create table zxh_article

( a_id int primary key,

content varchar(250) not null,

writer varchar(20) not null,

at_id int,

constraint fk_a_at foreign key(at_id) references zxh_article_type(at_id)

)engine=innodb;

mysql資料表命令是 MySQL資料表操作命令

mysql語句 1 修改表名 rename table 舊表名 to 新錶名 2 修改字段型別 alter table 表名 modify column 欄位名 字段型別 長度 3 修改欄位名稱和型別 alter table 表名 change 現有欄位名稱 修改後欄位名稱 資料型別 4 增加字段 ...

mysql 資料表的基本操作

1.建立表 create database name use database name create tabletable name id int 11 name varchar 25 salary float 2.show tables 顯示當前資料庫的表 3.單字段主鍵,設定主鍵有兩種情況。主...

mysql資料表的基本操作

一 先建立乙個資料庫,然後使用資料庫 資料庫舉例命名為student 1.建立資料庫 create database student 2.使用資料庫 use student 二 建立乙個儲存資訊的資料表 命名為test create table test name varchar 25 age in...