資料庫與表的操作語句

2021-09-19 04:16:50 字數 1769 閱讀 3959

例:create database if not exists `intheima`;

use `itheima`;

create table if not exists `student`(

`id` int unsigned primary key auto_increment comment `學號`,

`name` varchar(32) not null comment `姓名`,

`gender` enum(`男`,`女`) default `男` not null comment `性別`

)default charset=utf8;

一、資料庫

create database `intheima`;(建立mysql資料庫)

create database if not exists `intheima`;(建立mysql資料庫)自動判斷資料庫的名字是否存在

show create database; (查詢資料庫)

show databases;(查詢mysql所有資料庫)

use `intheima`;(選擇資料庫)

drop database `intheima`;(刪除資料庫)

二、表show tables;(檢視資料庫中有哪些表)

desc `student`;(檢視指定表字段資訊)

desc `student` `name`;(檢視表的某一列資訊)

show create table `student`\g(檢視資料表的建立語句和字串編碼)

show columns from `student`;(檢視表的結構)

alter table `student` add `area` varchar(100);(新增字元)

alter table `student` change `area` `desc` char(50);(修改字串名稱)

alter table `student` modify `desc` varchar(255);(修改字串型別)

alter table `student` drop `desc`;(刪除指定字段)

alter table `student` rename `stu`;(修改資料表名稱)

rename table `stu` to `student`;(將名字為stu的從命名為student)

drop table if exisis `student`;(刪除存在的資料表student)

三、資料表單查詢語句

1.檢視資料庫的所有的表

select * from `subject1`;

2.查詢指定字段

select `name`,`id` from `subject1`;

指定字段(設定別名)

select `name` as `code` from `subject1`;

3.條件查詢(比較運算子)(= <> != < <= > >=)

select * from `subject1` where `price`=89;

select * from `subject1` where (`id`,`price`)=(10,89);

4.帶distinct關鍵字查詢

select distinct `name`,`***` from `subject1`;

5.帶in關鍵字的查詢 in--包含, not in---不包含

select *from `subject1` where `id`in (1,6);

資料庫語句 表操作

1.檢視表結構 desc 表名2.建立表結構的語法 create table table name 欄位名 資料型別 可選的約束條件 demo 建立班級和學生表 create table classes id int unsigned auto increment primary key not n...

資料庫操作表資料基本語句

insert into 語句用於向表中插入新記錄。sql insert into語法 insert into 語句可以有兩種編寫形式。第一種形式無需指定要插入資料的列名,只需提供被插入的值即可 insert into table name 表名 values value1 取值 value2,val...

資料庫操作語句

mysql分頁limit用法 limit後的第乙個引數是輸出記錄的初始位置,第二個引數偏移量。如 select from tables limit 10,3 返回的就是11到13,3條資料。如果只給定乙個引數,它表示返回最大的記錄行數目 select from table limit 5 檢索前 5...