mysql常用語句 MySQL常用語句

2021-10-18 22:21:35 字數 2261 閱讀 5588

create table student(

id int primary key auto_increment comment '學號',

name varchar(200) comment '姓名',

age int comment '年齡'

) comment='學生資訊';

修改表注釋

alter table student comment '學生表';

修改列注釋

alter table student modify column name varchar(100) comment '姓名';

show variables like 'character_set_client'; #查詢字符集

show databases; #列出所有的伺服器上的資料庫alter

create database if not exists test; #建立乙個資料庫

drop database fk; #刪除資料庫

show tables from test; #顯示乙個資料庫中的表

use test; #選擇資料庫

#建立部門表

create table tb_dept(

id int primary key auto_increment,#部門編號 整形 主鍵 自增長

name varchar(18),#部門名稱

description varchar(100)#描述

show tables from test;#顯示當前資料庫中所有表的名稱

desc tb_dept;#檢視表資訊

show create table tb_dept;#檢視建立表時候用的sql語句

#建立員工表

create table tb_emp(

id int primary key auto_increment,#auto_increment只是mysql特有的

name varchar(18),

*** varchar(2),

age int,

address varchar(200),

email varchar(100)

drop table tb_dept; #刪除部門表

#修改列型別

#注意:不是任何情況下都可以去修改的,

#只有當字段只包含空值時才可以修改。

alter table tb_emp modify *** varchar(4);

#增加列

alter table tb_emp add tel varchar(12);

#刪除列

alter table tb_emp drop tel;

alter table tb_emp drop column tel;

#列改名

alter table tb_emp change name emp_name varchar(18);

#更改表名

alter table tb_emp rename emp;

rename table emp to tb_emp;

insert into dept_emp (name,***,age,address,email)values('','','','','');

#約束#是在表上強制執行地資料校驗規則,主要用於保證資料庫地完整性

not null

unique 唯一鍵tb_depttb_dept

primary key

foreign key 外來鍵

check 檢查

create table tb_emp(

id int primary key auto_increment,

name varchar(18),

*** varchar(2) default'男' check(***='男'or ***='女'),#表級寫法check

在mysql中不起作用

age int,

address varchar(200),

email varchar(100) unique,

dept_id int,#references tb_dept(id) #表級寫法外來鍵不起作用

constraint foreign key fk_emp(dept_id) references tb_dept(id)

#建立表之後在新增

alter table tb_emp add constraint foreign key fk_emp(dept_id) references tb_dept(id);

MySQL常用語句

and和or可以混用,and比or具有更高的優先順序,但盡量使用圓括號區分 自動過濾重複的資料owner,關鍵字distinct select distinct owner from pet 按照生日公升序排列,關鍵字order by select name,birth from pet order...

MySQL常用語句

1.insert into mis users name,age values ywm 13 select id,name,age from mis users limit 0,50 delete from mis users where id 8 update mis users set name...

MySQL常用語句

基本操作 檢視所有資料庫 表的名稱 show databases tables 檢視表的字段資訊 desc 表名 檢視表的所有資訊 show create table 表名 更改當前資料庫 use 資料庫名 ddl語句 建立新錶 create table 表名 欄位1 字段型別,欄位2 字段型別,c...